HomeGuidesAPI Reference
GuidesAPI ReferenceGitHubAirheads Developer CommunityLog In
Guides

ServiceNow

Ticket Creation Using ServiceNow

This tutorial will teach you to create a ServiceNow ticket using NAE.

Instructions for using the ServiceNow API can be found here.

To prepare, we import the necessary libraries that are not already part of NAE.
For example:

from requests import get, post, exceptions
from json import dumps

Adding the parameters

Step one is to add the required information to the ParameterDefinitions section of the script.
These parameters are used by the agent to connect to the ServiceNow instance and create the ticket.
This includes fields such as the urgency, severity, and description of the event.
For example:

'urgency': {
        'Name': 'Urgency of the config change event',
        'Description': 'Specify the urgency of the configuration change event, '
                       'that is provided in the ServiceNow ticket. Options are '
                       '1 - High, 2 - Medium and 3 - Low.',
        'Type': 'integer',
        'Default': 3
    },
    'severity': {
        'Name': 'Severity of the config change event',
        'Description': 'Specify the severity of the configuration change event, '
                       'that is provided in the ServiceNow ticket. Options are '
                       '1 - High, 2 - Medium and 3 - Low.',
        'Type': 'integer',
        'Default': 3
    },
    'username': {
        'Name': 'Username of the ServiceNow account instance',
        'Description': 'Enter the username of the ServiceNow account instance '
                       'used to create the ticket for configuration change event.',
        'Type': 'string',
        'Default': 'admin'
    },
    'password': {
        'Name': 'Password of the ServiceNow account instance',
        'Description': 'Enter the password of the ServiceNow account instance '
                       'used to create the ticket for configuration change event.',
        'Type': 'string',
        'Default': 'pwd',
        'Encrypted': True
    },
    'domain_name': {
        'Name': 'Domain name of the ServiceNow account instance',
        'Description': 'Enter the domain name of the ServiceNow account instance '
                       'used to create the ticket for configuration change event.',
        'Type': 'string',
        'Default': 'devxxxxx'
    },
    'short_description': {
        'Name': 'Short description for the configuration change event',
        'Description': 'Enter the short description for the configuration change '
                       'event, that will be described in the ServiceNow ticket.',
        'Type': 'string',
        'Default': 'NAE detected a config change event'
    },
    'web_proxy': {
        'Name': 'Web proxy IP address',
        'Description': 'Enter the IP address for web proxy.',
        'Type': 'string',
        'Default': ''
    }

Building the message
Secondly, we'll build the message. In this case, it's the JSON version of the running-configuration.
For example:

r = rest_get('/rest/v1/config/diff/%s/running-config' % base_checkpoint)
r.raise_for_status()
description = r.json()['output']

Defining the API call function

Finally, we make the request to the ServiceNow API endpoint, as documented in the link above.
For example:

📘

Proxy

This particular example assumes that the NAE script will run behind a firewall and would need to use a proxy.

def post_ticket(self, short_description, description):
  user = str(self.params['username'])
  pwd = str(self.params['password'])
  domain_name = str(
    self.params['domain_name'])

  if str(self.params['web_proxy']) == '':
    proxies = {}
  else:
    proxies = {
        'http': 'http://' + str(self.params['web_proxy']) + ':8080',
        'https': 'https://' + str(self.params['web_proxy']) + ':8080'
    }

  url = 'https://' + domain_name + \
        '.service-now.com/api/now/table/incident'

  urgency = str(self.params['urgency'])
  severity = str(self.params['severity'])

  headers = {
        "content-type": "application/json", "Accept": "application/json"}
  payload = {
        'description': description,
        'severity': severity,
        'urgency': urgency,
        'short_description': short_description,
        'category': 'network'}
  try:
        response = post(
                        url,
                        auth=(user,pwd),
                        headers=headers,
                        proxies=proxies,
                        data=dumps(payload)
                       )
        self.logger.info(
                "Agent post: Status code: {}".format(str(response.status_code)))
  except exceptions.RequestException as e:
        self.logger.error(
                "Could not create configuration change ticket in ServiceNow. Error:    {}".format(str(e)))

And that's it!