-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·113 lines (94 loc) · 3.2 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
import argparse
# Local module, see sql.py file
import sql
'''
API for racktables database.
'''
def print_array(array):
'''Print array in column'''
try:
for i in array:
print(i)
except:
print(array)
def check_params(args):
'''
Check parameters compatibility.
'''
if (args.command in ['add', 'del']
and args.arg in ['FQDN', 'OS', 'SERVER', 'RAM']):
print("ERROR: `{}` and `{}` incompatible."
.format(args.command, args.arg))
exit(20)
if (args.command == 'set'
and args.arg in ['STORAGE', 'SERVER', 'CPU']):
print("ERROR: `{}` and `{}` arguments are incompatible."
.format(args.command, args.arg))
exit(20)
if (args.command == 'get'
and args.value is not None):
print("ERROR: `{}` argument and -v (--value) option are incompatible."
.format(args.command))
exit(20)
if (args.command in ['add', 'del', 'set']
and args.value is None):
print("ERROR: `{} {}` requires -v (--value) option."
.format(args.command, args.arg))
exit(20)
if (args.command in ['add', 'del', 'set']
and args.server is None):
print("ERROR: `{} {}` requires -s (--server) option."
.format(args.command, args.arg))
exit(20)
if (args.command == 'get'
and args.arg in ['RAM', 'FQDN']
and args.server is None):
print("ERROR: `{} {}` requires -s (--server) option."
.format(args.command, args.arg))
exit(20)
def main():
parser = argparse.ArgumentParser(
description="A CLI API for racktables database.")
parser.add_argument('command', help="Command.",
choices=['get', 'add', 'del', 'set'], type=str.lower)
parser.add_argument('arg', help="Argument for the command.",
choices=[
'STORAGE',
'CPU',
'FQDN',
'OS',
'SERVER',
'RAM',
'STATUS',
'SLA'
], type=str.upper
)
parser.add_argument('-s', '--server', help="Server for the command.")
parser.add_argument('-v', '--value', help="Value for the command.")
try:
args = parser.parse_args()
except SystemExit as e:
exit(20)
check_params(args)
if (args.arg in
['STORAGE', 'CPU', 'OS', 'STATUS', 'SLA']
and args.server is None):
print_array(sql.get_available_values(args.arg))
if args.command == 'get':
if args.arg == 'SERVER':
print_array(sql.get_servers())
if args.arg in ['STORAGE', 'CPU', 'OS', 'STATUS',
'SLA', 'RAM', 'FQDN']:
print_array(sql.get_attr_values(args.arg, args.server))
if args.command == 'add':
if args.arg in ['STORAGE', 'CPU']:
sql.add_attr_value(args.arg, args.server, args.value)
if args.command == 'del':
if args.arg in ['STORAGE', 'CPU']:
sql.del_attr_value(args.arg, args.server, args.value)
if args.command == 'set':
if args.arg in ['OS', 'STATUS', 'SLA', 'RAM', 'FQDN']:
sql.set_attr_value(args.arg, args.server, args.value)
if __name__ == "__main__":
main()