def __init__(self, session, server, username, password, bearer_token, hok_token, private_key): """ Initialize VsphereClient by creating a parent stub factory instance of all vSphere components. :type session: :class:`requests.Session` :param session: Requests HTTP session instance. If not specified, then one is automatically created and used :type server: :class:`str` :param server: vCenter host name or IP address :type username: :class:`str` :param username: Name of the user :type password: :class:`str` :param password: Password of the user :type bearer_token: :class:`str` :param bearer_token: SAML Bearer Token :type hok_token: :class:`str` :param hok_token: SAML Hok Token :type private_key: :class:`str` :param private_key: Absolute file path of the private key of the user """ if not session: self.session = session = requests.Session() host_url = "https://" + server + JSON_RPC_ENDPOINT if username is not None and password is not None and \ not bearer_token and not hok_token: sec_ctx = create_user_password_security_context(username, password) elif bearer_token and not username and not hok_token: sec_ctx = create_saml_bearer_security_context(bearer_token) elif hok_token and private_key and not bearer_token and not username: sec_ctx = create_saml_security_context(hok_token, private_key) else: raise ValueError('Please provide exactly one of the following ' 'authentication scheme: username/password, ' 'bear_token or hok_token/private_key') session_svc = Session( StubConfigurationFactory.new_std_configuration( get_requests_connector( session=session, url=host_url, provider_filter_chain=[ LegacySecurityContextFilter(security_context=sec_ctx) ]))) session_id = session_svc.create() sec_ctx = create_session_security_context(session_id) stub_config = StubConfigurationFactory.new_std_configuration( get_requests_connector( session=session, url=host_url, provider_filter_chain=[ LegacySecurityContextFilter(security_context=sec_ctx) ])) self.session_svc = Session(stub_config) stub_factory = StubFactory(stub_config) ApiClient.__init__(self, stub_factory)
def __init__(self, stub_factory_class, session, refresh_token, vmc_url, csp_url, org_id, sddc_id): """ Initialize VmcClient by creating a stub factory instance using a CSP Security context filter added to the filter chain of the connector :type stub_factory_class: :class:`type` :param stub_factory_class: Which stub factory class to use :type session: :class:`requests.Session` :param session: Requests HTTP session instance :type refresh_token: :class:`str` :param refresh_token: Refresh token obtained from CSP :type vmc_url: :class:`str` :param vmc_url: URL of the VMC service :type csp_url: :class:`str` :param csp_url: URL of the CSP service :type org_id: :class:`str` :param org_id: ID of the VMC organization :type sddc_id: :class:`str` :param sddc_id: ID of the VMC Software-Defined Data Center (SDDC) """ # Call the VMC API to obtain the URL for the NSX Reverse Proxy refresh_url = "%s/%s" % (csp_url, self._CSP_REFRESH_URL_SUFFIX) resp = requests.post("%s?refresh_token=%s" % (refresh_url, refresh_token)) resp.raise_for_status() resp_json = resp.json() access_token = resp_json["access_token"] v_session = requests.Session() v_session.headers["csp-auth-token"] = access_token sddc_url = "%svmc/api/orgs/%s/sddcs/%s" % (vmc_url, org_id, sddc_id) resp = v_session.get(sddc_url) resp.raise_for_status() resp_json = resp.json() nsx_url = resp_json.get("resource_config", {}).get("nsx_api_public_endpoint_url") # Strip trailing "/" if present if nsx_url and nsx_url[-1] == "/": nsx_url = nsx_url[:-1] # Create the stub factory for the NSX API stub_factory = stub_factory_class( StubConfigurationFactory.new_std_configuration( get_requests_connector(session=session, msg_protocol='rest', url=nsx_url, provider_filter_chain=[ CSPSecurityContextFilter( session, refresh_token, refresh_url) ]))) ApiClient.__init__(self, stub_factory)
def __init__(self, session, refresh_token, vmc_url, csp_url): """ Initialize VmcClient by creating a stub factory instance using a CSP Security context filter added to the filter chain of the connector :type session: :class:`requests.Session` :param session: Requests HTTP session instance :type refresh_token: :class:`str` :param refresh_token: Refresh token obtained from CSP """ # Build Refresh URL using the CSP URL and the Refresh URL suffix refresh_url = csp_url.rstrip('/') + self._CSP_REFRESH_URL_SUFFIX stub_factory = VmcStubFactory( StubConfigurationFactory.new_std_configuration( get_requests_connector(session=session, msg_protocol='rest', url=vmc_url, provider_filter_chain=[ CSPSecurityContextFilter( session, refresh_token, refresh_url) ]))) ApiClient.__init__(self, stub_factory)