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 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}")
Ejemplo n.º 3
0
 def __init__(self, cluster_id: str, client: vcd_client.Client):
     self._client = client
     self._cloudapi_client = \
         vcd_utils.get_cloudapi_client_from_vcd_client(client)
     self._cluster_id = cluster_id
     self._def_entity: Optional[common_models.DefEntity] = None
     self._vapp: Optional[vcd_vapp.VApp] = None
 def __init__(self,
              entity_type_id: str,
              client: vcd_client.Client,
              logger_debug: logging.Logger = NULL_LOGGER,
              logger_wire: logging.Logger = NULL_LOGGER):
     self._client = client
     self._cloudapi_client = vcd_utils.get_cloudapi_client_from_vcd_client(
         client=client, logger_debug=logger_debug, logger_wire=logger_wire)
     self._entity_type_id = entity_type_id
     self._logger_debug = logger_debug
     self._logger_wire = logger_wire
 def __init__(self,
              cluster_id: str,
              client: vcd_client.Client,
              logger_debug: logging.Logger = NULL_LOGGER,
              logger_wire: logging.Logger = NULL_LOGGER):
     self._client = client
     self._cloudapi_client = vcd_utils.get_cloudapi_client_from_vcd_client(
         client=client, logger_debug=logger_debug, logger_wire=logger_wire)
     self._cluster_id = cluster_id
     self._def_entity: Optional[common_models.DefEntity] = None
     self._vapp: Optional[vcd_vapp.VApp] = None
 def __init__(self,
              sysadmin_client: vcd_client.Client,
              log_wire=False,
              logger_debug=NULL_LOGGER):
     vcd_utils.raise_error_if_user_not_from_system_org(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)
Ejemplo n.º 7
0
 def cloudapi_client(self):
     if self._cloudapi_client is None:
         log_wire = server_utils.get_server_runtime_config().get(
             'service', {}).get('log_wire', False)  # noqa: E501
         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,
         logger_debug=NULL_LOGGER,
         logger_wire=NULL_LOGGER
 ):
     vcd_utils.raise_error_if_user_not_from_system_org(sysadmin_client)
     self.cloudapi_client = vcd_utils.get_cloudapi_client_from_vcd_client(
         sysadmin_client,
         logger_debug=logger_debug,
         logger_wire=logger_wire
     )
Ejemplo n.º 9
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)
     self._client = client
     self._server_rde_version = \
         def_utils.get_runtime_rde_version_by_vcd_api_version(
             float(client.get_api_version()))
 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)
     self._client = client
     schema_service = def_schema_svc.DefSchemaService(self._cloudapi_client)
     self._server_rde_version = \
         schema_service.get_latest_registered_schema_version()
Ejemplo n.º 11
0
    def __init__(self, gateway: vcd_gateway.Gateway,
                 client: vcd_client.Client):
        assert gateway.is_nsxt_backed()

        self._gateway = gateway
        self._client = client
        self._cloudapi_client = \
            pyvcloud_utils.get_cloudapi_client_from_vcd_client(client)
        gateway_id = core_utils.extract_id_from_href(self._gateway.href)
        self._gateway_urn = f'{nsxt_constants.GATEWAY_URN_PREFIX}:' \
                            f'{gateway_id}'
        self._gateway_relative_path = \
            f'{cloudapi_constants.CloudApiResource.EDGE_GATEWAYS}/' \
            f'{self._gateway_urn}'
def _get_gateway(
    client: vcd_client.Client,
    org_name: str,
    network_name: str,
):
    config = server_utils.get_server_runtime_config()
    logger_wire = NULL_LOGGER
    if utils.str_to_bool(config.get_value_at('service.log_wire')):
        logger_wire = SERVER_CLOUDAPI_WIRE_LOGGER
    cloudapi_client = vcd_utils.get_cloudapi_client_from_vcd_client(
        client=client, logger_debug=LOGGER, logger_wire=logger_wire)

    gateway_name, gateway_href, gateway_exists = None, None, False
    page, page_count = 1, 1
    base_path = f'{cloudapi_constants.CloudApiResource.ORG_VDC_NETWORKS}?filter=name=={network_name};_context==includeAccessible&pageSize=1&page='  # noqa: E501

    while page <= page_count:
        response, headers = cloudapi_client.do_request(
            method=RequestMethod.GET,
            cloudapi_version=cloudapi_constants.CloudApiVersion.VERSION_1_0_0,
            resource_url_relative_path=base_path + f'{page}',
            return_response_headers=True)
        for entry in response['values']:
            # only routed networks allowed
            is_target_network = entry['orgRef']['name'] == org_name and \
                entry['networkType'] == 'NAT_ROUTED'
            if is_target_network:
                if gateway_exists:
                    raise MultipleRecordsException(
                        f"Multiple routed networks named {network_name} found. CSE Server expects unique network names."
                    )  # noqa: E501
                gateway_exists = True
                gateway_name = entry['connection']['routerRef']['name']
                gateway_id = entry['connection']['routerRef']['id'].split(
                    ':').pop()  # noqa: E501
                gateway_href = headers['Content-Location'].split('cloudapi')[
                    0] + f'api/admin/edgeGateway/{gateway_id}'  # noqa: E501
        page += 1
        page_count = response['pageCount']

    if not gateway_exists:
        raise EntityNotFoundException(
            f"No routed networks named {network_name} found.")  # noqa: E501

    gateway = vcd_gateway.Gateway(client, name=gateway_name, href=gateway_href)
    return gateway
    def _update_user_context_map(self, api_version: Optional[str]):
        _client = vcd_utils.connect_vcd_user_via_token(
            tenant_auth_token=self._auth_token,
            is_jwt_token=self._is_jwt,
            api_version=api_version)

        log_wire = server_utils.get_server_runtime_config() \
            .get('service', {}).get('log_wire', False)  # noqa: E501
        logger_wire = logger.NULL_LOGGER
        if log_wire:
            logger_wire = logger.SERVER_CLOUDAPI_WIRE_LOGGER
        _cloudapi_client = vcd_utils.get_cloudapi_client_from_vcd_client(
            _client, logger.SERVER_LOGGER, logger_wire)

        _user_context = user_context.UserContext(
            client=_client, cloud_api_client=_cloudapi_client)
        self._user_context_map[api_version] = _user_context
Ejemplo n.º 14
0
 def __init__(self, client):
     logger_wire = logger.CLIENT_WIRE_LOGGER \
         if core_utils.str_to_bool(
             os.getenv(cli_constants.ENV_CSE_CLIENT_WIRE_LOGGING)
         ) \
         else logger.NULL_LOGGER
     self._client = client
     self._cloudapi_client = \
         vcd_utils.get_cloudapi_client_from_vcd_client(
             client=client,
             logger_debug=logger.CLIENT_LOGGER,
             logger_wire=logger_wire
         )
     self._nativeCluster = DEClusterNative(client)
     self._tkgCluster = DEClusterTKGS(client)
     schema_svc = def_schema_svc.DefSchemaService(self._cloudapi_client)
     self._server_rde_version = \
         schema_svc.get_latest_registered_schema_version()
    def _update_sysadmin_user_context_map(self, api_version: Optional[str]):
        _sysadmin_client = vcd_utils.get_sys_admin_client(
            api_version=api_version)

        log_wire = server_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
        _sysadmin_cloudapi_client = \
            vcd_utils.get_cloudapi_client_from_vcd_client(
                _sysadmin_client,
                logger.SERVER_LOGGER,
                logger_wire)

        _sysadmin_user_context = user_context.UserContext(
            client=_sysadmin_client,
            cloud_api_client=_sysadmin_cloudapi_client)
        self._sysadmin_user_context_map[api_version] = _sysadmin_user_context
Ejemplo n.º 16
0
def rde_exists(rde_name, logger=NULL_LOGGER):
    try:
        cloudapi_client: CloudApiClient = \
            pyvcloud_utils.get_cloudapi_client_from_vcd_client(
                CLIENT,
                logger_debug=logger,
                logger_wire=SERVER_CLOUDAPI_WIRE_LOGGER)
        # To avoid cyclic import
        from container_service_extension.rde.common.entity_service import \
            DefEntityService
        entity_svc = DefEntityService(cloudapi_client)
        rde_version = rde_utils.get_runtime_rde_version_by_vcd_api_version(
            CLIENT.get_api_version())
        entity = \
            entity_svc.get_native_rde_by_name_and_rde_version(
                rde_name, rde_version)
        return bool(entity)
    except Exception as e:
        logger.warning(f"Exception occured when checking if rde exists: {e}")
        return False
Ejemplo n.º 17
0
def delete_rde(cluster_name, logger=NULL_LOGGER):
    """Delete defined entity with the given name.

    NOTE: RDE names are not unique. This function deletes all occurances
    """
    try:
        cloudapi_client: CloudApiClient = \
            pyvcloud_utils.get_cloudapi_client_from_vcd_client(
                CLIENT, logger_debug=logger, logger_wire=SERVER_CLOUDAPI_WIRE_LOGGER)  # noqa: E501
        from container_service_extension.rde.common.entity_service import DefEntityService  # noqa: E501
        entity_svc = DefEntityService(cloudapi_client)
        for cluster_rde in \
            entity_svc.list_all_native_rde_by_name_and_rde_version(
                cluster_name,
                rde_utils.get_runtime_rde_version_by_vcd_api_version(CLIENT.get_api_version())):  # noqa: E501
            entity_svc.resolve_entity(cluster_rde.id, cluster_rde.entityType)
            entity_svc.delete_entity(cluster_rde.id)
    except cse_exceptions.DefEntityServiceError as e:
        PYTEST_LOGGER.error(f"Failed to delete RDE: {e}")
        pass
    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_user_not_from_system_org(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.º 19
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()