From 71b13afcc853b0076395df37632c55b6d69ee80a Mon Sep 17 00:00:00 2001 From: Jure Medvesek Date: Wed, 5 Oct 2022 12:42:38 +0200 Subject: [PATCH 1/5] Added query params to API --- plugins/module_utils/api.py | 1 + plugins/module_utils/table.py | 10 +++++---- plugins/modules/api.py | 20 +++++++++++++++++ tests/integration/targets/api/tasks/main.yml | 23 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index 514f90d7..fcbce085 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -31,6 +31,7 @@ FIELD_DATA = "data" FIELD_TEMPLATE = "template" FIELD_DATA_RENDERED = "data_rendered" +FIELD_QUERY_PARAMS = "query_params" POSSIBLE_FILTER_PARAMETERS = [ FIELD_QUERY_NAME, diff --git a/plugins/module_utils/table.py b/plugins/module_utils/table.py index 95910f29..411d5c34 100644 --- a/plugins/module_utils/table.py +++ b/plugins/module_utils/table.py @@ -63,20 +63,22 @@ def get_record(self, table, query, must_exist=False): return records[0] if records else None - def create_record(self, table, payload, check_mode): + def create_record(self, table, payload, check_mode, query=None): if check_mode: # Approximate the result using the payload. return payload - return self.client.post(_path(table), payload, query=_query()).json["result"] + return self.client.post(_path(table), payload, query=_query(query)).json[ + "result" + ] - def update_record(self, table, record, payload, check_mode): + def update_record(self, table, record, payload, check_mode, query=None): if check_mode: # Approximate the result by manually patching the existing state. return dict(record, **payload) return self.client.patch( - _path(table, record["sys_id"]), payload, query=_query() + _path(table, record["sys_id"]), payload, query=_query(query) ).json["result"] def delete_record(self, table, record, check_mode): diff --git a/plugins/modules/api.py b/plugins/modules/api.py index 2fae4c24..91b1a942 100644 --- a/plugins/modules/api.py +++ b/plugins/modules/api.py @@ -123,6 +123,22 @@ short_description: demo-description2 register: result +- name: create user (object with encrzped fields) + servicenow.itsm.api: + resource: sys_user + action: post + query_params: + sysparm_input_display_value: true + data: + user_name: "demo_username" + user_password: "demo_password" + first_name: "first_name" + last_name: Demouser + department: IT + email: "demo_username@example.com" + title: Demo user + register: user + - name: Create a record in sc_req_item with column values set in template, located in Ansible controller file system servicenow.itsm.api: resource: sc_req_item @@ -252,6 +268,7 @@ FIELD_SYS_ID, FIELD_DATA, FIELD_TEMPLATE, + FIELD_QUERY_PARAMS, ) @@ -265,6 +282,7 @@ def update_resource(module, table_client): record=record_old, payload=module.params.get(FIELD_DATA, dict()), check_mode=module.check_mode, + query=module.params.get(FIELD_QUERY_PARAMS, dict()), ) return True, record_new, dict(before=record_old, after=record_new) @@ -276,6 +294,7 @@ def create_resource(module, table_client): table=table_name(module), payload=module.params.get(FIELD_DATA, dict()), check_mode=module.check_mode, + query=module.params.get(FIELD_QUERY_PARAMS, dict()), ) return True, new, dict(before=None, after=new) @@ -315,6 +334,7 @@ def main(): ACTION_DELETE, # delete ], ), + query_params=dict(type="dict", default=dict()), data=dict(type="dict", default=dict()), template=dict( type="str", diff --git a/tests/integration/targets/api/tasks/main.yml b/tests/integration/targets/api/tasks/main.yml index 6987f8c2..8d22b120 100644 --- a/tests/integration/targets/api/tasks/main.yml +++ b/tests/integration/targets/api/tasks/main.yml @@ -5,6 +5,29 @@ SN_PASSWORD: "{{ sn_password }}" block: + - name: create user (object with encryped fields) + servicenow.itsm.api: + resource: sys_user + action: post + query_params: + sysparm_input_display_value: true + data: + user_name: "demo_username" + user_password: "demo_password" + first_name: "first_name" + last_name: Demouser + department: IT + email: "demo_username@example.com" + title: Demo user + register: user + + - name: Delete user + servicenow.itsm.api: + resource: sys_user + action: delete + sys_id: "{{ user.record.sys_id }}" + register: deleted_user + - name: Retrieve all incidents servicenow.itsm.api_info: resource: incident From 27518815b4d9fb482afab702e74a68dffcb4ae5c Mon Sep 17 00:00:00 2001 From: Jure Medvesek Date: Wed, 5 Oct 2022 13:16:35 +0200 Subject: [PATCH 2/5] Added documentation --- plugins/modules/api.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/modules/api.py b/plugins/modules/api.py index 91b1a942..3666bba5 100644 --- a/plugins/modules/api.py +++ b/plugins/modules/api.py @@ -43,6 +43,11 @@ - post - patch - delete + query_params: + version_added: "2.1.0" + description: + - Query parameters that may be used on POST or PATCH request. + type: dict data: description: - The data that we want to update or create the resource with. From 10ba84be9f0a4429346a8ff22f237932fddaff74 Mon Sep 17 00:00:00 2001 From: juremedvesek Date: Fri, 7 Oct 2022 07:14:22 +0200 Subject: [PATCH 3/5] Update plugins/modules/api.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Uroš Paščinski <95997469+uscinski@users.noreply.github.com> --- plugins/modules/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/api.py b/plugins/modules/api.py index 3666bba5..5c1763a8 100644 --- a/plugins/modules/api.py +++ b/plugins/modules/api.py @@ -128,7 +128,7 @@ short_description: demo-description2 register: result -- name: create user (object with encrzped fields) +- name: create user (object with encrypted fields) servicenow.itsm.api: resource: sys_user action: post From 330f36ef092ee6e39fe820c01bfb5045e6278570 Mon Sep 17 00:00:00 2001 From: Jure Medvesek Date: Fri, 7 Oct 2022 07:22:52 +0200 Subject: [PATCH 4/5] Added changelogs --- changelogs/fragments/api_query_params.yml | 2 ++ plugins/modules/api.py | 1 + 2 files changed, 3 insertions(+) create mode 100644 changelogs/fragments/api_query_params.yml diff --git a/changelogs/fragments/api_query_params.yml b/changelogs/fragments/api_query_params.yml new file mode 100644 index 00000000..3e8dba77 --- /dev/null +++ b/changelogs/fragments/api_query_params.yml @@ -0,0 +1,2 @@ +minor_changes: + - Added query_params to api module. \ No newline at end of file diff --git a/plugins/modules/api.py b/plugins/modules/api.py index 3666bba5..f056c028 100644 --- a/plugins/modules/api.py +++ b/plugins/modules/api.py @@ -13,6 +13,7 @@ author: - Tjaž Eržen (@tjazsch) + - Jure Medvešek (@juremedvesek) short_description: Manage ServiceNow POST, PATCH and DELETE requests description: From ad7d1cc6e8deb1b8a220fae831b9f114b80b35bb Mon Sep 17 00:00:00 2001 From: juremedvesek Date: Fri, 7 Oct 2022 07:41:43 +0200 Subject: [PATCH 5/5] Update changelogs/fragments/api_query_params.yml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Uroš Paščinski <95997469+uscinski@users.noreply.github.com> --- changelogs/fragments/api_query_params.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/api_query_params.yml b/changelogs/fragments/api_query_params.yml index 3e8dba77..e4eea9f1 100644 --- a/changelogs/fragments/api_query_params.yml +++ b/changelogs/fragments/api_query_params.yml @@ -1,2 +1,2 @@ minor_changes: - - Added query_params to api module. \ No newline at end of file + - api - Added parameter query_params to api module (https://github.com/ansible-collections/servicenow.itsm/pull/225). \ No newline at end of file