def get_org_id_from_vdc_name(client: vcd_client.Client, vdc_name: str):
    """Return org id given vdc name.

    :param vcd_client.Client client: vcd client
    :param str vdc_name: vdc name

    :return: org id, with no prefix, e.g., '12345'
    :rtype: str
    """
    if client.is_sysadmin():
        resource_type = vcd_client.ResourceType.ADMIN_ORG_VDC.value
    else:
        resource_type = vcd_client.ResourceType.ORG_VDC.value
    query = client.get_typed_query(
        query_type_name=resource_type,
        query_result_format=vcd_client.QueryResultFormat.ID_RECORDS,
        equality_filter=('name', vdc_name))
    records = list(query.execute())
    if len(records) == 0:
        return None

    # Process org id
    if client.is_sysadmin():
        org_urn_id = records[0].attrib['org']
    else:
        org_name = records[0].attrib['orgName']
        org_resource = client.get_org_by_name(org_name)
        org_urn_id = org_resource.attrib['id']
    return extract_id(org_urn_id)
    def __init__(self,
                 vcd_api_client: vcd_client.Client,
                 oauth_client_name: str,
                 logger_debug: logging.Logger = NULL_LOGGER,
                 logger_wire: logging.Logger = NULL_LOGGER):

        self.vcd_api_client: vcd_client.Client = vcd_api_client
        cloudapi_url = vcd_api_client.get_cloudapi_uri()

        super().__init__(base_url=cloudapi_url,
                         token=vcd_api_client.get_access_token(),
                         api_version=vcd_api_client.get_api_version(),
                         logger_debug=logger_debug,
                         logger_wire=logger_wire,
                         verify_ssl=vcd_api_client._verify_ssl_certs,
                         is_sys_admin=vcd_api_client.is_sysadmin())

        self.oauth_client_name = oauth_client_name

        # cloudapi_url will be of the format https://vcd-host/cloudapi
        # since /oauth endpoint is not associated with /cloudapi or /api,
        # we need to format the cloudapi_url so that only https://vcd-host
        # part is retained
        url_host = urlparse(cloudapi_url)
        self._host_url = f"{url_host.scheme}://{url_host.netloc}"

        self.oauth_client_id = None
        self.refresh_token = None
def get_all_ovdcs(client: vcd_client.Client):
    if client.is_sysadmin():
        # use adminOrgVdc in typed query
        query = client.get_typed_query(
            vcd_client.ResourceType.ADMIN_ORG_VDC.value,
            query_result_format=vcd_client.QueryResultFormat.ID_RECORDS)
    else:
        # use orgVdc in typed query
        query = client.get_typed_query(
            vcd_client.ResourceType.ORG_VDC.value,
            query_result_format=vcd_client.QueryResultFormat.ID_RECORDS)
    return list(query.execute())
Exemple #4
0
def get_cloudapi_client_from_vcd_client(client: vcd_client.Client,
                                        logger_debug=NULL_LOGGER,
                                        logger_wire=NULL_LOGGER):
    token = client.get_access_token()
    return cloud_api_client.CloudApiClient(
        base_url=client.get_cloudapi_uri(),
        token=token,
        api_version=client.get_api_version(),
        logger_debug=logger_debug,
        logger_wire=logger_wire,
        verify_ssl=client._verify_ssl_certs,
        is_sys_admin=client.is_sysadmin())
Exemple #5
0
def get_cloudapi_client_from_vcd_client(client: vcd_client.Client,
                                        logger_debug=NULL_LOGGER,
                                        logger_wire=NULL_LOGGER):
    token = client.get_access_token()
    is_jwt = True
    if not token:
        token = client.get_xvcloud_authorization_token()
        is_jwt = False
    return cloudApiClient.CloudApiClient(base_url=client.get_cloudapi_uri(),
                                         token=token,
                                         is_jwt_token=is_jwt,
                                         api_version=client.get_api_version(),
                                         logger_debug=logger_debug,
                                         logger_wire=logger_wire,
                                         verify_ssl=client._verify_ssl_certs,
                                         is_sys_admin=client.is_sysadmin())
Exemple #6
0
def get_ovdcs_by_page(client: vcd_client.Client,
                      page=CSE_PAGINATION_FIRST_PAGE_NUMBER,
                      page_size=CSE_PAGINATION_DEFAULT_PAGE_SIZE):
    """Get the list of ovdcs in the page."""
    query = None
    if client.is_sysadmin():
        # use adminOrgVdc in typed query
        query = client.get_typed_query(
            vcd_client.ResourceType.ADMIN_ORG_VDC.value,
            page=page,
            page_size=page_size,
            query_result_format=vcd_client.QueryResultFormat.ID_RECORDS)
    else:
        # use orgVdc in typed query
        query = client.get_typed_query(
            vcd_client.ResourceType.ORG_VDC.value,
            page=page,
            page_size=page_size,
            query_result_format=vcd_client.QueryResultFormat.ID_RECORDS)
    vdc_results = query.execute()
    return vdc_results
Exemple #7
0
def raise_error_if_user_not_from_system_org(client: vcd_client.Client):
    if not client.is_sysadmin():
        raise ValueError("Client should be sysadmin.")
def raise_error_if_user_not_from_system_org(client: vcd_client.Client):
    # ToDo: current implementation of client.is_sysadmin checks if
    # org of the user is System or not. If implementation changes,
    # we should adapt accordingly.
    if not client.is_sysadmin():
        raise ValueError("Client does not belong to System Org.")
Exemple #9
0
def raise_error_if_not_sysadmin(client: vcd_client.Client):
    if not client.is_sysadmin():
        raise ValueError("Client should be sysadmin.")
Exemple #10
0
# Update with valid vCD's URL or IP address
client = Client(os.environ['VCD_IP'],
                api_version='31.0',
                verify_ssl_certs=False,
                log_file='pyvcloud.log',
                log_requests=True,
                log_headers=True,
                log_bodies=True)

# Update with valid administrator's credentials
client.set_credentials(
    BasicLoginCredentials(os.environ['VCD_USER'], 'SYSTEM',
                          os.environ['VCD_PASSWORD']))

# Check if current used is system admin
if not client.is_sysadmin():
    print(
        "The current user is not system admin, can't manage all Organizations")
else:
    orgtable = PrettyTable(["Organization name", "href"])
    orgs = client.get_org_list()
    for o in orgs:
        # print(o.attrib['name'])
        # print(o.get('name'))
        # print(o.get('href'))
        orgtable.add_row([o.get('name'), o.get('href')])
    cprint('Print all Organizations', 'yellow')
    print(orgtable)

# Log out.
print("Logging out")