[docs]asyncdefpresent(hub,ctx,name:str,config:Any,id:Any,typeId:Any,**kwargs):""" :param object config: (required in body) Source custom configuration :param string id: (required in body) Catalog Source id :param string name: (required in body) Catalog Source name :param string typeId: (required in body) Type of source, e.g. blueprint, CFT... etc :param string apiVersion: (optional in query) The version of the API in yyyy-MM-dd format (UTC). If you do not specify explicitly an exact version, you will be calling the latest supported API version. :param boolean validationOnly: (optional in query) If true, the source will not be created. It returns the number of items belonging to the source. The request will still return an error code if the source is invalid. :param string createdAt: (optional in body) Creation time :param string createdBy: (optional in body) Created By :param string description: (optional in body) Catalog Source description :param boolean global: (optional in body) Global flag indicating that all the items can be requested across all projects. :param string iconId: (optional in body) Default Icon Id :param integer itemsFound: (optional in body) Number of items found :param integer itemsImported: (optional in body) Number of items imported. :param string lastImportCompletedAt: (optional in body) Last import completion time :param array lastImportErrors: (optional in body) Last import error(s) :param string lastImportStartedAt: (optional in body) Last import start time :param string lastUpdatedAt: (optional in body) Update time :param string lastUpdatedBy: (optional in body) Updated By :param string projectId: (optional in body) Project id where the source belongs """try:state=CatalogsourcesState(hub,ctx)returnawaitstate.present(hub,ctx,name,config,id,typeId,**kwargs)exceptExceptionaserror:hub.log.error("Error during enforcing present state: catalogsources")hub.log.error(str(error))raiseerror
[docs]asyncdefabsent(hub,ctx,name:str,**kwargs):""" :param string p_sourceId: (required in path) Catalog source ID :param string apiVersion: (optional in query) The version of the API in yyyy-MM-dd format (UTC). If you do not specify explicitly an exact version, you will be calling the latest supported API version. """""" :param string name: (required) name of the resource """try:state=CatalogsourcesState(hub,ctx)returnawaitstate.absent(hub,ctx,name,**kwargs)exceptExceptionaserror:hub.log.error("Error during enforcing absent state: catalogsources")hub.log.error(str(error))raiseerror
[docs]asyncdefdescribe(hub,ctx):try:state=CatalogsourcesState(hub,ctx)returnawaitstate.describe(hub,ctx)exceptExceptionaserror:hub.log.error("Error during describe: catalogsources")hub.log.error(str(error))raiseerror
[docs]defis_pending(hub,ret:dict,state:str=None,**pending_kwargs):try:state=CatalogsourcesState(hub,None)returnstate.is_pending(hub,ret,state,**pending_kwargs)exceptExceptionaserror:hub.log.error("Error during is_pending: catalogsources")hub.log.error(str(error))raiseerror
[docs]asyncdefpresent(self,hub,ctx,name:str,config:Any,id:Any,typeId:Any,**kwargs):search_result=(awaitself.paginate_find(hub,ctx))["ret"]forsinsearch_result.content:ifname==s["name"]andTrue:hub.log.info(f'Returning resource catalogsources "{s["name"]}" due to existing resource "{name}"')s=awaitself.remap_resource_structure(hub,ctx,s)returnStateReturn(result=True,comment=f"Resource catalogsources {name} already exists.",old=s,new=s,)res=(awaithub.exec.vra.catalog.catalogsources.post_using_post2(ctx,config,id,name,typeId,**kwargs))["ret"]res=awaitself.remap_resource_structure(hub,ctx,res)returnStateReturn(result=True,comment=f"Creation of catalogsources {name} success.",old=None,new=res,)
[docs]asyncdefabsent(self,hub,ctx,name:str,**kwargs):search_result=(awaitself.paginate_find(hub,ctx))["ret"]resource=Noneforsinsearch_result.content:ifname==s["name"]andTrue:hub.log.info(f'Found resource catalogsources "{s["name"]}" due to existing resource "{name}"')s=awaitself.remap_resource_structure(hub,ctx,s)resource=sifresource:# it exists!delete_kwargs={}delete_kwargs["p_sourceId"]=resource.get("sourceId")hub.log.debug(f"catalogsources with name = {resource.get('name')} already exists")awaithub.exec.vra.catalog.catalogsources.delete_using_delete4(ctx,**delete_kwargs)returnStateReturn(result=True,comment=f"Resource with name = {resource.get('name')} deleted.",old=resource,new=None,)returnStateReturn(result=True,comment=f"Resource with name = {name} is already absent.",old=None,new=None,)
[docs]asyncdefdescribe(self,hub,ctx):result={}res=awaitself.paginate_find(hub,ctx)forobjinres.get("ret",{}).get("content",[]):# Keep track of name and id properties as they may get remappedobj_name=obj.get("name","unknown")obj_id=obj.get("id","unknown")obj=awaitself.remap_resource_structure(hub,ctx,obj)# Define propsprops=[{key:value}forkey,valueinobj.items()]# Build resultresult[f"{obj_name}-{obj_id.split('-')[-1]}"]={"vra.catalog.catalogsources.present":props}returnresult
[docs]asyncdefpaginate_find(self,hub,ctx,**kwargs):""" Paginate through all resources using their 'find' method. """res=awaithub.exec.vra.catalog.catalogsources.get_page_using_get2(ctx,**kwargs)numberOfElements=res.get("ret",{}).get("numberOfElements",0)totalElements=res.get("ret",{}).get("totalElements",0)initialElements=numberOfElementsifnumberOfElements!=totalElementsandtotalElements!=0:whileinitialElements<totalElements:hub.log.debug(f"Requesting catalogsources with offset={initialElements} out of {totalElements}")pres=awaithub.exec.vra.catalog.catalogsources.get_page_using_get2(ctx,skip=initialElements)initialElements+=pres.get("ret",{}).get("numberOfElements",0)aggO=res.get("ret",{}).get("content",[])aggN=pres.get("ret",{}).get("content",[])res["ret"]["content"]=[*aggO,*aggN]res["ret"]["numberOfElements"]=initialElementsreturnres
[docs]defis_pending(self,hub,ret:dict,state:str=None,**pending_kwargs):""" State reconciliation """hub.log.debug(f'Running is_pending for resource: {ret.get("__id__",None)}...')is_pending_result=Falsehub.log.debug(f'is_pending_result for resource "{ret.get("__id__",None)}": {is_pending_result}')returnis_pending_result
[docs]asyncdefremap_resource_structure(self,hub,ctx,obj:dict)->dict:schema_mapper={"add":[],"omit":["createdAt","createdBy","lastImportCompletedAt","lastImportErrors","lastImportStartedAt","lastUpdatedAt","lastUpdatedBy",],}# Perform resource mapping by adding properties and omitting properties.# Property renaming is addition followed by omission.ifschema_mapper:resource_name="catalogsources"hub.log.debug(f"Remapping resource {resource_name}...")obj=awaitadd_properties(obj,schema_mapper.get("add",[]))obj=omit_properties(obj,schema_mapper.get("omit",[]))returnobj