def set_ovdc_container_provider_metadata(self, ovdc, container_prov_data=None, container_provider=None): """Set the container provider metadata of given ovdc. :param resource ovdc: vdc resource :param dict container_prov_data: container provider context details :param str container_provider: name of container provider for which the ovdc is being enabled to deploy k8 clusters on. """ ovdc_name = ovdc.resource.get('name') metadata = {} if container_provider != K8sProviders.PKS: LOGGER.debug(f"Remove existing metadata for ovdc:{ovdc_name}") self._remove_metadata(ovdc, PksCache.get_pks_keys()) metadata[K8S_PROVIDER_KEY] = container_provider or \ K8sProviders.NONE LOGGER.debug(f"Updated metadata for {container_provider}:" f"{metadata}") else: container_prov_data.pop('username') container_prov_data.pop('secret') container_prov_data.pop('nsxt') metadata[K8S_PROVIDER_KEY] = container_provider metadata.update(container_prov_data) # set ovdc metadata into Vcd LOGGER.debug(f"On ovdc:{ovdc_name}, setting metadata:{metadata}") return ovdc.set_multiple_metadata(metadata, MetadataDomain.SYSTEM, MetadataVisibility.PRIVATE)
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)
def get_ovdc_container_provider_metadata(self, ovdc_name=None, ovdc_id=None, org_name=None, credentials_required=False, nsxt_info_required=False): """Get metadata of given ovdc, pertaining to the container provider. :param str ovdc_name: name of the ovdc :param str ovdc_id: UUID of ovdc :param str org_name: specific org to use if @org is not given. If None, uses currently logged-in org from @client. :param bool credentials_required: Decides if output metadata should include credentials or not. :return: metadata of the ovdc :rtype: dict :raises EntityNotFoundException: if the ovdc could not be found. """ # Get pvdc and pks information from oVdc metadata ovdc = self.get_ovdc(ovdc_name, ovdc_id, org_name) all_metadata = utils.metadata_to_dict(ovdc.get_all_metadata()) if CONTAINER_PROVIDER_KEY not in all_metadata: container_provider = CtrProvType.NONE.value else: container_provider = \ all_metadata[CONTAINER_PROVIDER_KEY] ctr_prov_details = {} if container_provider == CtrProvType.PKS.value: # Filter out container provider metadata into a dict ctr_prov_details = { metadata_key: all_metadata[metadata_key] for metadata_key in PksCache.get_pks_keys() } # Get the credentials from PksCache pvdc_id = self.get_pvdc_id(ovdc) pvdc_info = self.pks_cache.get_pvdc_info(pvdc_id) ctr_prov_details[PKS_PLANS] = \ ctr_prov_details[PKS_PLANS].split(',') if credentials_required: pks_info = self.pks_cache.get_pks_account_info( org_name, pvdc_info.vc) ctr_prov_details.update(pks_info.credentials._asdict()) if nsxt_info_required: nsxt_info = self.pks_cache.get_nsxt_info(pvdc_info.vc) ctr_prov_details['nsxt'] = nsxt_info ctr_prov_details[CONTAINER_PROVIDER_KEY] = container_provider return ctr_prov_details
def get_ovdc_k8s_provider_metadata(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 str org_name: :param str ovdc_name: :param str ovdc_id: :param bool include_credentials: :param bool include_nsxt_info: :return: Dictionary with k8s provider metadata :rtype: Dict """ client = None try: client = vcd_utils.get_sys_admin_client() ovdc = vcd_utils.get_vdc(client=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(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 finally: if client is not None: client.logout()
def update_ovdc_k8s_provider_metadata(ovdc_id, k8s_provider_data=None, k8s_provider=None): """Set the k8s provider metadata of given ovdc. :param str ovdc_id: :param dict k8s_provider_data: k8s provider context details :param K8sProvider k8s_provider: name of k8s provider for which the ovdc is being enabled to deploy k8 clusters on. """ client = None try: client = vcd_utils.get_sys_admin_client() ovdc = vcd_utils.get_vdc(client, vdc_id=ovdc_id) ovdc_name = ovdc.get_resource().get('name') metadata = {} 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, MetadataDomain.SYSTEM, MetadataVisibility.PRIVATE) finally: if client: client.logout()
def get_ovdc_container_provider_metadata(self, ovdc_name=None, ovdc_id=None, org_name=None, credentials_required=False, nsxt_info_required=False): """Get metadata of given ovdc, pertaining to the container provider. :param str ovdc_name: name of the ovdc :param str ovdc_id: UUID of ovdc :param str org_name: specific org to use if @org is not given. If None, uses currently logged-in org from @client. :param bool credentials_required: Decides if output metadata should include credentials or not. :return: metadata of the ovdc :rtype: dict :raises EntityNotFoundException: if the ovdc could not be found. """ # Get pvdc and pks information from oVdc metadata client = None try: client = get_sys_admin_client() ovdc = get_vdc(client=client, vdc_name=ovdc_name, vdc_id=ovdc_id, org_name=org_name, is_admin_operation=True) all_metadata = metadata_to_dict(ovdc.get_all_metadata()) if K8S_PROVIDER_KEY not in all_metadata: container_provider = K8sProviders.NONE else: container_provider = all_metadata[K8S_PROVIDER_KEY] ctr_prov_details = {} if container_provider == K8sProviders.PKS: # Filter out container provider metadata into a dict ctr_prov_details = { metadata_key: all_metadata[metadata_key] for metadata_key in PksCache.get_pks_keys() } # Get the credentials from PksCache pvdc_id = get_pvdc_id(ovdc) pks_cache = get_pks_cache() pvdc_info = pks_cache.get_pvdc_info(pvdc_id) ctr_prov_details[PKS_PLANS_KEY] = \ ctr_prov_details[PKS_PLANS_KEY].split(',') if credentials_required: pks_info = pks_cache.get_pks_account_info( org_name, pvdc_info.vc) ctr_prov_details.update(pks_info.credentials._asdict()) if nsxt_info_required: nsxt_info = pks_cache.get_nsxt_info(pvdc_info.vc) ctr_prov_details['nsxt'] = nsxt_info ctr_prov_details[K8S_PROVIDER_KEY] = container_provider return ctr_prov_details finally: if client: client.logout()