Skip to content

Commit 37ae890

Browse files
authored
Merge pull request #3 from xebialabs-community/listBookmarks
List bookmarks
2 parents b17ec61 + 03d7fe0 commit 37ae890

File tree

8 files changed

+79
-267
lines changed

8 files changed

+79
-267
lines changed

build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ sourceSets.test.resources.srcDirs = ["src/test/jython", "src/test/resources"]
4040

4141

4242
task testJython(type: Exec, dependsOn: ['testClasses']) {
43-
environment = ["itest_conf_file": "$projectDir/src/test/jython/itests/itest-conf.json","CLASSPATH": sourceSets.test.runtimeClasspath.asPath, "PATH": System.getenv("PATH"), "HOME": System.getenv("HOME")]
44-
commandLine jythonInterpreter, "-Dpython.path=/opt/xlr/server/plugins/xlr-xld-plugin-7.1.0.jar", "-m", "unittest", "discover", "-s", "./src/test/jython", "-p", "*_test.py", "-v"
43+
def classpath = sourceSets.main.runtimeClasspath.asPath + ":$projectDir/build/jython/main:$projectDir/src/main/resources"
44+
environment = ["itest_conf_file": "$projectDir/src/test/jython/itests/itest-conf.json","CLASSPATH": classpath, "PATH": System.getenv("PATH"), "HOME": System.getenv("HOME")]
45+
commandLine jythonInterpreter, "-m", "unittest", "discover", "-s", "./src/test/jython", "-p", "*_test.py", "-v"
4546
workingDir = projectDir
4647
}
4748

src/main/resources/delphix/DelphixClient.py

Lines changed: 33 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -7,205 +7,50 @@
77
#
88
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
99
#
10-
11-
import requests
1210
import json
1311

12+
from delphixpy.web.selfservice import bookmark
13+
from delphixpy.web.selfservice import branch
1414

15-
class DelphixClient:
16-
17-
def __init__(self, server):
18-
self.server = server
19-
self.cookies = self.get_session()
20-
21-
def get_session(self):
22-
print('- open session on {0}'.format(self.server['url']))
23-
headers = {'Content-type': 'application/json'}
24-
data = {
25-
"type": "APISession",
26-
"version": {
27-
"type": "APIVersion",
28-
"major": 1,
29-
"minor": 8,
30-
"micro": 2
31-
}
32-
}
33-
34-
r = requests.post('{0}/resources/json/delphix/session'.format(self.server['url']),
35-
headers=headers,
36-
data=json.dumps(data))
37-
return r.cookies
38-
39-
def _login(self):
40-
print('- login')
41-
headers = {'Content-type': 'application/json'}
42-
data = {
43-
"type": "LoginRequest",
44-
"username": self.server['username'],
45-
"password": self.server['password'],
46-
}
47-
r = requests.post('{0}/resources/json/delphix/login'.format(self.server['url']),
48-
headers=headers,
49-
cookies=self.cookies,
50-
data=json.dumps(data))
51-
52-
if self._response_ok(r):
53-
print("- Authenticated ! ")
54-
else:
55-
raise self._response_error(r)
56-
return r.text
57-
58-
def _logout(self):
59-
print('- logout')
60-
headers = {'Content-type': 'application/json'}
61-
requests.post('{0}/resources/json/delphix/logout'.format(self.server['url']),
62-
headers=headers,
63-
cookies=self.cookies)
64-
65-
def _get_params(self, vdb):
66-
result = {'ref': 'NOT_FOUND', 'cont': 'NOT_FOUND', 'vsrc': 'NOT_FOUND'}
67-
print('- get params')
68-
headers = {'Content-type': 'application/json'}
69-
70-
r = requests.get('{0}/resources/json/delphix/database'.format(self.server['url']),
71-
headers=headers,
72-
cookies=self.cookies)
15+
from delphix.lib.DelphixSession import DelphixSession
16+
from delphix.lib.GetReferences import find_obj_name
17+
import delphix.lib.Util as Util
7318

74-
for db in r.json()['result']:
75-
if db['name'] == vdb:
76-
result['ref'] = db['reference']
77-
result['cont'] = db['provisionContainer']
7819

79-
r = requests.get('{0}/resources/json/delphix/source'.format(self.server['url']),
80-
headers=headers,
81-
cookies=self.cookies)
20+
class DelphixClient(object):
8221

83-
for db in r.json()['result']:
84-
if db['name'] == vdb:
85-
result['vsrc'] = db['reference']
86-
87-
print('- database reference {ref}'.format(**result))
88-
print('- database provisionContainer {cont}'.format(**result))
89-
print('- source reference {vsrc}'.format(**result))
90-
return result
91-
92-
def _rewind(self, vdb):
93-
parameters = self._get_params(vdb=vdb)
94-
print("- rewind ")
95-
headers = {'Content-type': 'application/json'}
96-
data = {
97-
"type": "OracleRollbackParameters",
98-
"timeflowPointParameters": {
99-
"type": "TimeflowPointSemantic",
100-
"container": parameters['ref'],
101-
"location": "LATEST_SNAPSHOT"
102-
}
103-
}
104-
105-
r = requests.post(
106-
'{0}/resources/json/delphix/database/{1}/rollback'.format(self.server['url'], parameters['ref']),
107-
headers=headers,
108-
cookies=self.cookies,
109-
data=json.dumps(data))
110-
if self._response_ok(r):
111-
print("- Job {0}".format(r.json()['job']))
112-
print("- Action {0}".format(r.json()['action']))
113-
return {'job': r.json()['job'], 'action': r.json()['action']}
114-
else:
115-
raise self._response_error(r)
116-
117-
def _snapshot(self, vdb):
118-
parameters = self._get_params(vdb=vdb)
119-
print("- snapshot ")
120-
headers = {'Content-type': 'application/json'}
121-
data = {
122-
"type": "OracleSyncParameters"
123-
}
124-
125-
r = requests.post(
126-
'{0}/resources/json/delphix/database/{1}/sync'.format(self.server['url'], parameters['ref']),
127-
headers=headers,
128-
cookies=self.cookies,
129-
data=json.dumps(data))
130-
131-
if self._response_ok(r):
132-
print("- Job {0}".format(r.json()['job']))
133-
print("- Action {0}".format(r.json()['action']))
134-
return {'job': r.json()['job'], 'action': r.json()['action']}
135-
else:
136-
raise self._response_error(r)
137-
138-
def _stop(self, vdb):
139-
parameters = self._get_params(vdb=vdb)
140-
print("- stop ")
141-
headers = {'Content-type': 'application/json'}
142-
# data = {
143-
# "type": "OracleSyncParameters"
144-
# }
145-
146-
r = requests.post(
147-
'{0}/resources/json/delphix/database/{1}/stop'.format(self.server['url'], parameters['ref']),
148-
headers=headers,
149-
cookies=self.cookies,
150-
# data=json.dumps(data))
22+
def __init__(self, server):
23+
self.engine = DelphixSession.create(server).server_session
15124

152-
if self._response_ok(r):
153-
print("- Job {0}".format(r.json()['job']))
154-
print("- Action {0}".format(r.json()['action']))
155-
return {'job': r.json()['job'], 'action': r.json()['action']}
156-
else:
157-
raise self._response_error(r)
15825

159-
def _start(self, vdb):
160-
parameters = self._get_params(vdb=vdb)
161-
print("- snapshot ")
162-
headers = {'Content-type': 'application/json'}
163-
# data = {
164-
# "type": "OracleSyncParameters"
165-
# }
26+
@staticmethod
27+
def create_client(http_connection):
28+
return DelphixClient(http_connection)
16629

167-
r = requests.post(
168-
'{0}/resources/json/delphix/database/{1}/start'.format(self.server['url'], parameters['ref']),
169-
headers=headers,
170-
cookies=self.cookies)
171-
# data=json.dumps(data))
30+
def delphix_listbookmarks(self, variables):
31+
js_bookmarks = bookmark.get_all(self.engine)
17232

173-
if self._response_ok(r):
174-
print("- Job {0}".format(r.json()['job']))
175-
print("- Action {0}".format(r.json()['action']))
176-
return {'job': r.json()['job'], 'action': r.json()['action']}
177-
else:
178-
raise self._response_error(r)
179-
def _response_ok(self, r):
180-
return r.status_code == 200 and r.json()['type'] == 'OKResult'
33+
path = Util.split_path(variables['template_path'])
18134

182-
def _response_error(self, r):
183-
return Exception(r.text)
35+
result = []
36+
for js_bookmark in js_bookmarks:
37+
if js_bookmark.template_name != path['template_name']:
38+
continue
18439

185-
def snapshot(self, vdb):
186-
self._login()
187-
print("- Snapshot {0} with Delphix".format(vdb))
188-
result = self._snapshot(vdb)
189-
self._logout()
190-
return result
40+
add_it = False
41+
if variables['tag_filter'] is None:
42+
tag = js_bookmark.tags if js_bookmark.tags else None
43+
add_it = True
44+
else:
45+
for tag in variables['tag_filter']:
46+
if tag in js_bookmark.tags:
47+
add_it = True
48+
break
19149

192-
def rewind(self, vdb):
193-
self._login()
194-
print("- Rewind {0} with Delphix".format(vdb))
195-
result = self._rewind(vdb)
196-
self._logout()
197-
return result
198-
199-
def stop(self, vdb):
200-
self._login()
201-
print("- Stop {0} with Delphix".format(vdb))
202-
result = self._stop(vdb)
203-
self._logout()
204-
return result
50+
if add_it:
51+
branch_name = find_obj_name(self.engine, branch, js_bookmark.branch)
52+
key = Util.form_bookmark_path(js_bookmark.template_name, js_bookmark.container_name, branch_name, js_bookmark.name)
53+
result.append(key)
54+
print "%s\n" % key
20555

206-
def start(self, vdb):
207-
self._login()
208-
print("- start {0} with Delphix".format(vdb))
209-
result = self._start(vdb)
210-
self._logout()
211-
return result
56+
variables['bookmarks'] = result
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright 2017 XEBIALABS
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
#
6+
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
#
8+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9+
#
10+
11+
from delphix.DelphixClient import DelphixClient
12+
13+
delphix_client = DelphixClient.create_client(server)
14+
method = str(task.getTaskType()).lower().replace('.', '_')
15+
call = getattr(delphix_client, method)
16+
output = call(locals())

src/main/resources/delphix/lib/GetReferences.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
"""
1414

1515
import re
16-
from datetime import datetime
17-
from dateutil import tz
1816

1917
from delphixpy.v1_9_0.web.service import time
2018
from delphixpy.v1_9_0.exceptions import RequestError
@@ -35,30 +33,6 @@
3533

3634
VERSION = 'v.0.2.0019'
3735

38-
def convert_timestamp(engine, timestamp):
39-
"""
40-
Convert timezone from Zulu/UTC to the Engine's timezone
41-
engine: A Delphix engine session object.
42-
timestamp: the timstamp in Zulu/UTC to be converted
43-
"""
44-
45-
default_tz = tz.gettz('UTC')
46-
engine_tz = time.time.get(engine)
47-
48-
try:
49-
convert_tz = tz.gettz(engine_tz.system_time_zone)
50-
utc = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
51-
utc = utc.replace(tzinfo=default_tz)
52-
converted_tz = utc.astimezone(convert_tz)
53-
engine_local_tz = '{} {} {}'.format(str(converted_tz.date()),
54-
str(converted_tz.time()),
55-
str(converted_tz.tzname()))
56-
57-
return engine_local_tz
58-
except TypeError:
59-
return None
60-
61-
6236
def find_all_objects(engine, f_class):
6337
"""
6438
Return all objects from a given class

src/main/resources/delphix/listBookmarks.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/main/resources/synthetic.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
<type type="delphix.Task" extends="xlrelease.PythonScript" virtual="true">
24+
<property name="scriptLocation" default="delphix/DelphixTask.py" hidden="true"/>
2425
<property name="iconLocation" default="delphix/delphix.png" hidden="true"/>
2526
<property name="taskColor" hidden="true" default="#70767a"/>
2627

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# Copyright 2017 XEBIALABS
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
#
6+
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
#
8+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9+
#
10+
11+
12+
13+
import unittest
14+
from itests import DelphixServerCi
15+
from delphix.DelphixClient import DelphixClient
16+
17+
18+
class ListBookmarks(unittest.TestCase):
19+
def test_list_bookmarks(self):
20+
client = DelphixClient.create_client(DelphixServerCi())
21+
variables = {"template_path": "/something" }
22+
client.delphix_listbookmarks(variables)
23+
self.assertIsNotNone(variables["bookmarks"])

0 commit comments

Comments
 (0)