def get_org_name_from_ovdc_id(sysadmin_client: vcd_client.Client, vdc_id): """Get org_name from vdc_id using OVDC_TO_ORG_MAP. Update OVDC_TO_ORG_MAP for new {org_name:vdc_id} pair :param vdc_id: unique ovdc id :return: org_name :rtype: str """ raise_error_if_not_sysadmin(sysadmin_client) if vdc_id in OVDC_TO_ORG_MAP: return OVDC_TO_ORG_MAP.get(vdc_id) vdc_href = f"{sysadmin_client.get_api_uri()}/vdc/{vdc_id}" vdc_resource = sysadmin_client.get_resource(get_admin_href(vdc_href)) vdc_obj = VDC(sysadmin_client, resource=vdc_resource) link = vcd_client.find_link(vdc_obj.get_resource(), vcd_client.RelationType.UP, vcd_client.EntityType.ADMIN_ORG.value) org = vcd_org.Org(sysadmin_client, href=link.href) OVDC_TO_ORG_MAP[vdc_id] = org.get_name() return org.get_name()
def get_org_name_from_ovdc_id(vdc_id): """Get org_name from vdc_id using OVDC_TO_ORG_MAP. Update OVDC_TO_ORG_MAP for new {org_name:vdc_id} pair :param vdc_id: unique ovdc id :return: org_name :rtype: str """ if vdc_id in OVDC_TO_ORG_MAP: org_name = OVDC_TO_ORG_MAP.get(vdc_id) else: client = None try: client = get_sys_admin_client() vdc_href = f"{client._uri}/vdc/{vdc_id}" vdc_resource = client.get_resource(get_admin_href(vdc_href)) vdc_obj = VDC(client, resource=vdc_resource) link = find_link(vdc_obj.get_resource(), RelationType.UP, EntityType.ADMIN_ORG.value) org = Org(client, href=link.href) OVDC_TO_ORG_MAP[vdc_id] = org.get_name() org_name = org.get_name() finally: if client: client.logout return org_name
def __init__(self, client, name=None, href=None, resource=None): """Constructor for Gateway objects. :param pyvcloud.vcd.client.Client client: the client that will be used to make REST calls to vCD. :param str name: name of the entity. :param str href: URI of the entity. :param lxml.objectify.ObjectifiedElement resource: object containing EntityType.EDGE_GATEWAY XML data representing the gateway. """ self.client = client self.name = name if href is None: raise InvalidParameterException( "Gateway initialization failed as arguments are either " "invalid or None") self.href = href if resource is None: self.resource = None self.get_resource() self.resource = resource if resource is not None: self.name = resource.get('name') self.href = resource.get('href') self.href_admin = get_admin_href(self.href)
def get_org_name_href_from_ovdc_id(sysadmin_client: vcd_client.Client, vdc_id): """Get org name and href from vdc_id using OVDC_TO_ORG_MAP. Update OVDC_TO_ORG_MAP for new vdc_id :param vdc_id: unique ovdc id :return: org's name and href :rtype: dict """ raise_error_if_user_not_from_system_org(sysadmin_client) if vdc_id in OVDC_TO_ORG_MAP: return OVDC_TO_ORG_MAP.get(vdc_id) vdc_href = f"{sysadmin_client.get_api_uri()}/vdc/{vdc_id}" vdc_resource = sysadmin_client.get_resource(get_admin_href(vdc_href)) vdc_obj = VDC(sysadmin_client, resource=vdc_resource) link = vcd_client.find_link(vdc_obj.get_resource(), vcd_client.RelationType.UP, vcd_client.EntityType.ADMIN_ORG.value) org_href = link.href org = vcd_org.Org(sysadmin_client, href=org_href) org_name = org.get_name() result = {'name': org_name, 'href': org_href} OVDC_TO_ORG_MAP[vdc_id] = result return result
def get_metadata_value(self, key, domain=MetadataDomain.GENERAL, use_admin_endpoint=False): """Fetch a metadata value identified by the domain and key. :param str key: key of the value to be fetched. :param client.MetadataDomain domain: domain of the value to be fetched. :param bool use_admin_endpoint: if True, will use the /api/admin endpoint to retrieve the metadata value else will use the vanilla /api endpoint. :return: an object containing EntityType.METADATA_VALUE XML data which represents the metadata value corresponding to the provided key and domain. :rtype: lxml.objectify.ObjectifiedElement :raises: AccessForbiddenException: If there is no metadata entry corresponding to the key provided. """ if not isinstance(domain, MetadataDomain): raise InvalidParameterException('Invalid domain.') if not use_admin_endpoint: href = self.href else: href = get_admin_href(self.href) metadata_entry_href = \ f"{href}/{domain.value}/{key}" return self.client.get_resource(metadata_entry_href)
def __init__(self, client, name=None, href=None, resource=None): self.client = client self.name = name self.href = href self.resource = resource if resource is not None: self.name = resource.get('name') self.href = resource.get('href') self.href_admin = get_admin_href(self.href)
def __init__(self, client, name=None, href=None, resource=None): self.client = client self.name = name if href is None and resource is None: raise TypeError("VDC initialization failed as arguments" " are either invalid or None") self.href = href self.resource = resource if resource is not None: self.name = resource.get('name') self.href = resource.get('href') self.href_admin = get_admin_href(self.href)
def get_vdc(client, vdc_id=None, vdc_name=None, org=None, org_name=None, is_admin_operation=False): """Get the specified VDC object. Atleast one of vdc_id or vdc_name must be specified. If org or org_name both are not specified, the currently logged in user's org will be used to look for the vdc. :param pyvcloud.vcd.client.Client client: :param str vdc_id: id of the vdc :param str vdc_name: name of the vdc :param pyvcloud.vcd.org.Org org: specific org to use. :param str org_name: specific org to use if @org is not given. If None, uses currently logged-in org from @client. :param bool is_admin_operation: if set True, will return the admin view of the org vdc resource. :return: pyvcloud VDC object :rtype: pyvcloud.vcd.vdc.VDC :raises EntityNotFoundException: if the vdc could not be found. """ if vdc_id: base_url = client.get_api_uri() # add a trailing slash if missing if base_url[-1] != '/': base_url += '/' if is_admin_operation: base_url = get_admin_href(base_url) vdc_href = f'{base_url}vdc/{vdc_id}' vdc = VDC(client, href=vdc_href) vdc.reload() return vdc resource = None if vdc_name: if not org: org = get_org(client, org_name=org_name) resource = org.get_vdc(vdc_name, is_admin_operation=is_admin_operation) # TODO() org.get_vdc() should throw exception if vdc not found in the org. # This should be handled in pyvcloud. For now, it is handled here. if resource is None: raise EntityNotFoundException(f"VDC '{vdc_name}' not found in ORG " f"'{org.get_name()}'") return VDC(client, resource=resource)
def delete_org(self, org_name, force=None, recursive=None): """Delete an organization. :param org_name: (str): name of the org to be deleted. :param force: (bool): pass force=True along with recursive=True to remove an organization and any objects it contains, regardless of their state. :param recursive: (bool): pass recursive=True to remove an organization and any objects it contains that are in a state that normally allows removal. """ org = self.client.get_org_by_name(org_name) org_href = get_admin_href(org.get('href')) return self.client.delete_resource(org_href, force, recursive)
def delete_org(self, org_name, force=None, recursive=None): """Delete an organization. :param str org_name: name of the org to be deleted. :param bool force: pass force=True along with recursive=True to remove an organization and any objects it contains, regardless of their state. :param bool recursive: pass recursive=True to remove an organization and any objects it contains that are in a state that normally allows removal. """ org_resource = self.client.get_org_by_name(org_name) org_admin_href = get_admin_href(org_resource.get('href')) return self.client.delete_resource(org_admin_href, force, recursive)
def __init__(self, client, href=None, resource=None): """Constructor for a PVDC object. :param client: (pyvcloud.vcd.client): The client. :param href: (str): URI of the entity. :param resource: (lxml.objectify.ObjectifiedElement): XML representation of the entity. """ self.client = client self.href = href self.resource = resource if resource is not None: self.name = resource.get('name') self.href = resource.get('href') self.admin_resource = get_admin_href(self.href)
def convert_access_settings_list_to_params(self, access_settings_list): """Convert access settings from one format to other. Convert dictionary representation of access settings to AccessSettings XML element. Please refer to schema definition of EntityType.CONTROL_ACCESS_PARAMS for more details. :param list access_settings_list: list of dictionaries, where each dictionary represents a single access setting. The dictionary structure is as follows, - type: (str): type of the subject. One of 'org' or 'user'. - name: (str): name of the user or org. - access_level: (str): access_level of the particular subject. Allowed values are 'ReadOnly', 'Change' or 'FullControl'. :return: an object containing AccessSettings XML data. :rtype: lxml.objectify.ObjectifiedElement """ access_settings_params = E.AccessSettings() for access_setting in access_settings_list: if access_setting["type"] == 'user': org_href = self.get_org_href() subject_href = self.client.get_user_in_org( access_setting['name'], org_href).get('href') subject_type = EntityType.USER.value elif access_setting["type"] == 'org': subject_href = get_admin_href( self.client.get_org_by_name( access_setting['name']).get('href')) subject_type = EntityType.ADMIN_ORG.value else: raise InvalidParameterException("Invalid subject type") subject_name = access_setting['name'] # Make 'ReadOnly' the default access_level if it is not specified. if 'access_level' in access_setting: access_level = access_setting['access_level'] else: access_level = 'ReadOnly' access_setting_params = E.AccessSetting( E.Subject(name=subject_name, href=subject_href, type=subject_type), E.AccessLevel(access_level)) access_settings_params.append(access_setting_params) return access_settings_params
def convert_access_settings_list_to_params(self, access_settings_list): """Convert access settings from one format to other. Convert dictionary representation of access settings to AccessSettings XML element. Please refer to schema definition of EntityType.CONTROL_ACCESS_PARAMS for more details. :param list access_settings_list: list of dictionaries, where each dictionary represents a single access setting. The dictionary structure is as follows, - type: (str): type of the subject. One of 'org' or 'user'. - name: (str): name of the user or org. - access_level: (str): access_level of the particular subject. Allowed values are 'ReadOnly', 'Change' or 'FullControl'. :return: an object containing AccessSettings XML data. :rtype: lxml.objectify.ObjectifiedElement """ access_settings_params = E.AccessSettings() for access_setting in access_settings_list: if access_setting["type"] == 'user': org_href = self.get_org_href() subject_href = self.client.get_user_in_org( access_setting['name'], org_href).get('href') subject_type = EntityType.USER.value elif access_setting["type"] == 'org': subject_href = get_admin_href( self.client.get_org_by_name( access_setting['name']).get('href')) subject_type = EntityType.ADMIN_ORG.value else: raise InvalidParameterException("Invalid subject type") subject_name = access_setting['name'] # Make 'ReadOnly' the default access_level if it is not specified. if 'access_level' in access_setting: access_level = access_setting['access_level'] else: access_level = 'ReadOnly' access_setting_params = E.AccessSetting( E.Subject( name=subject_name, href=subject_href, type=subject_type), E.AccessLevel(access_level)) access_settings_params.append(access_setting_params) return access_settings_params
def get_all_metadata(self, use_admin_endpoint=False): """Fetch all metadata entries of the parent object. :param bool use_admin_endpoint: if True, will use the /api/admin endpoint to retrieve the metadata object else will use the vanilla /api endpoint. :return: an object containing EntityType.METADATA XML data which represents the metadata entries associated with parent object. :rtype: lxml.objectify.ObjectifiedElement """ if not use_admin_endpoint: return self.get_resource() else: admin_href = get_admin_href(self.href) return self.client.get_resource(admin_href)
def __init__(self, client, href=None, resource=None): """Constructor for a PVDC object. :param client: (pyvcloud.vcd.client): The client. :param href: (str): URI of the entity. :param resource: (lxml.objectify.ObjectifiedElement): XML representation of the entity. """ self.client = client if href is None and resource is None: raise TypeError("PVDC initialization failed as arguments" " are either invalid or None") self.href = href self.resource = resource if resource is not None: self.name = resource.get('name') self.href = resource.get('href') self.admin_resource = get_admin_href(self.href)
def convert_access_settings_list_to_params(self, access_settings_list): """Convert access_settings_list to object of type AccessSettingsType :param access_settings_list: (list of dict): list of access_setting in the dict format. Each dict contains: type: (str): type of the subject. One of 'org' or 'user' name: (str): subject name access_level: (str): access_level of each subject. One of 'ReadOnly', 'Change', 'FullControl'. :return: A :class:`lxml.objectify.StringElement` object representing xml of type AccessSettingsType """ access_settings_params = E.AccessSettings() for access_setting in access_settings_list: if access_setting["type"] == 'user': org_href = find_link(self.parent_resource, RelationType.UP, EntityType.ORG.value).href subject_href = self.client.get_user_in_org( access_setting['name'], org_href).get('href') subject_type = EntityType.USER.value elif access_setting["type"] == 'org': subject_href = get_admin_href( self.client.get_org_by_name( access_setting['name']).get('href')) subject_type = EntityType.ADMIN_ORG.value else: raise Exception("Invalid subject type") subject_name = access_setting['name'] # Make 'ReadOnly' the default access_level if it is not specified. if 'access_level' in access_setting: access_level = access_setting['access_level'] else: access_level = 'ReadOnly' access_setting_params = E.AccessSetting( E.Subject(name=subject_name, href=subject_href, type=subject_type), E.AccessLevel(access_level)) access_settings_params.append(access_setting_params) return access_settings_params
def __init__(self, client, href=None, resource=None): """Constructor for a PVDC object. :param pyvcloud.vcd.client.Client client: the client that will be used to make REST calls to vCD. :param str href: URI of the entity. :param lxml.objectify.ObjectifiedElement resource: object containing EntityType.PROVIDER_VDC XML data representing the provider vdc. """ self.client = client if href is None and resource is None: raise InvalidParameterException( "PVDC initialization failed as arguments are either invalid " "or None") self.href = href self.resource = resource if resource is not None: self.name = resource.get('name') self.href = resource.get('href') self.admin_resource = get_admin_href(self.href)
def convert_access_settings_list_to_params(self, access_settings_list): """Convert access_settings_list to object of type AccessSettingsType. :param access_settings_list: (list of dict): list of access_setting in the dict format. Each dict contains: type: (str): type of the subject. One of 'org' or 'user' name: (str): subject name access_level: (str): access_level of each subject. One of 'ReadOnly', 'Change', 'FullControl'. :return: A :class:`lxml.objectify.StringElement` object representing xml of type AccessSettingsType """ access_settings_params = E.AccessSettings() for access_setting in access_settings_list: if access_setting["type"] == 'user': org_href = self.get_org_href() subject_href = self.client.get_user_in_org( access_setting['name'], org_href).get('href') subject_type = EntityType.USER.value elif access_setting["type"] == 'org': subject_href = get_admin_href( self.client.get_org_by_name( access_setting['name']).get('href')) subject_type = EntityType.ADMIN_ORG.value else: raise Exception("Invalid subject type") subject_name = access_setting['name'] # Make 'ReadOnly' the default access_level if it is not specified. if 'access_level' in access_setting: access_level = access_setting['access_level'] else: access_level = 'ReadOnly' access_setting_params = E.AccessSetting( E.Subject( name=subject_name, href=subject_href, type=subject_type), E.AccessLevel(access_level)) access_settings_params.append(access_setting_params) return access_settings_params
def __init__(self, client, name=None, href=None, resource=None): """Constructor for External Network objects. :param pyvcloud.vcd.client.Client client: the client that will be used to make REST calls to vCD. :param str name: name of the entity. :param str href: URI of the entity. :param lxml.objectify.ObjectifiedElement resource: object containing EntityType.EXTERNAL_NETWORK XML data representing the external network. """ self.client = client self.name = name if href is None and resource is None: raise InvalidParameterException( "External network initialization failed as arguments are " "either invalid or None") self.href = href self.resource = resource if resource is not None: self.name = resource.get('name') self.href = resource.get('href') self.href_admin = get_admin_href(self.href)
def get_org_name_of_ovdc(vdc_id): """Get org_name from vdc_id using OVDC_TO_ORG_MAP. Update OVDC_TO_ORG_MAP for new {org_name:vdc_id} pair :param vdc_id: unique ovdc id :return: org_name """ if vdc_id in OVDC_TO_ORG_MAP: org_name = OVDC_TO_ORG_MAP.get(vdc_id) else: client = get_vcd_sys_admin_client() vdc_href = f"{client._uri}/vdc/{vdc_id}" vdc_resource = client.get_resource(get_admin_href(vdc_href)) vdc_obj = VDC(client, resource=vdc_resource) link = find_link(vdc_obj.resource, RelationType.UP, EntityType.ADMIN_ORG.value) org = Org(client, href=link.href) '''Add the entry to the map to be used next time the \ same ovdc is requested''' OVDC_TO_ORG_MAP[vdc_id] = org.get_name() org_name = org.get_name() return org_name