-
Notifications
You must be signed in to change notification settings - Fork 73
/
testisam_cmd.py
executable file
·156 lines (136 loc) · 5.02 KB
/
testisam_cmd.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
"""
Usage: testisam_cmd.py
testisam_cmd.py [--hostname=ISAM_LMI --username=ISAM_ADMIN --password=ISAM_ADMIN_PASSWORD
--lmi_port=443 --method=ibm.isam.appliance.get --method_options="name=test" --commit]
Options:
--hostname=hostname Hostname (eg. isamlmi.local.com)
--username=admin The LMI administration user. Defaults to admin@local
--password=password The LMI administration user's password. Defaults to admin
--lmi_port=443 The lmi port, defaults to 443
--method=ibmsecurity.isam.method The method to call
--method_options="name=name" String of key-value pairs "name=test,key2=key2"
--commit Perform commit of the changes. Not required if you do a GET
-h --help Show this screen.
"""
import logging.config
import pprint
from ibmsecurity.appliance.isamappliance import ISAMAppliance
from ibmsecurity.user.applianceuser import ApplianceUser
import pkgutil
import importlib
import ibmsecurity
from docopt import docopt
def import_submodules(package, recursive=True):
"""
Import all submodules of a module, recursively, including subpackages
:param package: package (name or actual module)
:type package: str | module
:rtype: dict[str, types.ModuleType]
"""
if isinstance(package, str):
package = importlib.import_module(package)
results = {}
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
full_name = package.__name__ + "." + name
results[full_name] = importlib.import_module(full_name)
if recursive and is_pkg:
results.update(import_submodules(full_name))
return results
# Import all packages within ibmsecurity - recursively
# Note: Advisable to replace this code with specific imports for production code
import_submodules(ibmsecurity)
# Setup logging to send to stdout, format and set log level
# logging.getLogger(__name__).addHandler(logging.NullHandler())
logging.basicConfig()
# Valid values are 'DEBUG', 'INFO', 'ERROR', 'CRITICAL'
logLevel = "DEBUG"
DEFAULT_LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {
"format": "[%(asctime)s] [PID:%(process)d TID:%(thread)d] [%(levelname)s] [%(name)s] [%(funcName)s():%(lineno)s] %(message)s"
},
},
"handlers": {
"default": {
"level": logLevel,
"formatter": "standard",
"class": "logging.StreamHandler",
},
},
"loggers": {
"": {"level": logLevel, "handlers": ["default"], "propagate": True},
"requests.packages.urllib3.connectionpool": {
"level": "ERROR",
"handlers": ["default"],
"propagate": True,
},
},
}
logging.config.dictConfig(DEFAULT_LOGGING)
# Function to pretty print JSON data
def p(jdata):
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(jdata)
def load_args(__doc__):
args = docopt(__doc__)
method = None
_options = "isamAppliance=isam_server"
commit = False
if args["--commit"]:
commit = True
if args["--hostname"]:
hostname = args["--hostname"]
else:
hostname = "127.0.0.1"
if args["--username"]:
username = args["--username"]
else:
username = "admin@local"
if args["--password"]:
password = args["--password"]
else:
password = "admin"
if args["--lmi_port"]:
lmi_port = args["--lmi_port"]
else:
lmi_port = "443"
if args["--method"]:
method = args["--method"]
if args["--method_options"]:
_newoptions = args["--method_options"]
# split in key/value pairs
d = dict(kv.split("=", 1) for kv in _newoptions.split(","))
for k, v in d.items():
logging.debug(f"VALUE: {v}")
if "json." in v:
_options = _options + "," + k + "=" + eval(v)
else:
_options = _options + "," + k + "='" + str(v) + "'"
logging.debug(_options)
return commit, hostname, username, password, lmi_port, method, _options
if __name__ == "__main__":
"""
This test program should not execute when imported, which would otherwise
cause problems when generating the documentation.
"""
commit, hostname, username, password, lmi_port, isam_module, options = load_args(
__doc__
)
# Create a user credential for ISAM appliance
u = ApplianceUser(username=username, password=password)
# Create an ISAM appliance with above credential
isam_server = ISAMAppliance(hostname=hostname, user=u, lmi_port=lmi_port)
# Run the method with options
module_name, method_name = isam_module.rsplit(".", 1)
mod = importlib.import_module(module_name)
func_ptr = getattr(mod, method_name) # Convert action to actual function pointer
logging.debug(func_ptr)
func_call = "func_ptr(" + options + ")"
# Execute requested 'action'
p(eval(func_call))
# Commit or Deploy the changes
if commit:
p(ibmsecurity.isam.appliance.commit(isamAppliance=isam_server))