Ejemplo n.º 1
0
    def __init__(self, sysadmin_client: vcd_client.Client, log_wire=True):
        vcd_utils.raise_error_if_user_not_from_system_org(sysadmin_client)
        self._sysadmin_client: vcd_client.Client = sysadmin_client
        self._cloudapi_client = None
        self._session = self._sysadmin_client.get_vcloud_session()
        self._is_operation_supported = True

        try:
            wire_logger = logger.NULL_LOGGER
            if log_wire:
                wire_logger = logger.SERVER_CLOUDAPI_WIRE_LOGGER
            self._cloudapi_client = \
                vcd_utils.get_cloudapi_client_from_vcd_client(self._sysadmin_client, # noqa: E501
                                                              logger.SERVER_LOGGER, # noqa: E501
                                                              wire_logger)
            self._cloudapi_version = \
                cloudapi_constants.CloudApiVersion.VERSION_2_0_0
            if float(self._cloudapi_client.get_api_version()) < \
                    GLOBAL_PVDC_COMPUTE_POLICY_MIN_VERSION:
                self._cloudapi_version = \
                    cloudapi_constants.CloudApiVersion.VERSION_1_0_0

            # Since the /cloudapi endpoint was added before the compute policy
            # endpoint. Mere presence of the /cloudapi uri is not enough, we
            # need to make sure that this cloud api client will be of actual
            # use to us.
            self._cloudapi_client.do_request(
                method=RequestMethod.GET,
                cloudapi_version=self._cloudapi_version,
                resource_url_relative_path=
                f"{cloudapi_constants.CloudApiResource.VDC_COMPUTE_POLICIES}"
            )  # noqa: E501
        except requests.exceptions.HTTPError as err:
            logger.SERVER_LOGGER.error(err)
            self._is_operation_supported = False
Ejemplo n.º 2
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.ConsoleMessagePrinter msg_update_callback
        """
        sysadmin_client = None
        try:
            sysadmin_client = vcd_utils.get_sys_admin_client()
            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)
            schema_svc = def_schema_svc.DefSchemaService(cloudapi_client)
            defKey = def_utils.DefKey
            keys_map = def_utils.MAP_API_VERSION_TO_KEYS[float(
                sysadmin_client.get_api_version())]  # noqa: E501
            interface_id = def_utils.generate_interface_id(
                vendor=keys_map[defKey.VENDOR],  # noqa: E501
                nss=keys_map[defKey.INTERFACE_NSS],  # noqa: E501
                version=keys_map[defKey.INTERFACE_VERSION])  # noqa: E501
            entity_type_id = def_utils.generate_entity_type_id(
                vendor=keys_map[defKey.VENDOR],  # noqa: E501
                nss=keys_map[defKey.ENTITY_TYPE_NSS],  # noqa: E501
                version=keys_map[defKey.ENTITY_TYPE_VERSION])  # noqa: E501
            self._nativeInterface = schema_svc.get_interface(interface_id)
            self._nativeEntityType = schema_svc.get_entity_type(entity_type_id)
            msg = "Successfully loaded defined entity schema 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.minor_error_code}"  # noqa: E501
            msg_update_callback.error(msg)
            logger.SERVER_LOGGER.debug(msg)
            raise e
        except Exception as e:
            msg = f"Failed to load defined entity schema to global context: {str(e)}"  # noqa: E501
            msg_update_callback.error(e)
            logger.SERVER_LOGGER.error(e)
            raise (e)
        finally:
            if sysadmin_client:
                sysadmin_client.logout()
Ejemplo n.º 3
0
 def __init__(self, client):
     logger_wire = logger.NULL_LOGGER
     if os.getenv(cli_constants.ENV_CSE_CLIENT_WIRE_LOGGING):
         logger_wire = logger.CLIENT_WIRE_LOGGER
     self._cloudapi_client = \
         vcd_utils.get_cloudapi_client_from_vcd_client(
             client=client, logger_debug=logger.CLIENT_LOGGER,
             logger_wire=logger_wire)
     self._native_cluster_api = NativeClusterApi(client)
Ejemplo n.º 4
0
 def __init__(self,
              sysadmin_client: vcd_client.Client,
              log_wire=False,
              logger_debug=NULL_LOGGER):
     vcd_utils.raise_error_if_not_sysadmin(sysadmin_client)
     self.logger_wire = SERVER_CLOUDAPI_WIRE_LOGGER \
         if log_wire else NULL_LOGGER
     self.logger_debug = logger_debug
     self.cloudapi_client = vcd_utils.get_cloudapi_client_from_vcd_client(
         sysadmin_client,
         logger_debug=self.logger_debug,
         logger_wire=self.logger_wire)
 def cloudapi_client(self):
     if self._cloudapi_client is None:
         log_wire = utils.get_server_runtime_config() \
                         .get('service', {}).get('log_wire', False)
         logger_wire = logger.NULL_LOGGER
         if log_wire:
             logger_wire = logger.SERVER_CLOUDAPI_WIRE_LOGGER
         self._cloudapi_client = \
             vcd_utils.get_cloudapi_client_from_vcd_client(self.client,
                                                           logger.SERVER_LOGGER, # noqa: E501
                                                           logger_wire)
     return self._cloudapi_client
    def __init__(self, sysadmin_client: vcd_client.Client,
                 debug_logger=logger.SERVER_LOGGER, log_wire=True):
        # Ensure correct credentials and api version
        vcd_utils.raise_error_if_not_sysadmin(sysadmin_client)
        client_api_version = float(sysadmin_client.get_api_version())
        if client_api_version < constants.MQTT_MIN_API_VERSION:
            raise ValueError(f'API version {client_api_version} '
                             f'is less than required version '
                             f'{constants.MQTT_MIN_API_VERSION} to use MQTT')

        self._sysadmin_client: vcd_client.Client = sysadmin_client
        wire_logger = logger.NULL_LOGGER
        if log_wire:
            wire_logger = logger.SERVER_CLOUDAPI_WIRE_LOGGER
        self._wire_logger = wire_logger
        self._debug_logger = debug_logger
        self._cloudapi_client = \
            vcd_utils.get_cloudapi_client_from_vcd_client(
                self._sysadmin_client, self._debug_logger, self._wire_logger)
Ejemplo n.º 7
0
def _register_def_schema(client: Client,
                         msg_update_callback=utils.NullPrinter(),
                         log_wire=False):
    """Register defined entity interface and defined entity type.

    If vCD api version is >= 35, register the vCD api version based
    defined entity interface and defined entity type. Read the schema present
    in the location dictated by api version to register the
    defined entity type.

    :param pyvcloud.vcd.client.Client client:
    :param utils.ConsoleMessagePrinter msg_update_callback: Callback object.
    :param bool log_wire: wire logging enabled
    """
    msg = "Registering defined entity schema"
    msg_update_callback.info(msg)
    INSTALL_LOGGER.debug(msg)
    logger_wire = SERVER_CLOUDAPI_WIRE_LOGGER if log_wire else NULL_LOGGER
    cloudapi_client = vcd_utils.get_cloudapi_client_from_vcd_client(
        client=client,  # noqa: E501
        logger_debug=INSTALL_LOGGER,  # noqa: E501
        logger_wire=logger_wire)  # noqa: E501
    schema_file = None
    try:
        def_utils.raise_error_if_def_not_supported(cloudapi_client)
        schema_svc = def_schema_svc.DefSchemaService(cloudapi_client)
        keys_map = def_utils.MAP_API_VERSION_TO_KEYS[float(
            client.get_api_version())]  # noqa: E501
        defKey = def_utils.DefKey
        native_interface = def_models.\
            DefInterface(name=keys_map[defKey.INTERFACE_NAME],
                         vendor=keys_map[defKey.VENDOR],
                         nss=keys_map[defKey.INTERFACE_NSS],
                         version=keys_map[defKey.INTERFACE_VERSION], # noqa: E501
                         readonly=False)
        msg = ""
        try:
            schema_svc.get_interface(native_interface.get_id())
            msg = "defined entity interface already exists." \
                  " Skipping defined entity interface creation"
        except HTTPError:
            # TODO handle this part only if the interface was not found
            native_interface = schema_svc.create_interface(native_interface)
            msg = "Successfully created defined entity interface"
        msg_update_callback.general(msg)
        INSTALL_LOGGER.debug(msg)

        # TODO stop-gap fix - find efficient way to import schema
        import importlib
        import importlib.resources as pkg_resources
        schema_module = importlib.import_module(
            f'{def_utils.DEF_SCHEMA_DIRECTORY}.{keys_map[defKey.ENTITY_TYPE_SCHEMA_VERSION]}'
        )  # noqa: E501
        schema_file = pkg_resources.open_text(
            schema_module, def_utils.DEF_ENTITY_TYPE_SCHEMA_FILE)  # noqa: E501
        native_entity_type = def_models.\
            DefEntityType(name=keys_map[defKey.ENTITY_TYPE_NAME],
                          description='',
                          vendor=keys_map[defKey.VENDOR],
                          nss=keys_map[defKey.ENTITY_TYPE_NSS],
                          version=keys_map[defKey.ENTITY_TYPE_VERSION],
                          schema=json.load(schema_file),
                          interfaces=[native_interface.get_id()],
                          readonly=False)
        msg = ""
        try:
            schema_svc.get_entity_type(native_entity_type.get_id())
            msg = "defined entity type already exists." \
                  " Skipping defined entity type creation"
        except HTTPError:
            # TODO handle this part only if the entity type was not found
            native_entity_type = schema_svc.create_entity_type(
                native_entity_type)  # noqa: E501
            msg = "Successfully registered defined entity type"
        msg_update_callback.general(msg)
        INSTALL_LOGGER.debug(msg)
    except cse_exception.DefNotSupportedException:
        msg = "Skipping defined entity type and defined entity interface" \
              " registration"
        msg_update_callback.general(msg)
        INSTALL_LOGGER.debug(msg)
    except Exception as e:
        msg = f"Error occured while registering defined entity schema: {str(e)}"  # noqa: E501
        msg_update_callback.error(msg)
        INSTALL_LOGGER.error(msg)
        raise (e)
    finally:
        try:
            schema_file.close()
        except Exception:
            pass
 def __init__(self, client):
     self.client = client
     self.cloudapi_client = vcd_utils.get_cloudapi_client_from_vcd_client(
         client=client, logger_debug=CLIENT_LOGGER)