Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api query params #225

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/api_query_params.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- Added query_params to api module.
juremedvesek marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions plugins/module_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions plugins/module_utils/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 26 additions & 0 deletions plugins/modules/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

author:
- Tjaž Eržen (@tjazsch)
- Jure Medvešek (@juremedvesek)

short_description: Manage ServiceNow POST, PATCH and DELETE requests
description:
Expand Down Expand Up @@ -43,6 +44,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.
Expand Down Expand Up @@ -123,6 +129,22 @@
short_description: demo-description2
register: result

- name: create user (object with encrypted 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: "[email protected]"
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
Expand Down Expand Up @@ -252,6 +274,7 @@
FIELD_SYS_ID,
FIELD_DATA,
FIELD_TEMPLATE,
FIELD_QUERY_PARAMS,
)


Expand All @@ -265,6 +288,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)

Expand All @@ -276,6 +300,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)

Expand Down Expand Up @@ -315,6 +340,7 @@ def main():
ACTION_DELETE, # delete
],
),
query_params=dict(type="dict", default=dict()),
data=dict(type="dict", default=dict()),
template=dict(
type="str",
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/targets/api/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]"
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
Expand Down