Exemple #1
0
    def get_native_rde_by_name_and_rde_version(
            self,
            name: str,
            version: str,  # noqa: E501
            filters: dict = None) -> DefEntity:  # noqa: E501
        """Get native RDE given its name and RDE version.

        This function is used commonly by CSE CLI client and CSE server

        :param str name: Name of the native cluster.
        :param str version: RDE version
        :rtype: DefEntity
        :return: Native cluster RDE
        """
        if not filters:
            filters = {}
        filters[def_constants.ClusterEntityFilterKey.CLUSTER_NAME.value] = name
        native_entity_type: DefEntityType = \
            def_utils.get_rde_metadata(version)[def_constants.RDEMetadataKey.ENTITY_TYPE]  # noqa: E501
        for entity in \
            self.list_entities_by_entity_type(vendor=native_entity_type.vendor,  # noqa: E501
                                              nss=native_entity_type.nss,  # noqa: E501
                                              version=native_entity_type.version,  # noqa: E501
                                              filters=filters):
            return entity
Exemple #2
0
def cleanup_rde_artifacts(logger=NULL_LOGGER):
    """Cleanup all defined entity related artifacts.

    Deletes the following -
    - CSE interface
    - Native entity type
    """
    try:
        rde_version_in_use = rde_utils.get_runtime_rde_version_by_vcd_api_version(
            CLIENT.get_api_version())  # noqa: E501
        rde_metadata = rde_utils.get_rde_metadata(rde_version_in_use)
        cloudapi_client = pyvcloud_utils.get_cloudapi_client_from_vcd_client(
            client=CLIENT,
            logger_debug=logger,
            logger_wire=SERVER_CLOUDAPI_WIRE_LOGGER)
        schema_svc = def_schema_svc.DefSchemaService(
            cloudapi_client=cloudapi_client)  # noqa: E501
        if rde_constants.RDEMetadataKey.ENTITY_TYPE in rde_metadata:
            # delete entity_type
            entity_type: common_models.DefEntityType = \
                rde_metadata[rde_constants.RDEMetadataKey.ENTITY_TYPE]
            schema_svc.delete_entity_type(entity_type.get_id())
            logger.debug(f"Deleted entity type: {entity_type.name}")
        if rde_constants.RDEMetadataKey.INTERFACES in rde_metadata:
            # delete interface
            interfaces: List[common_models.DefInterface] = \
                rde_metadata[rde_constants.RDEMetadataKey.INTERFACES]
            for i in interfaces:
                interface_id = i.get_id()
                if interface_id != common_models.K8Interface.VCD_INTERFACE.value.get_id(
                ):  # noqa: E501
                    schema_svc.delete_interface(interface_id)
                    logger.debug(f"Deleted interface: {i.name}")
    except Exception as e:
        logger.warning(f"Failed to clean up RDE artifacts: {e}")
    def list_all_native_rde_by_name_and_rde_version(
            self,
            name: str,
            version: str,
            filters: dict = None) -> DefEntity:  # noqa: E501
        """List all native RDE given its name and RDE version.

        This function has been introduced to make it easier to iterate over
            all the RDE with the same name.

        :param str name: Name of the native cluster.
        :param str version: RDE version
        :param dict filters: dictionary representing filters
        :return: List of entities
        :rtype: Generator[DefEntity, None, None]
        """
        if not filters:
            filters = {}
        filters[def_constants.RDEFilterKey.NAME.value] = name
        native_entity_type: DefEntityType = \
            def_utils.get_rde_metadata(version)[def_constants.RDEMetadataKey.ENTITY_TYPE]  # noqa: E501
        for entity in \
            self.list_entities_by_entity_type(vendor=native_entity_type.vendor,
                                              nss=native_entity_type.nss,
                                              version=native_entity_type.version,  # noqa: E501
                                              filters=filters):
            yield entity
Exemple #4
0
    def _load_def_schema(self, msg_update_callback=utils.NullPrinter()):
        """Load cluster interface and cluster entity type to global context.

        If defined entity framework is supported by vCD api version, load
        defined entity interface and defined entity type registered during
        server install

        :param utils.NullMessagePrinter msg_update_callback:
        """
        sysadmin_client = None
        try:
            sysadmin_client = vcd_utils.get_sys_admin_client(api_version=None)
            logger_wire = logger.NULL_LOGGER
            if utils.str_to_bool(
                    utils.str_to_bool(self.config['service'].get(
                        'log_wire', False))):  # noqa: E501
                logger_wire = logger.SERVER_CLOUDAPI_WIRE_LOGGER

            cloudapi_client = \
                vcd_utils.get_cloudapi_client_from_vcd_client(sysadmin_client,
                                                              logger.SERVER_LOGGER,  # noqa: E501
                                                              logger_wire)
            raise_error_if_def_not_supported(cloudapi_client)

            server_rde_version = server_utils.get_rde_version_in_use()
            msg_update_callback.general(
                f"Using RDE version: {server_rde_version}")  # noqa: E501

            schema_svc = def_schema_svc.DefSchemaService(cloudapi_client)
            def_metadata_dict: dict = def_utils.get_rde_metadata(
                server_rde_version)  # noqa: E501
            entity_type: common_models.DefEntityType = \
                def_metadata_dict[def_constants.RDEMetadataKey.ENTITY_TYPE]  # noqa: E501
            interfaces: List[common_models.DefInterface] = \
                def_metadata_dict[def_constants.RDEMetadataKey.INTERFACES]  # noqa: E501

            for interface in interfaces:
                # TODO change _kubernetesInterface to an array once additional
                # interface for CSE is added.
                self._kubernetesInterface = \
                    schema_svc.get_interface(interface.get_id())

            self._nativeEntityType = \
                schema_svc.get_entity_type(entity_type.get_id())

            msg = f"Successfully loaded defined entity schema " \
                  f"{entity_type.get_id()} to global context"
            msg_update_callback.general(msg)
            logger.SERVER_LOGGER.debug(msg)
        except cse_exception.DefNotSupportedException:
            msg = "Skipping initialization of defined entity type" \
                  " and defined entity interface"
            msg_update_callback.info(msg)
            logger.SERVER_LOGGER.debug(msg)
        except cse_exception.DefSchemaServiceError as e:
            msg = f"Error while loading defined entity schema: {e.error_message}"  # noqa: E501
            msg_update_callback.error(msg)
            logger.SERVER_LOGGER.debug(msg)
            raise
        except Exception as e:
            msg = f"Failed to load defined entity schema to global context: {str(e)}"  # noqa: E501
            msg_update_callback.error(msg)
            logger.SERVER_LOGGER.error(msg)
            raise
        finally:
            if sysadmin_client:
                sysadmin_client.logout()