14
14
HTTP_RESOURCE_NOT_FOUND ,
15
15
HTTP_SUCCESS_CODES ,
16
16
NESTED_POST_DATA_RESOURCES ,
17
- GLOBAL_BINDING_ARG_LIST
17
+ GLOBAL_BINDING_ARG_LIST ,
18
+ NESTED_POST_DATA_RESOURCES_ALIAS ,
18
19
)
19
20
from .decorators import trace
20
21
from .logger import log
@@ -96,7 +97,7 @@ def get_resource(client, resource_name, resource_id=None, resource_module_params
96
97
else :
97
98
if resource_name in NESTED_POST_DATA_RESOURCES :
98
99
status_code , response_body = client .get (
99
- resource = "routerDynamicRouting/%s" % resource_name ,
100
+ resource = "routerDynamicRouting/%s" % NESTED_POST_DATA_RESOURCES_ALIAS [ resource_name ] ,
100
101
id = resource_id ,
101
102
args = get_args ,
102
103
)
@@ -108,12 +109,15 @@ def get_resource(client, resource_name, resource_id=None, resource_module_params
108
109
)
109
110
if status_code in {HTTP_RESOURCE_NOT_FOUND }:
110
111
return False , []
112
+ return_response = None
111
113
if status_code in HTTP_SUCCESS_CODES :
112
114
# for zero bindings and some resources, the response_body will be {'errorcode': 0, 'message': 'Done', 'severity': 'NONE'}
113
115
if resource_name in NESTED_POST_DATA_RESOURCES :
114
116
if "routerDynamicRouting" in response_body :
115
- response_body = response_body ["routerDynamicRouting" ]
116
- if resource_name not in response_body :
117
+ return_response = response_body ["routerDynamicRouting" ][NESTED_POST_DATA_RESOURCES_ALIAS [resource_name ]]
118
+ else :
119
+ return False , []
120
+ elif resource_name not in response_body :
117
121
if resource_name == "sslcipher" :
118
122
resource_primary_key = NITRO_RESOURCE_MAP [resource_name ]["primary_key" ]
119
123
return True , [
@@ -122,32 +126,33 @@ def get_resource(client, resource_name, resource_id=None, resource_module_params
122
126
123
127
return False , []
124
128
125
- # `update-only` resources return a dict instead of a list.
126
- return_response = response_body [resource_name ]
129
+ # `update-only` resources return a dict instead of a list.
130
+ if resource_name not in NESTED_POST_DATA_RESOURCES :
131
+ return_response = response_body [resource_name ]
127
132
# FIXME: NITRO-BUG: for some resources like `policypatset_pattern_binding`, NITRO returns keys with uppercase. eg: `String` for `string`.
128
133
# So, we are converting the keys to lowercase.
129
134
# except for `ping` and `traceroute`, all the othe resources returns a keys with lowercase.
130
135
# These `ping` and `traceroute` do not have GET operation. So, we are not handling them here.
131
- if isinstance (return_response , dict ):
132
- return_response = [{k .lower (): v for k , v in return_response .items ()}]
133
- elif isinstance (return_response , list ):
134
- return_response = [
135
- {k .lower (): v for k , v in resource .items ()}
136
- for resource in return_response
137
- ]
138
- else :
139
- log (
140
- "WARNING: Unexpected response for resource `{}`. Expected a list or a dict, but got: {}" .format (
141
- resource_name , return_response
136
+ if resource_name not in NESTED_POST_DATA_RESOURCES :
137
+ if isinstance (return_response , dict ):
138
+ return_response = [{k .lower (): v for k , v in return_response .items ()}]
139
+ elif isinstance (return_response , list ):
140
+ return_response = [
141
+ {k .lower (): v for k , v in resource .items ()}
142
+ for resource in return_response
143
+ ]
144
+ else :
145
+ log (
146
+ "WARNING: Unexpected response for resource `{}`. Expected a list or a dict, but got: {}" .format (
147
+ resource_name , return_response
148
+ )
142
149
)
143
- )
144
150
145
151
# Take care of NITRO Anomolies
146
152
return_response = fix_nitro_anomolies (
147
153
resource_name , resource_module_params , return_response
148
154
)
149
155
return (True , return_response )
150
- log ("pass 3 " )
151
156
return False , []
152
157
153
158
@@ -233,26 +238,27 @@ def _check_create_resource_params(resource_name, resource_module_params, action=
233
238
else :
234
239
# TODO: Should we allow non-add keys for the resource? OR should we error out if any non-add key is passed?
235
240
if resource_name in NESTED_POST_DATA_RESOURCES :
236
- post_data = { "routerDynamicRouting" : { resource_name : {}}}
237
- resource_add_keys = NITRO_RESOURCE_MAP [resource_name ][ "add_payload_keys" ]
238
-
239
- for key in resource_module_params . keys ():
240
- if key in resource_add_keys :
241
- keylist = key . split ( "." )
242
- current_dict = post_data [ "routerDynamicRouting" ][ resource_name ]
243
- for i , k in enumerate ( keylist ):
244
- if i == len (keylist ) - 1 :
245
- current_dict [ k ] = resource_module_params [ key ]
246
- else :
247
- if k not in current_dict :
248
- current_dict [ k ] = {}
249
- current_dict = current_dict [k ]
250
- else :
251
- log (
252
- "WARNING: Key `{}` is not allowed for the resource `{}` for CREATE operation. Skipping the key for the operation" . format (
253
- key , resource_name
254
- )
241
+ resource_add_keys = NITRO_RESOURCE_MAP [ resource_name ][ "add_payload_keys" ]
242
+ resource_name = NESTED_POST_DATA_RESOURCES_ALIAS [resource_name ]
243
+ post_data = { "routerDynamicRouting" : { resource_name : {}}}
244
+
245
+ for key in resource_module_params . keys () :
246
+ if key in resource_add_keys :
247
+ keylist = key . split ( "." )
248
+ current_dict = post_data [ "routerDynamicRouting" ][ resource_name ]
249
+ for i , k in enumerate (keylist ):
250
+ if i == len ( keylist ) - 1 :
251
+ current_dict [ k ] = resource_module_params [ key ]
252
+ else :
253
+ if k not in current_dict :
254
+ current_dict [k ] = {}
255
+ current_dict = current_dict [ k ]
256
+ else :
257
+ log (
258
+ "WARNING: Key `{}` is not allowed for the resource `{}` for CREATE operation. Skipping the key for the operation" . format (
259
+ key , resource_name
255
260
)
261
+ )
256
262
else :
257
263
for key in resource_module_params .keys ():
258
264
if not action :
0 commit comments