def set_up(self): transport = transports.XmlRpcTransport( endpoint_url='http://endpoint-url', ) self.env.client = SoftLayer.BaseClient( transport=transport, auth=auth.BasicAuthentication('username', 'api-key'))
def get_api_key(client, username, secret): """Attempts API-Key and password auth to get an API key. This will also generate an API key if one doesn't exist """ # Try to use a client with username/api key if len(secret) == 64: try: client.auth = auth.BasicAuthentication(username, secret) client['Account'].getCurrentUser() return secret except SoftLayer.SoftLayerAPIError as ex: if 'invalid api token' not in ex.faultString.lower(): raise else: # Try to use a client with username/password client.authenticate_with_password(username, secret) user_record = client['Account'].getCurrentUser( mask='id, apiAuthenticationKeys') api_keys = user_record['apiAuthenticationKeys'] if len(api_keys) == 0: return client['User_Customer'].addApiAuthenticationKey( id=user_record['id']) return api_keys[0]['authenticationKey']
def get_client_settings_config_file(**kwargs): """Retrieve client settings from the possible config file locations. :param \\*\\*kwargs: Arguments that are passed into the client instance """ config_files = ['/etc/softlayer.conf', '~/.softlayer'] if kwargs.get('config_file'): config_files.append(kwargs.get('config_file')) config_files = [os.path.expanduser(f) for f in config_files] config = utils.configparser.RawConfigParser({ 'username': '', 'api_key': '', 'endpoint_url': '', 'timeout': '', 'proxy': '', }) config.read(config_files) if not config.has_section('softlayer'): return settings = { 'endpoint_url': config.get('softlayer', 'endpoint_url'), 'timeout': config.get('softlayer', 'timeout'), 'proxy': config.get('softlayer', 'proxy'), } username = config.get('softlayer', 'username') api_key = config.get('softlayer', 'api_key') if username and api_key: settings['auth'] = auth.BasicAuthentication(username, api_key) return settings
def set_up(self): client = testing.FixtureClient() client.auth = auth.BasicAuthentication('default-user', 'default-key') client.endpoint_url = 'default-endpoint-url' client.timeout = 10 self.client = client self.env = mock.MagicMock()
def get_client_settings_env(**_): """Retrieve client settings from environment settings. :param \\*\\*kwargs: Arguments that are passed into the client instance """ username = os.environ.get('SL_USERNAME') api_key = os.environ.get('SL_API_KEY') proxy = os.environ.get('https_proxy') config = {'proxy': proxy} if username and api_key: config['auth'] = auth.BasicAuthentication(username, api_key) return config
def get_client_settings_args(**kwargs): """Retrieve client settings from user-supplied arguments. :param \\*\\*kwargs: Arguments that are passed into the client instance """ settings = { 'endpoint_url': kwargs.get('endpoint_url'), 'timeout': kwargs.get('timeout'), 'auth': kwargs.get('auth'), 'proxy': kwargs.get('proxy'), } username = kwargs.get('username') api_key = kwargs.get('api_key') if username and api_key and not settings['auth']: settings['auth'] = auth.BasicAuthentication(username, api_key) return settings
def create_client_from_env(username=None, api_key=None, endpoint_url=None, timeout=None, auth=None, config_file=None, proxy=None, user_agent=None, transport=None): """Creates a SoftLayer API client using your environment. Settings are loaded via keyword arguments, environemtal variables and config file. :param username: an optional API username if you wish to bypass the package's built-in username :param api_key: an optional API key if you wish to bypass the package's built in API key :param endpoint_url: the API endpoint base URL you wish to connect to. Set this to API_PRIVATE_ENDPOINT to connect via SoftLayer's private network. :param proxy: proxy to be used to make API calls :param integer timeout: timeout for API requests :param auth: an object which responds to get_headers() to be inserted into the xml-rpc headers. Example: `BasicAuthentication` :param config_file: A path to a configuration file used to load settings :param user_agent: an optional User Agent to report when making API calls if you wish to bypass the packages built in User Agent string :param transport: An object that's callable with this signature: transport(SoftLayer.transports.Request) Usage: >>> import SoftLayer >>> client = SoftLayer.create_client_from_env() >>> resp = client['Account'].getObject() >>> resp['companyName'] 'Your Company' """ settings = config.get_client_settings(username=username, api_key=api_key, endpoint_url=endpoint_url, timeout=timeout, proxy=proxy, config_file=config_file) # Default the transport to use XMLRPC if transport is None: transport = transports.XmlRpcTransport( endpoint_url=settings.get('endpoint_url'), proxy=settings.get('proxy'), timeout=settings.get('timeout'), user_agent=user_agent, ) # If we have enough information to make an auth driver, let's do it if auth is None and settings.get('username') and settings.get('api_key'): auth = slauth.BasicAuthentication( settings.get('username'), settings.get('api_key'), ) return BaseClient(auth=auth, transport=transport)
def set_up(self): self.auth = auth.BasicAuthentication('USERNAME', 'APIKEY')
def create_client_from_env(username=None, api_key=None, endpoint_url=None, timeout=None, auth=None, config_file=None, proxy=None, user_agent=None, transport=None, verify=True): """Creates a SoftLayer API client using your environment. Settings are loaded via keyword arguments, environemtal variables and config file. :param username: an optional API username if you wish to bypass the package's built-in username :param api_key: an optional API key if you wish to bypass the package's built in API key :param endpoint_url: the API endpoint base URL you wish to connect to. Set this to API_PRIVATE_ENDPOINT to connect via SoftLayer's private network. :param proxy: proxy to be used to make API calls :param integer timeout: timeout for API requests :param auth: an object which responds to get_headers() to be inserted into the xml-rpc headers. Example: `BasicAuthentication` :param config_file: A path to a configuration file used to load settings :param user_agent: an optional User Agent to report when making API calls if you wish to bypass the packages built in User Agent string :param transport: An object that's callable with this signature: transport(SoftLayer.transports.Request) :param bool verify: decide to verify the server's SSL/TLS cert. DO NOT SET TO FALSE WITHOUT UNDERSTANDING THE IMPLICATIONS. Usage: >>> import SoftLayer >>> client = SoftLayer.create_client_from_env() >>> resp = client.call('Account', 'getObject') >>> resp['companyName'] 'Your Company' """ if config_file is None: config_file = CONFIG_FILE settings = config.get_client_settings(username=username, api_key=api_key, endpoint_url=endpoint_url, timeout=timeout, proxy=proxy, verify=verify, config_file=config_file) if transport is None: url = settings.get('endpoint_url') if url is not None and '/rest' in url: # If this looks like a rest endpoint, use the rest transport transport = transports.RestTransport( endpoint_url=settings.get('endpoint_url'), proxy=settings.get('proxy'), timeout=settings.get('timeout'), user_agent=user_agent, verify=verify, ) else: # Default the transport to use XMLRPC transport = transports.XmlRpcTransport( endpoint_url=settings.get('endpoint_url'), proxy=settings.get('proxy'), timeout=settings.get('timeout'), user_agent=user_agent, verify=verify, ) # If we have enough information to make an auth driver, let's do it if auth is None and settings.get('username') and settings.get('api_key'): # NOTE(kmcdonald): some transports mask other transports, so this is # a way to find the 'real' one real_transport = getattr(transport, 'transport', transport) if isinstance(real_transport, transports.XmlRpcTransport): auth = slauth.BasicAuthentication( settings.get('username'), settings.get('api_key'), ) elif isinstance(real_transport, transports.RestTransport): auth = slauth.BasicHTTPAuthentication( settings.get('username'), settings.get('api_key'), ) return BaseClient(auth=auth, transport=transport, config_file=config_file)