Skip to content

Commit

Permalink
Merge pull request #2 from RomanNyschuk/master
Browse files Browse the repository at this point in the history
D42-16628 - Cherwell - pageNumber property needs to be set to record number.
D42-16864 - Device42/Cherwell Integration error
D42-16865 - Cherwell sync is not mapping software to each device
Updated to support newer versions of the Cherwell API.
Fixed recursion issue.
Fixed API parameter for "Copy Networks from Device42 to Cherwell" task.
  • Loading branch information
cscaglioned42 authored Sep 17, 2020
2 parents 30765d6 + 8b5fce0 commit 6987475
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
63 changes: 48 additions & 15 deletions lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,49 @@ def fill_business_object_doql(fields, data, bus_ob_id, match_map, existing_objec
return response_object


def get_existing_cherwell_objects(service, configuration_item, page, data, fields=None):
def get_existing_cherwell_objects(service, configuration_item, page, fields=None):
"""
PageNumber essentialy means RowNumber
If you have "totalRows": 100 then PageSize = 50 the logic dictates that you should have 2 pages worth of content of content
However PageNumber actually refers to the row number. So you will have the first 50 rows on PageNumber=1 and then 2-51 on PageNumber=2 at PageNumber = 50 it will return 50-100 then at PageNumber = 51 it will return 51-100.
PageNumber works more like Row Number and PageSize just dictates how many to pull from that point in the rows.
"""
page_size = 100
bus_ib_pub_ids_request_data = {
"busObId": configuration_item,
'includeAllFields': True,
"pageNumber": page,
"pageSize": 100
"pageSize": page_size
}

if service.is_updated_page_number_version():
bus_ib_pub_ids_request_data['pageNumber'] = (page - 1) * page_size + 1
else:
bus_ib_pub_ids_request_data['pageNumber'] = page

if isinstance(fields, collections.Iterable):
fields = [str(field) for field in fields]
bus_ib_pub_ids_request_data['fields'] = fields
bus_ib_pub_ids_request_data['includeAllFields'] = False
else:
bus_ib_pub_ids_request_data['includeAllFields'] = True

bus_ib_pub_ids = service.request('/api/V1/getsearchresults', 'POST', bus_ib_pub_ids_request_data)
data += bus_ib_pub_ids["businessObjects"]
print("Loaded {} of {} business objects".format(len(data), bus_ib_pub_ids["totalRows"]))
data = []
while True:
bus_ib_pub_ids = service.request('/api/V1/getsearchresults', 'POST', bus_ib_pub_ids_request_data)
data += bus_ib_pub_ids["businessObjects"]
print("Loaded {} of {} business objects".format(len(data), bus_ib_pub_ids["totalRows"]))

if bus_ib_pub_ids["totalRows"] > page * 100:
page += 1
get_existing_cherwell_objects(service, configuration_item, page, data)

if service.is_updated_page_number_version():
if not(bus_ib_pub_ids["totalRows"] > (page - 1) * page_size):
break
bus_ib_pub_ids_request_data["pageNumber"] = (page - 1) * page_size + 1
else:
if not(bus_ib_pub_ids["totalRows"] > page * page_size):
break
bus_ib_pub_ids_request_data["pageNumber"] = page

return data

Expand Down Expand Up @@ -384,20 +405,26 @@ def refresh_existing_cherwell_objects(self, fields=()):
if field_id:
field_ids.append(field_id)

items = get_existing_cherwell_objects(self.cherwell_api, self.bus_ob_id, 1, [], field_ids)
items = get_existing_cherwell_objects(self.cherwell_api, self.bus_ob_id, 1, field_ids)
self.cherwell_items = get_existing_cherwell_objects_map(items, full_objects=True)

def get_related_cherwell_objects(self, d42_pk, relationship_id, params=None):
cherwell_obj = self.get_cherwell_obj(d42_pk)
if not cherwell_obj:
raise Exception('Cherwell object not found')

page_size = 200
page = 1
def_params = {
'pageNumber': 1,
'pageSize': 200,
'pageSize': page_size,
'allfields': False,
}

if self.cherwell_api.is_updated_page_number_version():
def_params['pageNumber'] = (page - 1) * page_size + 1
else:
def_params['pageNumber'] = page

if not params:
params = def_params
else:
Expand All @@ -424,10 +451,16 @@ def get_related_cherwell_objects(self, d42_pk, relationship_id, params=None):
raise Exception(msg)

res += response.get('relatedBusinessObjects', [])
if response.get('totalRecords', 0) > response.get('pageNumber') * params.get('pageSize'):
params['pageNumber'] = response.get('pageNumber') + 1

page += 1
if self.cherwell_api.is_updated_page_number_version():
if not(response.get('totalRecords', 0) > (page - 1) * page_size):
break
params["pageNumber"] = (page - 1) * page_size + 1
else:
break
if not(response.get('totalRecords', 0) > page * page_size):
break
params["pageNumber"] = page

return res

Expand Down Expand Up @@ -728,7 +761,7 @@ def from_d42(source, mapping, _target, _resource, target_api, resource_api, conf
)


existing_objects = get_existing_cherwell_objects(target_api, configuration_item, 1, [])
existing_objects = get_existing_cherwell_objects(target_api, configuration_item, 1)
existing_objects_map = get_existing_cherwell_objects_map(existing_objects)
bus_object = target_api.request('/api/V1/getbusinessobjecttemplate', 'POST', bus_object_config)
success = perform_butch_request(bus_object, mapping, match_map, _target, _resource, source, existing_objects_map,
Expand Down
5 changes: 3 additions & 2 deletions mapping.xml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
url=""
user=""
password=""
client_id=""/>
client_id=""
updated_page_number_version="9.7.0"/>
<device42
url=""
user=""
Expand Down Expand Up @@ -65,7 +66,7 @@
<api>
<target target="cherwell" method="POST" update_method="PATCH" path="/api/V1/savebusinessobjectbatch"/>
<resource model="device" target="device42" method="GET" extra-filter="last_updated_gt=2016-09-04 00:00"
path="/api/1.0/devices/all/?type=physycal&amp;is_it_blade_host=yes&amp;"/>
path="/api/1.0/devices/all/?type=physical&amp;is_it_blade_host=yes&amp;"/>
</api>

<configuration-item bus-ob-id="9343f947d5000ac81406e746f0975ddeb11c184480"/>
Expand Down
17 changes: 17 additions & 0 deletions sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@ def __init__(self, settings):
class Cherwell(Service):
def __init__(self, settings):
super().__init__(settings)

self.updatedVersion = settings.attrib.get("updated_page_number_version", "9.7.0")

headers = {
'accept': "application/json",
'content-type': "application/x-www-form-urlencoded",
}

url = "%s/api/V1/serviceinfo" % (self.url,)

response = requests.request("GET", url, headers=headers)
response_data = deserialize_json(response.content.decode('utf-8'))
self.currentVersion = response_data['apiVersion']

data = (
('password', bytes(self.password, 'utf-8')),
('username', self.user),
Expand Down Expand Up @@ -104,6 +114,13 @@ def perform_request(path, method, data=()):

return result

def is_updated_page_number_version(self):
currentVersion = [int(i) for i in self.currentVersion.split('.')]
updatedVersion = [int(i) for i in self.updatedVersion.split('.')]
if currentVersion >= updatedVersion:
return True
return False


class Device42(Service):
def request(self, path, method, data=(), doql=None):
Expand Down

0 comments on commit 6987475

Please sign in to comment.