Source code for saltext.servicenow.modules.servicenow
"""Module for execution of ServiceNow CI (configuration items).. versionadded:: 2016.11.0:depends: servicenow_rest python module:configuration: Configure this module by specifying the name of a configuration profile in the minion config, minion pillar, or master config. The module will use the 'servicenow' key by default, if defined. For example: .. code-block:: yaml servicenow: instance_name: '' username: '' password: ''"""importloggingHAS_LIBS=Falsetry:fromservicenow_rest.apiimportClientHAS_LIBS=TrueexceptImportError:passlog=logging.getLogger(__name__)__virtualname__="servicenow"SERVICE_NAME="servicenow"
[docs]def__virtual__():""" Only load this module if servicenow is installed on this minion. """ifHAS_LIBS:return__virtualname__return(False,"The servicenow execution module failed to load: ""requires servicenow_rest python library to be installed.",)
[docs]defset_change_request_state(change_id,state="approved"):""" Set the approval state of a change request/record :param change_id: The ID of the change request, e.g. CHG123545 :type change_id: ``str`` :param state: The target state, e.g. approved :type state: ``str`` CLI Example: .. code-block:: bash salt myminion servicenow.set_change_request_state CHG000123 declined salt myminion servicenow.set_change_request_state CHG000123 approved """client=_get_client()client.table="change_request"# Get the change record firstrecord=client.get({"number":change_id})ifnotrecord:log.error("Failed to fetch change record, maybe it does not exist?")returnFalse# Use the sys_id as the unique system recordsys_id=record[0]["sys_id"]response=client.update({"approval":state},sys_id)returnresponse
[docs]defdelete_record(table,sys_id):""" Delete an existing record :param table: The table name, e.g. sys_user :type table: ``str`` :param sys_id: The unique ID of the record :type sys_id: ``str`` CLI Example: .. code-block:: bash salt myminion servicenow.delete_record sys_computer 2134566 """client=_get_client()client.table=tableresponse=client.delete(sys_id)returnresponse
[docs]defnon_structured_query(table,query=None,**kwargs):""" Run a non-structed (not a dict) query on a servicenow table. :param table: The table name, e.g. sys_user :type table: ``str`` :param query: The query to run (or use keyword arguments to filter data) :type query: ``str`` CLI Example: .. code-block:: bash salt myminion servicenow.non_structured_query sys_computer 'role=web' salt myminion servicenow.non_structured_query sys_computer role=web type=computer """client=_get_client()client.table=tableifqueryisNone:# try and assemble a query by keywordquery_parts=[]forkey,valueinkwargs.items():query_parts.append(f"{key}={value}")query="^".join(query_parts)query=str(query)response=client.get(query)returnresponse
[docs]defupdate_record_field(table,sys_id,field,value):""" Update the value of a record's field in a servicenow table :param table: The table name, e.g. sys_user :type table: ``str`` :param sys_id: The unique ID of the record :type sys_id: ``str`` :param field: The new value :type field: ``str`` :param value: The new value :type value: ``str`` CLI Example: .. code-block:: bash salt myminion servicenow.update_record_field sys_user 2348234 first_name jimmy """client=_get_client()client.table=tableresponse=client.update({field:value},sys_id)returnresponse