-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commits adds a new client which can access services outside "/table" namespace. A new client, on top of the rest client, has been added to add functionality of snow services. This is needed because the snow services don't necessary have the same behaviour. For example, the `Table API` server puts the total items in the custom header `x-total-count` where others don't. The high level clients `TableClient` and `GenericClient` inherits this client. A new field `api_path` is added in the modules `api` and `api_info` to choose between `TableClient` or `GenericClient`. `api_path` is mutally exclusive with `resource` which refers to a table in `Table API`. ``` - name: Create test ci - check mode servicenow.itsm.api: api_path: "api/now/cmdb/instance/cmdb_ci_linux_server" action: post data: attributes: name: "linux99" firewall_status: "intranet" source: "ServiceNow" ```
- Loading branch information
Showing
8 changed files
with
232 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2024, Red Hat | ||
# | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
__metaclass__ = type | ||
|
||
from . import snow | ||
|
||
|
||
class GenericClient(snow.SNowClient): | ||
def __init__(self, client, batch_size=1000): | ||
super(GenericClient, self).__init__(client, batch_size) | ||
|
||
def list_records(self, api_path, query=None): | ||
return self.list(api_path, query) | ||
|
||
def get_record(self, api_path, query, must_exist=False): | ||
return self.get(api_path, query, must_exist) | ||
|
||
def get_record_by_sys_id(self, api_path, sys_id): | ||
response = self.client.get("/".join((api_path, sys_id))) | ||
record = response.json["result"] | ||
|
||
return record | ||
|
||
def create_record(self, api_path, payload, check_mode, query=None): | ||
if check_mode: | ||
# Approximate the result using the payload. | ||
return payload | ||
return self.create(api_path, payload, query) | ||
|
||
def update_record(self, api_path, record, payload, check_mode, query=None): | ||
if check_mode: | ||
# Approximate the result by manually patching the existing state. | ||
return dict(record, **payload) | ||
return self.update(api_path, record["sys_id"], payload, query) | ||
|
||
def delete_record(self, api_path, record, check_mode): | ||
if not check_mode: | ||
return self.delete(api_path, record["sys_id"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2024, Red Hat | ||
# | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
__metaclass__ = type | ||
|
||
|
||
from . import errors | ||
|
||
|
||
class SNowClient: | ||
def __init__(self, client, batch_size=1000): | ||
self.client = client | ||
self.batch_size = batch_size | ||
|
||
def list(self, api_path, query=None): | ||
base_query = self._sanitize_query(query) | ||
base_query["sysparm_limit"] = self.batch_size | ||
|
||
offset = 0 | ||
total = 1 # Dummy value that ensures loop executes at least once | ||
result = [] | ||
|
||
while offset < total: | ||
response = self.client.get( | ||
api_path, | ||
query=dict(base_query, sysparm_offset=offset), | ||
) | ||
|
||
result.extend(response.json["result"]) | ||
# This is a header only for Table API. | ||
# When using this client for generic api, the header is not present anymore | ||
# and we need to find a new method to break from the loop | ||
# It can be removed from Table API but for it's better to keep it for now. | ||
if response.headers['x-total-count']: | ||
total = int(response.headers["x-total-count"]) | ||
else: | ||
if len(response.json['result']) == 0: | ||
break | ||
|
||
offset += self.batch_size | ||
|
||
return result | ||
|
||
def get(self, api_path, query, must_exist=False): | ||
records = self.list(api_path, query) | ||
|
||
if len(records) > 1: | ||
raise errors.ServiceNowError( | ||
"{0} {1} records match the {2} query.".format( | ||
len(records), api_path, query | ||
) | ||
) | ||
|
||
if must_exist and not records: | ||
raise errors.ServiceNowError( | ||
"No {0} records match the {1} query.".format(api_path, query) | ||
) | ||
|
||
return records[0] if records else None | ||
|
||
def create(self, api_path, payload, query=None): | ||
return self.client.post( | ||
api_path, payload, query=self._sanitize_query(query) | ||
).json["result"] | ||
|
||
def update(self, api_path, sys_id, payload, query=None): | ||
return self.client.patch( | ||
"/".join((api_path.rstrip("/"), sys_id)), | ||
payload, | ||
query=self._sanitize_query(query), | ||
).json["result"] | ||
|
||
def delete(self, api_path, sys_id): | ||
self.client.delete("/".join((api_path.rstrip("/"), sys_id))) | ||
|
||
def _sanitize_query(self, query): | ||
query = query or dict() | ||
query.setdefault("sysparm_exclude_reference_link", "true") | ||
return query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.