-
Notifications
You must be signed in to change notification settings - Fork 5
/
ucs_config.py
84 lines (64 loc) · 2.45 KB
/
ucs_config.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
"""
ucs_config.py
Purpose:
Manage UCS Objects from configuration file, using dynamic module loading.
All Configuration settings and module requirements come from a JSON/YAML
file.
Configure/Manage UCS Manager and Cisco IMC from JSON/YAML
Author:
John McDonough ([email protected])
Cisco Systems, Inc.
"""
from importlib import import_module
import json
import logging
import os
import sys
import yaml
# pylint: disable=invalid-name,redefined-outer-name
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
def traverse(managed_object, mo=''):
"""
Traverse the configuration this function will be called
recursively until the end of the configuration is reached
"""
logging.info(managed_object['class'])
logging.debug(managed_object['module'])
logging.debug(managed_object['properties'])
mo_module = import_module(managed_object['module'])
mo_class = getattr(mo_module, managed_object['class'])
if 'parent_mo_or_dn' not in managed_object['properties']:
managed_object['properties']['parent_mo_or_dn'] = mo
mo = mo_class(**managed_object['properties'])
logging.debug(mo)
HANDLE.add_mo(mo, modify_present=True)
if 'children' in managed_object:
for child in managed_object['children']:
traverse(child, mo)
if __name__ == '__main__':
FILENAME = os.path.join(sys.path[0], sys.argv[1])
logging.info('Reading config file: %s', FILENAME)
try:
with open(FILENAME, 'r') as file:
if FILENAME.endswith('.json'):
config = json.load(file)
elif FILENAME.endswith('.yml'):
config = yaml.load(file, Loader=yaml.FullLoader)
else:
logging.info(
'Unsupported file extension for configuration file: %s'
, FILENAME
)
except IOError as io_error:
sys.exit(io_error)
mo_module = import_module(config['connection']['module'])
obj_class = config['connection']['class']
mo_class = getattr(mo_module, obj_class)
HANDLE = mo_class(**config['connection']['properties'])
HANDLE.login()
for managed_object in config['objects']:
traverse(managed_object)
if config['connection']['commit-buffer']:
HANDLE.commit()
HANDLE.logout()