Skip to content

Commit 30e6279

Browse files
committed
Feedback review changes
- Split read node into 2 functions for errors and normal records - Also enforces typing on methods
1 parent f169a8f commit 30e6279

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

plugins/module_utils/cmci.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,32 @@ def is_alphanumeric(value):
152152
return re.match('^([A-Za-z0-9]{1,100})$', value, flags=0)
153153

154154

155-
def read_node(node):
155+
def read_node(node): # type: (OrderedDict) -> List[OrderedDict]
156+
# Reads a record node that can contain multiple lists of attributes
157+
result = [
158+
OrderedDict(
159+
[get_attribute(k, v)
160+
for k, v in n.items()]
161+
) for n in node
162+
]
163+
return result
164+
165+
166+
def read_error_node(node): # type: (OrderedDict) -> List[OrderedDict]
167+
# Reads an error node than can contain multiple lists of attributes that themselves contain
168+
# multiple lists of attributes
156169
result = [
157170
OrderedDict(
158-
# Copy feedback from result, stripping @ from attributes
159171
# Feedback nodes can contain error types with further information
160-
[(k[1:], v) if k[0] == '@'
172+
[get_attribute(k, v) if k[0] == '@'
161173
else read_error_detail(k, v)
162174
for k, v in n.items()]
163175
) for n in node
164176
]
165177
return result
166178

167179

168-
def read_error_detail(key, value):
180+
def read_error_detail(key, value): # type: (List[str, OrderedDict]) -> Tuple[str, List[OrderedDict]]
169181
# Xmltodict parses inner error types as Dicts when there is only one item in it even though it may well be a list
170182
# if multiple results were returned. If we find a dict here, wrap it in a list so we can account for only one result or many
171183
# being returned
@@ -179,6 +191,11 @@ def read_error_detail(key, value):
179191
]
180192

181193

194+
def get_attribute(k, v):
195+
#Return key, value pair stripping @ from the attributes key
196+
return (k[1:], v)
197+
198+
182199
class AnsibleCMCIModule(object):
183200

184201
def __init__(self, method):
@@ -397,7 +414,7 @@ def handle_response(self, response_dict): # type: (Dict) -> None
397414
errors_node = response_node['errors']
398415
if FEEDBACK in errors_node:
399416
feedback = errors_node[FEEDBACK]
400-
self.result[FEEDBACK] = read_node(feedback)
417+
self.result[FEEDBACK] = read_error_node(feedback)
401418

402419
# Non-OK CPSM responses fail the module
403420
if cpsm_response_code != 1024:

0 commit comments

Comments
 (0)