20
20
from urlparse import urlparse # python2
21
21
22
22
23
- class EntityV4 (object ):
24
- module = None
25
-
26
- def __init__ (self , module ):
27
- self .module = module
28
-
29
- # old_spec is used for updating entity, where there is already spec object present.
30
- def get_spec (self , module_args , params = None , obj = None , old_spec = None ):
31
- """
32
- For given module parameters input, it will create new spec object or update old_spec object.
33
- It will pick module.params if 'params' is not given.
34
- Args:
35
- module_args (dict): module argument spec for reference
36
- obj (object): spec class from sdk
37
- params (dict): input for creating spec
38
- old_spec (object): Old state obj of entity
39
- Returns:
40
- spec (object): spec object
41
- """
42
-
43
- if not params :
44
- params = copy .deepcopy (self .module .params )
45
-
46
- if not old_spec and not obj :
47
- return (
48
- None ,
49
- "Either 'old_spec' or 'obj' is required to create/update spec object" ,
50
- )
51
-
52
- # create spec obj or shallow copy of old spec as per entity spec - new or existing
53
- spec = copy .copy (old_spec ) if old_spec else obj ()
54
-
55
- # Resolve each input param w.r.t its module argument spec
56
- for attr , schema in module_args .items ():
57
-
58
- if attr in params :
59
-
60
- type = schema .get ("type" )
61
- if not type :
62
- return (
63
- None ,
64
- "Invalid module argument: 'type' is required parameter for attribute {0}" .format (
65
- attr
66
- ),
67
- )
68
-
69
- options = schema .get ("options" )
70
- _obj = schema .get ("obj" )
71
- elements = schema .get ("elements" )
72
-
73
- # for dict type attribute, recursively create spec objects
74
- if type == "dict" and options is not None and _obj is not None :
75
- s , err = self .get_spec (
76
- module_args = options ,
77
- obj = _obj ,
78
- params = params [attr ],
79
- old_spec = getattr (spec , attr ),
80
- )
81
- if err :
82
- return None , err
83
- setattr (spec , attr , s )
84
-
85
- # for list type attribute, create list of spec objects recursively
86
- elif (
87
- type == "list"
88
- and elements == "dict"
89
- and options is not None
90
- and _obj is not None
91
- ):
92
- lst = []
93
- for item in params [attr ]:
94
- s , err = self .get_spec (
95
- module_args = options , obj = _obj , params = item
96
- )
97
- if err :
98
- return None , err
99
- lst .append (s )
100
- setattr (spec , attr , lst )
101
-
102
- # for other types directly assign
103
- else :
104
- setattr (spec , attr , params [attr ])
105
-
106
- return spec , None
107
-
108
- def get_info_spec (self , params = None ):
109
-
110
- if not params :
111
- params = copy .deepcopy (self .module .params )
112
- spec = {}
113
- all_params = ["page" , "limit" , "filter" , "orderby" , "select" ]
114
- if params .get ("name" ):
115
- _filter = params .get ("filter" )
116
- if _filter :
117
- _filter += f"""and name eq '{ params ["name" ]} '"""
118
- else :
119
- _filter = f"""name eq '{ params ["name" ]} '"""
120
- params ["filter" ] = _filter
121
- for key , val in params .items ():
122
- if key in all_params :
123
- spec [f"_{ key } " ] = val
124
- return spec
125
-
126
-
127
23
class Entity (object ):
128
24
entities_limitation = 20
129
25
entity_type = "entities"
@@ -172,7 +68,7 @@ def read(
172
68
no_response = False ,
173
69
timeout = 30 ,
174
70
method = "GET" ,
175
- ** kwargs ,
71
+ ** kwargs # fmt: skip
176
72
):
177
73
url = self .base_url + "/{0}" .format (uuid ) if uuid else self .base_url
178
74
if endpoint :
@@ -185,7 +81,7 @@ def read(
185
81
raise_error = raise_error ,
186
82
no_response = no_response ,
187
83
timeout = timeout ,
188
- ** kwargs ,
84
+ ** kwargs # fmt: skip
189
85
)
190
86
191
87
def update (
@@ -198,7 +94,7 @@ def update(
198
94
no_response = False ,
199
95
timeout = 30 ,
200
96
method = "PUT" ,
201
- ** kwargs ,
97
+ ** kwargs # fmt: skip
202
98
):
203
99
url = self .base_url + "/{0}" .format (uuid ) if uuid else self .base_url
204
100
if endpoint :
@@ -212,7 +108,7 @@ def update(
212
108
raise_error = raise_error ,
213
109
no_response = no_response ,
214
110
timeout = timeout ,
215
- ** kwargs ,
111
+ ** kwargs # fmt: skip
216
112
)
217
113
218
114
# source is the file path of resource where ansible yaml runs
@@ -458,7 +354,7 @@ def _fetch_url(
458
354
raise_error = True ,
459
355
no_response = False ,
460
356
timeout = 30 ,
461
- ** kwargs ,
357
+ ** kwargs # fmt: skip
462
358
):
463
359
# only jsonify if content-type supports, added to avoid incase of form-url-encodeded type data
464
360
if self .headers ["Content-Type" ] == "application/json" and data is not None :
0 commit comments