def update_ovdc_k8s_provider_metadata(sysadmin_client: vcd_client.Client,
                                      ovdc_id,
                                      k8s_provider_data=None,
                                      k8s_provider=None):
    """Set the k8s provider metadata for given ovdc.

    :param pyvcloud.vcd.client.Client sysadmin_client:
    :param str ovdc_id:
    :param dict k8s_provider_data:  k8s provider context details
    :param K8sProvider k8s_provider:
    :return:
    """
    vcd_utils.raise_error_if_not_sysadmin(sysadmin_client)

    ovdc = vcd_utils.get_vdc(sysadmin_client, vdc_id=ovdc_id)
    ovdc_name = ovdc.get_resource().get('name')
    metadata = {K8S_PROVIDER_KEY: k8s_provider or K8sProvider.NONE}

    if k8s_provider != K8sProvider.PKS:
        LOGGER.debug(f"Remove existing metadata for ovdc:{ovdc_name}")
        _remove_metadata_from_ovdc(ovdc, PksCache.get_pks_keys())
        LOGGER.debug(f"Updated metadata for {k8s_provider}:" f"{metadata}")
    else:
        k8s_provider_data.pop('username')
        k8s_provider_data.pop('secret')
        k8s_provider_data.pop('nsxt')
        metadata.update(k8s_provider_data)

    # set ovdc metadata into Vcd
    LOGGER.debug(f"On ovdc:{ovdc_name}, setting metadata:{metadata}")
    return ovdc.set_multiple_metadata(metadata,
                                      vcd_client.MetadataDomain.SYSTEM,
                                      vcd_client.MetadataVisibility.PRIVATE)
示例#2
0
    def __init__(self, sysadmin_client: vcd_client.Client, log_wire=True):
        vcd_utils.raise_error_if_not_sysadmin(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)
            # 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=cloudapi_constants.CLOUDAPI_VERSION_1_0_0,
                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
示例#3
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 get_ovdc_k8s_provider_metadata(sysadmin_client: vcd_client.Client,
                                   org_name=None,
                                   ovdc_name=None,
                                   ovdc_id=None,
                                   include_credentials=False,
                                   include_nsxt_info=False):
    """Get k8s provider metadata for an org VDC.

    :param sysadmin_client:
    :param org_name:
    :param ovdc_name:
    :param ovdc_id:
    :param include_credentials:
    :param include_nsxt_info:
    :return:  Dictionary with k8s provider metadata
    """
    vcd_utils.raise_error_if_not_sysadmin(sysadmin_client)

    ovdc = vcd_utils.get_vdc(client=sysadmin_client,
                             vdc_name=ovdc_name,
                             vdc_id=ovdc_id,
                             org_name=org_name,
                             is_admin_operation=True)

    all_metadata = pyvcd_utils.metadata_to_dict(ovdc.get_all_metadata())
    k8s_provider = all_metadata.get(K8S_PROVIDER_KEY, K8sProvider.NONE)

    result = {K8S_PROVIDER_KEY: k8s_provider}

    if k8s_provider == K8sProvider.PKS:
        result.update({k: all_metadata[k]
                       for k in PksCache.get_pks_keys()})  # noqa: E501
        result[PKS_PLANS_KEY] = result[PKS_PLANS_KEY].split(',')

        # Get the credentials from PksCache
        if include_credentials or include_nsxt_info:
            pks_cache = utils.get_pks_cache()
            pvdc_info = pks_cache.get_pvdc_info(
                vcd_utils.get_pvdc_id(sysadmin_client, ovdc))
            if include_credentials:
                # noqa: E501 TODO in case only ovdc_id is provided, we need a way to get org_name
                pks_info = pks_cache.get_pks_account_info(
                    org_name, pvdc_info.vc)
                result.update(pks_info.credentials._asdict())
            if include_nsxt_info:
                nsxt_info = pks_cache.get_nsxt_info(pvdc_info.vc)
                result['nsxt'] = nsxt_info

    return result
def _construct_pks_compute_profile_name(sysadmin_client: vcd_client.Client,
                                        vdc_id):
    """Construct pks compute profile name.

    :param pyvcloud.vcd.client.Client sysadmin_client:
    :param str vdc_id: UUID of the vdc in vcd

    :return: pks compute profile name

    :rtype: str
    """
    vcd_utils.raise_error_if_not_sysadmin(sysadmin_client)

    vdc = vcd_utils.get_vdc(client=sysadmin_client, vdc_id=vdc_id)
    return f"cp--{vdc_id}--{vdc.name}"
    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)
def get_ovdc_k8s_runtime_details(sysadmin_client: vcd_client.Client,
                                 ovdc_id=None,
                                 ovdc_name=None,
                                 org_name=None,
                                 log_wire=False) -> def_models.Ovdc:
    """Get k8s runtime details for an ovdc.

    Atleast ovdc_id and ovdc_name or org_name and ovdc_name should be provided.
    Additional call to get ovdc details can be avoided by providing ovdc_id and
    ovdc_name.

    :param sysadmin_client vcd_client.Client: vcd sysadmin client
    :param str org_name:
    :param str ovdc_name:
    :param str ovdc_id:
    :param bool log_wire:
    :return: Ovdc object with k8s runtimes
    :rtype: def_models.Ovdc
    """
    vcd_utils.raise_error_if_not_sysadmin(sysadmin_client)
    cpm = compute_policy_manager.ComputePolicyManager(sysadmin_client,
                                                      log_wire=log_wire)
    if not (org_name and ovdc_name) and not ovdc_id:
        msg = "Unable to fetch OVDC k8 runtime details with the " \
              "provided parameters"
        logger.SERVER_LOGGER.error(msg)
        raise Exception(msg)
    if not ovdc_id or not ovdc_name:
        # populate ovdc_id and ovdc_name
        ovdc = vcd_utils.get_vdc(client=sysadmin_client,
                                 vdc_id=ovdc_id,
                                 vdc_name=ovdc_name,
                                 org_name=org_name,
                                 is_admin_operation=True)
        ovdc_id = vcd_utils.extract_id(ovdc.get_resource().get('id'))
        ovdc_name = ovdc.get_resource().get('name')
    policies = []
    for cse_policy in \
            compute_policy_manager.list_cse_placement_policies_on_vdc(cpm, ovdc_id):  # noqa: E501
        policies.append(RUNTIME_INTERNAL_NAME_TO_DISPLAY_NAME_MAP[cse_policy['display_name']])  # noqa: E501
    return def_models.Ovdc(ovdc_name=ovdc_name, ovdc_id=ovdc_id, k8s_runtime=policies) # noqa: E501
def construct_k8s_metadata_from_pks_cache(sysadmin_client: vcd_client.Client,
                                          ovdc_id, org_name, pks_plans,
                                          pks_cluster_domain, k8s_provider):
    vcd_utils.raise_error_if_not_sysadmin(sysadmin_client)

    ctr_prov_context = {
        K8S_PROVIDER_KEY: k8s_provider,
    }
    if k8s_provider == K8sProvider.PKS:
        if not utils.is_pks_enabled():
            raise e.CseServerError('CSE is not configured to work with PKS.')

        ovdc = vcd_utils.get_vdc(client=sysadmin_client,
                                 vdc_id=ovdc_id,
                                 is_admin_operation=True)
        pks_cache = utils.get_pks_cache()
        pvdc_id = vcd_utils.get_pvdc_id(sysadmin_client, ovdc)
        pvdc_info = pks_cache.get_pvdc_info(pvdc_id)
        if not pvdc_info:
            LOGGER.debug(f"pvdc '{pvdc_id}' is not backed "
                         f"by PKS-managed-vSphere resources")
            raise e.CseServerError(f"VDC '{ovdc.get_resource().get('name')}'"
                                   " is not eligible to provide resources"
                                   " for PKS clusters.")
        pks_account_info = pks_cache.get_pks_account_info(
            org_name, pvdc_info.vc)
        nsxt_info = pks_cache.get_nsxt_info(pvdc_info.vc)

        pks_compute_profile_name = _construct_pks_compute_profile_name(
            sysadmin_client, ovdc_id)
        ctr_prov_context = construct_pks_context(
            pks_account_info=pks_account_info,
            pvdc_info=pvdc_info,
            nsxt_info=nsxt_info,
            pks_compute_profile_name=pks_compute_profile_name,
            pks_plans=pks_plans,
            pks_cluster_domain=pks_cluster_domain,
            credentials_required=True)
    return ctr_prov_context