@@ -28,7 +28,7 @@ def __virtual__():
2828
2929def present (name , description = None , category_id = None ):
3030 """
31- Create and update a tag instance.
31+ Create or update a tag instance.
3232
3333 name
3434 Name of tag.
@@ -151,3 +151,143 @@ def absent(name):
151151 else :
152152 ret ["comment" ] = "Tag does not exist"
153153 return ret
154+
155+
156+ def present_category (name , associable_types , cardinality , description = "" ):
157+ """
158+ Create or update a category.
159+
160+ name
161+ The display name of the category.
162+
163+ associable_types
164+ (list) Object types to which this category’s tags can be attached.
165+
166+ cardinality
167+ The CategoryModel.Cardinality enumerated type defines the number of tags in a category that can be assigned to an object. SINGLE, MULTIPLE
168+
169+ description
170+ (optional) The description of the category.
171+ """
172+ ret = {"name" : name , "changes" : {}, "result" : True , "comment" : "" }
173+ res = connect .request (
174+ "/rest/com/vmware/cis/tagging/category" , "GET" , opts = __opts__ , pillar = __pillar__
175+ )
176+ response = res ["response" ].json ()
177+ token = res ["token" ]
178+ found = None
179+ for cat in response ["value" ]:
180+ url = f"/rest/com/vmware/cis/tagging/category/id:{ cat } "
181+ cat_ref = connect .request (url , "GET" , token = token , opts = __opts__ , pillar = __pillar__ )
182+ cat_ref = cat_ref ["response" ].json ()
183+ if cat_ref ["value" ]["name" ] == name :
184+ found = cat_ref ["value" ]
185+ break
186+ if found :
187+ if (
188+ associable_types == found ["associable_types" ]
189+ and cardinality == found ["cardinality" ]
190+ and description == found ["description" ]
191+ ):
192+ ret ["comment" ] = "category exists"
193+ return ret
194+ else :
195+ ret ["changes" ]["new" ] = {}
196+ ret ["changes" ]["old" ] = {}
197+ if associable_types != found ["associable_types" ]:
198+ ret ["changes" ]["old" ]["associable_types" ] = found ["associable_types" ]
199+ ret ["changes" ]["new" ]["associable_types" ] = associable_types
200+ if cardinality != found ["cardinality" ]:
201+ ret ["changes" ]["old" ]["cardinality" ] = found ["cardinality" ]
202+ ret ["changes" ]["new" ]["cardinality" ] = cardinality
203+ if description != found ["description" ]:
204+ ret ["changes" ]["old" ]["description" ] = found ["description" ]
205+ ret ["changes" ]["new" ]["description" ] = description
206+ if __opts__ ["test" ]:
207+ ret ["result" ] = None
208+ ret ["comment" ] = f"{ name } category will be updated"
209+ return ret
210+ id = found ["id" ]
211+ spec = {"update_spec" : {}}
212+ if associable_types :
213+ spec ["update_spec" ]["associable_types" ] = associable_types
214+ if cardinality :
215+ spec ["update_spec" ]["cardinality" ] = cardinality
216+ if description :
217+ spec ["update_spec" ]["description" ] = description
218+ url = f"/rest/com/vmware/cis/tagging/category/id:{ id } "
219+ updated = connect .request (url , "PATCH" , body = spec , opts = __opts__ , pillar = __pillar__ )
220+ if updated ["response" ].status_code == 200 :
221+ ret ["comment" ] = "updated"
222+ return ret
223+ ret ["status_code" ] = updated ["response" ].status_code
224+ ret ["reason" ] = updated ["response" ].reason
225+ ret ["comment" ] = "failed to update"
226+ ret ["result" ] = False
227+ return ret
228+ else :
229+ if __opts__ ["test" ]:
230+ ret ["result" ] = None
231+ ret ["comment" ] = f"{ name } category will be created"
232+ return ret
233+ data = {
234+ "create_spec" : {
235+ "associable_types" : associable_types ,
236+ "cardinality" : cardinality ,
237+ "description" : description ,
238+ "name" : name ,
239+ }
240+ }
241+ create = connect .request (
242+ "/rest/com/vmware/cis/tagging/category" ,
243+ "POST" ,
244+ body = data ,
245+ opts = __opts__ ,
246+ pillar = __pillar__ ,
247+ )
248+ response = create ["response" ].json ()
249+ ret ["changes" ]["category_id" ] = response ["value" ]
250+ ret ["comment" ] = "created"
251+ return ret
252+
253+
254+ def absent_category (name ):
255+ """
256+ Delete category.
257+
258+ name
259+ Name of category.
260+ """
261+ ret = {"name" : name , "changes" : {}, "result" : True , "comment" : "" }
262+ res = connect .request (
263+ "/rest/com/vmware/cis/tagging/category" , "GET" , opts = __opts__ , pillar = __pillar__
264+ )
265+ response = res ["response" ].json ()
266+ token = res ["token" ]
267+ found = None
268+ for cat in response ["value" ]:
269+ url = f"/rest/com/vmware/cis/tagging/category/id:{ cat } "
270+ cat_ref = connect .request (url , "GET" , token = token , opts = __opts__ , pillar = __pillar__ )
271+ cat_ref = cat_ref ["response" ].json ()
272+ if cat_ref ["value" ]["name" ] == name :
273+ found = cat_ref ["value" ]
274+ break
275+ if found :
276+ if __opts__ ["test" ]:
277+ ret ["result" ] = None
278+ ret ["comment" ] = f"{ name } category will be deleted"
279+ return ret
280+ id = found ["id" ]
281+ url = f"/rest/com/vmware/cis/tagging/category/id:{ id } "
282+ delete = connect .request (url , "DELETE" , opts = __opts__ , pillar = __pillar__ )
283+ if delete ["response" ].status_code == 200 :
284+ ret ["comment" ] = "deleted"
285+ return ret
286+ ret ["status_code" ] = delete ["response" ].status_code
287+ ret ["reason" ] = delete ["response" ].reason
288+ ret ["comment" ] = "failed to delete"
289+ ret ["result" ] = False
290+ return ret
291+ else :
292+ ret ["comment" ] = "Category does not exist"
293+ return ret
0 commit comments