-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnow.py
executable file
·91 lines (63 loc) · 2.5 KB
/
snow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3.7
import argparse
#import json
import pysnow
import snow_cli
import sys
import yaml
from pprint import pprint
from types import SimpleNamespace
# validate python version, 3.7+ required
if not (sys.version_info[0] == 3 and sys.version_info[1] == 7):
print('\n\tYou need python 3.7!\n')
exit(1)
apis = SimpleNamespace(
app = '/table/cmdb_ci_appl',
change = '/table/change_request',
incident = '/table/incident'
)
def parse_args():
"""The CLI arguments."""
p = argparse.ArgumentParser()
s = p.add_subparsers(dest='resource', required=True)
# Global
p.add_argument('-c', '--config', required=False, default='./snow.yml',
help='The config file location')
app = s.add_parser('app', help='Appication resource')
app_s = app.add_subparsers(dest='action', required=True)
ch = s.add_parser('change', help='Change resource')
ch_s = ch.add_subparsers(dest='action', required=True)
inc = s.add_parser('incident', help='Incident resource')
inc_s = inc.add_subparsers(dest='action', required=True)
# App Subparsers
app_get = app_s.add_parser('get', help='Get an application configuration item')
app_get_id = app_get.add_mutually_exclusive_group(required=True)
app_get_id.add_argument('-n', '--name', help='Get the resource by name search')
app_get_id.add_argument('-i', '--id', help='Get the resource by id')
# Change Subparsers
ch_approved = ch_s.add_parser('is-approved?',
help='Check if a change is in approved state')
ch_approved.add_argument('-n', '--number', required=True,
help='The change record number (CHGXXXXXXX)')
# Incident Subparsers
return p.parse_args()
def load_config(_file):
"""Load a YAML config file and return a dict."""
with open(_file) as f:
return yaml.safe_load(f)
def new_conn(config):
"""Create a ServiceNow connection and return the object."""
return pysnow.Client(instance=config['host'], user=config['user'], password=config['pass'])
def resource_handler(sn_conn, api):
"""Return an API handler usin the API map."""
return sn_conn.resource(api_path=apis.__dict__[api])
def main():
args = parse_args()
config = load_config(args.config)
sn = new_conn(config)
res_h = resource_handler(sn, args.resource)
#for r in snow_cli.handler(res_h, args).all():
# print(json.dumps(r))
exit(True != snow_cli.handler(res_h, args))
if __name__ == '__main__':
main()