-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_config.py
273 lines (236 loc) · 11.8 KB
/
app_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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2023 Consoli Solutions, LLC. All rights reserved.
#
# NOT BROADCOM SUPPORTED
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may also obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
:mod:`app_config.py` - Examples on how to modify chassis configuration parameters.
**Description**
Illustrates how to read and change parameters available in the
"running/brocade-chassis/management-interface-configuration". Specifically:
* Enable/disable the Rest interface
* Enable/disable HTTPS
* Enable/disable keep alive
Version Control::
+-----------+---------------+-----------------------------------------------------------------------------------+
| Version | Last Edit | Description |
+===========+===============+===================================================================================+
| 4.0.0 | 04 Aug 2023 | Re-Launch |
+-----------+---------------+-----------------------------------------------------------------------------------+
"""
__author__ = 'Jack Consoli'
__copyright__ = 'Copyright 2023 Consoli Solutions, LLC'
__date__ = '04 August 2023'
__license__ = 'Apache License, Version 2.0'
__email__ = '[email protected]'
__maintainer__ = 'Jack Consoli'
__status__ = 'Released'
__version__ = '4.0.0'
import argparse
import pprint
import brcdapi.brcdapi_rest as brcdapi_rest
import brcdapi.fos_auth as fos_auth
import brcdapi.log as brcdapi_log
import brcdapi.util as brcdapi_util
_DOC_STRING = False # Should always be False. Prohibits any actual I/O. Only useful for building documentation
_DEBUG = False # When True, use _DEBUG_xxx below instead of parameters passed from the command line.
_DEBUG_ip = 'xx.xxx.x.69'
_DEBUG_id = 'admin'
_DEBUG_pw = 'password'
_DEBUG_s = 'self' # Use None or 'none' for HTTP. Use the certificate if HTTPS and not self signed
_DEBUG_rest_en = False
_DEBUG_rest_dis = False
_DEBUG_https_en = False
_DEBUG_https_dis = False
_DEBUG_max_rest = None
_DEBUG_ka_en = True
_DEBUG_ka_dis = False
_DEBUG_d = False # When True, all content and responses are formatted and printed (pprint).
_DEBUG_log = '_logs'
_DEBUG_nl = False
def _get_input():
"""Parses the module load command line
:return ec: Error code. 0: OK, -1: Errors encountered
:rtype ec: int
:return ip: Switch IP address
:rtype ip: str
:return id: User ID
:rtype id: str
:return pw: User password
:rtype ip: str
:return sec: Secure method. None for HTTP, otherwise the certificate or 'self' if self signed
:rtype sec: str, None
:return content: Content for "running/brocade-chassis/management-interface-configuration".
:rtype content: dict
"""
global _DEBUG_ip, _DEBUG_id, _DEBUG_pw, _DEBUG_s, _DEBUG_rest_en, _DEBUG_rest_dis
global _DEBUG_https_en, _DEBUG_https_dis, _DEBUG_max_rest, _DEBUG_ka_en, _DEBUG_ka_en
global _DEBUG_d, _DEBUG_log, _DEBUG_nl
ec = 0
if _DEBUG:
args_ip, args_id, args_pw, args_s = _DEBUG_ip, _DEBUG_id, _DEBUG_pw, 'none' if _DEBUG_s is None else _DEBUG_s
args_rest_en, args_rest_dis, args_https_en, args_https_dis = \
_DEBUG_rest_en, _DEBUG_rest_dis, _DEBUG_https_en, _DEBUG_https_dis
args_max_rest, args_ka_en, args_ka_dis = \
_DEBUG_max_rest, _DEBUG_ka_en, _DEBUG_ka_dis
args_d, args_log, args_nl = _DEBUG_d, _DEBUG_log, _DEBUG_nl
else:
buf = 'Useful as a programming example only on how to read and make chassis configuration changes via the '\
'"running/brocade-chassis/management-interface-configuration" URI. If the only input is the login '\
'credentials, the parameters are displayed and no other action taken.'
parser = argparse.ArgumentParser(description=buf)
parser.add_argument('-ip', help='(Required) IP address', required=True)
parser.add_argument('-id', help='(Required) User ID', required=True)
parser.add_argument('-pw', help='(Required) Password', required=True)
parser.add_argument('-s', help='(Optional) Default is HTTP. CA or "self" for HTTPS mode.', required=False)
parser.add_argument('-rest_en', help='(Optional) No parameters. Enables the Rest interface',
action='store_true', required=False)
parser.add_argument('-rest_dis', help='(Optional) No parameters. Disables the Rest interface',
action='store_true', required=False)
parser.add_argument('-https_en', help='(Optional) No parameters. Enable HTTPS', action='store_true',
required=False)
parser.add_argument('-https_dis', help='(Optional) No parameters. Disable HTTPS', action='store_true',
required=False)
parser.add_argument('-max_rest',
help='(Optional) Set the maximum number of REST sessions. Valid options are 1-10',
required=False)
parser.add_argument('-ka_en', help='(Optional) No parameters. Enable keep-alive', action='store_true',
required=False)
parser.add_argument('-ka_dis', help='(Optional) No parameters. Disable keep-alive', action='store_true',
required=False)
buf = '(Optional) Enable debug logging. Prints the formatted data structures (pprint) to the log and console.'
parser.add_argument('-d', help=buf, action='store_true', required=False)
buf = '(Optional) Directory where log file is to be created. Default is to use the current directory. The log' \
' file name will always be "Log_xxxx" where xxxx is a time and date stamp.'
parser.add_argument('-log', help=buf, required=False, )
buf = '(Optional) No parameters. When set, a log file is not created. The default is to create a log file.'
parser.add_argument('-nl', help=buf, action='store_true', required=False)
args = parser.parse_args()
args_ip, args_id, args_pw, args_s = args.ip, args.id, args.pw, 'none' if args.s is None else args.s
args_rest_en, args_rest_dis, args_https_en, args_https_dis = \
args.rest_en, args.rest_dis, args.https_en, args.https_dis
args_max_rest, args_ka_en, args_ka_dis = args.max_rest, args.ka_en, args.ka_dis
args_d, args_log, args_nl = args.d, args.log, args.nl
# Set up the log file
if not args_nl:
brcdapi_log.open_log(args_log)
if args_d: # Verbose debug
brcdapi_rest.verbose_debug = True
rd = {
'rest-enabled': True if args_rest_en else False if args_rest_dis else None,
'https-protocol-enabled': True if args_https_en else False if args_https_dis else None,
'max-rest-sessions': args_max_rest,
'https-keep-alive-enabled': True if args_ka_en else False if args_ka_dis else None
}
# User feedback
ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
ml.append('IP, -ip: ' + brcdapi_util.mask_ip_addr(args_ip, True))
ml.append('ID, -id: ' + args_id)
ml.append('Security, -s: ' + args_s)
ml.append('Enable Rest, -rest_en: ' + str(args_rest_en))
ml.append('Disable Rest, -rest_dis: ' + str(args_rest_dis))
ml.append('Enable HTTPS, -https_en: ' + str(args_https_en))
ml.append('Disable HTTPS, -https_dis: ' + str(args_https_dis))
ml.append('Enable keep-alive, -ka_en: ' + str(args_ka_en))
ml.append('Disable keep-alive, -ka_dis: ' + str(args_ka_dis))
ml.append('Max Rest sessions, -max_rest: ' + str(args_max_rest))
# Validate the input and set up the return dictionary
if args_rest_en and args_rest_dis:
ml.append('-rest_en and -rest_dis are mutually exclusive.')
ec = -1
if args_https_en and args_https_dis:
ml.append('-https_en and -https_dis are mutually exclusive.')
ec = -1
if args_ka_en and args_ka_dis:
ml.append('-ka_en and -ka_dis are mutually exclusive.')
ec = -1
if len(rd) == 0:
ml.extend(['', 'No changes'])
brcdapi_log.log(ml, True)
return ec, args_ip, args_id, args_pw, args_s, rd
def pseudo_main():
"""Basically the main().
:return: Exit code
:rtype: int
"""
global _DEBUG
# Get and validate command line input
ec, ip, user_id, pw, sec, input_d = _get_input()
if ec != 0:
return ec
# Login
brcdapi_log.log('Attempting login', True)
session = brcdapi_rest.login(user_id, pw, ip, sec)
if fos_auth.is_error(session):
brcdapi_log.log(fos_auth.formatted_error_msg(session), True)
return -1
brcdapi_log.log('Login succeeded', True)
ec = 0
uri = 'running/brocade-chassis/management-interface-configuration'
try: # try/except so that no matter what happens, the logout code gets executed.
# Display initial read (GET) of parameters
brcdapi_log.log(['', 'Before Changes:'], True)
obj = brcdapi_rest.get_request(session, uri)
if fos_auth.is_error(obj):
brcdapi_log.log(fos_auth.formatted_error_msg(obj), True)
ec = -1
else:
brcdapi_log.log(pprint.pformat(obj), True)
if ec == 0:
# Make the changes
content_d = dict()
for k, v in input_d.items():
if v is not None:
content_d.update({k: v})
if len(content_d) == 0:
brcdapi_log.log('No changes to make.', True)
else:
obj = brcdapi_rest.send_request(session,
uri,
'PATCH',
{'management-interface-configuration': content_d})
if fos_auth.is_error(obj):
brcdapi_log.log(fos_auth.formatted_error_msg(obj), True)
ec = -1
else:
# Display read (GET) after changing parameters
brcdapi_log.log(['', 'After Changes:'], True)
obj = brcdapi_rest.get_request(session, uri)
if fos_auth.is_error(obj):
brcdapi_log.log(fos_auth.formatted_error_msg(obj), True)
ec = -1
else:
brcdapi_log.log(pprint.pformat(obj), True)
except BaseException as e:
brcdapi_log.exception('Programming error encountered. Exception is: ' + str(e), True)
ec = -1
# Logout
obj = brcdapi_rest.logout(session)
if fos_auth.is_error(obj):
brcdapi_log.log('Logout failed', True)
ec = -1
else:
brcdapi_log.log('Logout succeeded', True)
return ec
###################################################################
#
# Main Entry Point
#
###################################################################
if _DOC_STRING:
print('_DOC_STRING is True. No processing')
exit(0)
_ec = pseudo_main()
brcdapi_log.close_log('Processing complete. Exit status: ' + str(_ec))
exit(_ec)