def list_entities(self, filters: dict = None) -> List[DefEntity]: """List all defined entities of all entity types. vCD's behavior when invalid filter keys are passed: * It will throw a 400 if invalid first-level filter keys are passed Valid keys : [name, id, externalId, entityType, entity, state]. * It will simply ignore any invalid nested properties and will simply return empty list. :param dict filters: Key-value pairs representing filter options :return: Generator of defined entities :rtype: Generator[DefEntity, None, None] """ filter_string = None if filters: filter_string = ";".join([f"{k}=={v}" for (k, v) in filters.items()]) # noqa: E501 page_num = 0 while True: page_num += 1 query_string = f"page={page_num}&sortAsc=name" if filter_string: query_string = f"filter={filter_string}&{query_string}" response_body = self._cloudapi_client.do_request( method=RequestMethod.GET, cloudapi_version=CLOUDAPI_VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}?{query_string}") # noqa: E501 if len(response_body['values']) == 0: break for entity in response_body['values']: yield DefEntity(**entity)
def list_entities_by_interface(self, vendor: str, nss: str, version: str): """List entities of a given interface. An interface is uniquely identified by properties vendor, nss and version. :param str vendor: Vendor of the interface :param str nss: nss of the interface :param str version: version of the interface :return: Generator of entities of that interface type :rtype: Generator[DefEntity, None, None] """ # TODO Yet to be verified. Waiting for the build from Extensibility # team. page_num = 0 while True: page_num += 1 response_body = self._cloudapi_client.do_request( method=RequestMethod.GET, cloudapi_version=CLOUDAPI_VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}/" f"{CloudApiResource.INTERFACES}/{vendor}/{nss}/{version}?" # noqa: E501 f"page={page_num}") if len(response_body['values']) == 0: break for entity in response_body['values']: yield DefEntity(**entity)
def list_entities_by_entity_type(self, vendor: str, nss: str, version: str) -> List[DefEntity]: """List entities of a given entity type. An entity type is uniquely identified by properties vendor, nss and version. :param str vendor: Vendor of the entity type :param str nss: nss of the entity type :param str version: version of the entity type :return: List of entities of that entity type :rtype: List[DefEntity] """ # TODO Yet to be verified. Waiting for the build from # Extensibility team. page_num = 0 while True: page_num += 1 response_body = self._cloudapi_client.do_request( method=RequestMethod.GET, cloudapi_version=CLOUDAPI_VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}/" f"{vendor}/{nss}/{version}?page={page_num}") # noqa: E501 if len(response_body['values']) > 0: for entity in response_body['values']: yield DefEntity(**entity) else: break
def get_entity_by_name(self, name: str) -> DefEntity: # TODO(DEF) Below call should add another filter field 'entity.kind==native' # noqa: E501 # It should not get entities if non-native clusters. # Awaiting dependency from Extensibility team." response_body = self._cloudapi_client.do_request( method=RequestMethod.GET, cloudapi_version=CLOUDAPI_VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}?filter=name=={name}") # noqa: E501 entity = response_body['values'][0] return DefEntity(**entity)
def get_entity(self, entity_id: str) -> DefEntity: """Get the defined entity given entity id. :param str entity_id: Id of the entity. :return: Details of the entity. :rtype: DefEntity """ response_body = self._cloudapi_client.do_request( method=RequestMethod.GET, cloudapi_version=CLOUDAPI_VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}/" f"{entity_id}") return DefEntity(**response_body)
def update_entity(self, entity_id: str, entity: DefEntity) -> DefEntity: """Update entity instance. :param str entity_id: Id of the entity to be updated. :param DefEntity entity: Modified entity to be updated. :return: Updated entity :rtype: DefEntity """ response_body = self._cloudapi_client.do_request( method=RequestMethod.PUT, cloudapi_version=CLOUDAPI_VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}/" f"{entity_id}", payload=asdict(entity)) return DefEntity(**response_body)
def list_entities(self): """List all defined entities of all entity types. :return: Generator of defined entities :rtype: Generator[DefEntity] """ page_num = 0 while True: page_num += 1 response_body = self._cloudapi_client.do_request( method=RequestMethod.GET, cloudapi_version=CLOUDAPI_VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}?" f"page={page_num}") if len(response_body['values']) > 0: for entity in response_body['values']: yield DefEntity(**entity) else: break
def resolve_entity(self, entity_id: str) -> DefEntity: """Resolve the entity. Validates the entity against the schema. Based on the result, entity state will be either changed to "RESOLVED" (or) "RESOLUTION ERROR". :param str entity_id: Id of the entity :return: Defined entity with its state updated. :rtype: DefEntity """ response_body = self._cloudapi_client.do_request( method=RequestMethod.POST, cloudapi_version=CLOUDAPI_VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}/" f"{entity_id}/{CloudApiResource.ENTITY_RESOLVE}") # noqa: E501 msg = response_body[def_utils.DEF_ERROR_MESSAGE_KEY] del response_body[def_utils.DEF_ERROR_MESSAGE_KEY] entity = DefEntity(**response_body) # TODO: Just record the error message; revisit after HTTP response code # is good enough to decide if exception should be thrown or not if entity.state != def_utils.DEF_RESOLVED_STATE: LOGGER.error(msg) return entity
def resolve_entity(self, entity_id: str) -> DefEntity: """Resolve the entity. Validates the entity against the schema. Based on the result, entity state will be either changed to "RESOLVED" (or) "RESOLUTION ERROR". :param str entity_id: Id of the entity :return: Defined entity with its state updated. :rtype: DefEntity """ response_body = self._cloudapi_client.do_request( method=RequestMethod.POST, cloudapi_version=CLOUDAPI_VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}/" f"{entity_id}/{CloudApiResource.ENTITY_RESOLVE}") # noqa: E501 msg = response_body[def_utils.DEF_ERROR_MESSAGE_KEY] del response_body[def_utils.DEF_ERROR_MESSAGE_KEY] entity = DefEntity(**response_body) if entity.state != def_utils.DEF_RESOLVED_STATE: raise cse_exception.DefEntityResolutionError(id=entity.id, state=entity.state, msg=msg) return entity
def list_entities_by_entity_type(self, vendor: str, nss: str, version: str, filters: dict = None) -> List[DefEntity]: """List entities of a given entity type. vCD's behavior when invalid filter keys are passed: * It will throw a 400 if invalid first-level filter keys are passed Valid keys : [name, id, externalId, entityType, entity, state]. * It will simply return empty list on passing any invalid nested properties. :param str vendor: Vendor of the entity type :param str nss: nss of the entity type :param str version: version of the entity type :param dict filters: Key-value pairs representing filter options :return: List of entities of that entity type :rtype: Generator[DefEntity, None, None] """ filter_string = utils.construct_filter_string(filters) page_num = 0 while True: page_num += 1 query_string = f"page={page_num}&sortAsc=name" if filter_string: query_string = f"filter={filter_string}&{query_string}" response_body = self._cloudapi_client.do_request( method=RequestMethod.GET, cloudapi_version=CloudApiVersion.VERSION_1_0_0, resource_url_relative_path=f"{CloudApiResource.ENTITIES}/" f"{CloudApiResource.ENTITY_TYPES_TOKEN}/" # noqa: E501 f"{vendor}/{nss}/{version}?{query_string}") # noqa: E501 if len(response_body['values']) == 0: break for entity in response_body['values']: yield DefEntity(**entity)