6
6
:license: MIT, see LICENSE for more details.
7
7
"""
8
8
# pylint: disable=invalid-name
9
- import os
10
9
import time
11
- import warnings
12
10
13
11
import concurrent .futures as cf
14
12
import json
@@ -148,7 +146,6 @@ def create_client_from_env(username=None,
148
146
149
147
def employee_client (username = None ,
150
148
access_token = None ,
151
- password = None ,
152
149
endpoint_url = None ,
153
150
timeout = None ,
154
151
auth = None ,
@@ -159,27 +156,22 @@ def employee_client(username=None,
159
156
verify = False ):
160
157
"""Creates an INTERNAL SoftLayer API client using your environment.
161
158
162
- Settings are loaded via keyword arguments, environemtal variables and
163
- config file.
159
+ Settings are loaded via keyword arguments, environemtal variables and config file.
164
160
165
161
:param username: your user ID
166
- :param access_token: hash from SoftLayer_User_Employee::performExternalAuthentication(username, password, 2fa_string )
162
+ :param access_token: hash from SoftLayer_User_Employee::performExternalAuthentication(username, password, token )
167
163
:param password: password to use for employee authentication
168
164
:param endpoint_url: the API endpoint base URL you wish to connect to.
169
- Set this to API_PRIVATE_ENDPOINT to connect via SoftLayer's private
170
- network.
165
+ Set this to API_PRIVATE_ENDPOINT to connect via SoftLayer's private network.
171
166
:param proxy: proxy to be used to make API calls
172
167
:param integer timeout: timeout for API requests
173
- :param auth: an object which responds to get_headers() to be inserted into
174
- the xml-rpc headers. Example: `BasicAuthentication`
168
+ :param auth: an object which responds to get_headers() to be inserted into the xml-rpc headers.
169
+ Example: `BasicAuthentication`
175
170
:param config_file: A path to a configuration file used to load settings
176
171
:param user_agent: an optional User Agent to report when making API
177
172
calls if you wish to bypass the packages built in User Agent string
178
- :param transport: An object that's callable with this signature:
179
- transport(SoftLayer.transports.Request)
180
- :param bool verify: decide to verify the server's SSL/TLS cert. DO NOT SET
181
- TO FALSE WITHOUT UNDERSTANDING THE IMPLICATIONS.
182
-
173
+ :param transport: An object that's callable with this signature: transport(SoftLayer.transports.Request)
174
+ :param bool verify: decide to verify the server's SSL/TLS cert.
183
175
"""
184
176
settings = config .get_client_settings (username = username ,
185
177
api_key = None ,
@@ -214,7 +206,6 @@ def employee_client(username=None,
214
206
verify = verify ,
215
207
)
216
208
217
-
218
209
if access_token is None :
219
210
access_token = settings .get ('access_token' )
220
211
@@ -241,16 +232,23 @@ class BaseClient(object):
241
232
:param auth: auth driver that looks like SoftLayer.auth.AuthenticationBase
242
233
:param transport: An object that's callable with this signature: transport(SoftLayer.transports.Request)
243
234
"""
244
-
245
235
_prefix = "SoftLayer_"
236
+ auth : slauth .AuthenticationBase
246
237
247
238
def __init__ (self , auth = None , transport = None , config_file = None ):
248
239
if config_file is None :
249
240
config_file = CONFIG_FILE
250
- self .auth = auth
251
241
self .config_file = config_file
252
242
self .settings = config .get_config (self .config_file )
243
+ self .__setAuth (auth )
244
+ self .__setTransport (transport )
245
+
246
+ def __setAuth (self , auth = None ):
247
+ """Prepares the authentication property"""
248
+ self .auth = auth
253
249
250
+ def __setTransport (self , transport = None ):
251
+ """Prepares the transport property"""
254
252
if transport is None :
255
253
url = self .settings ['softlayer' ].get ('endpoint_url' )
256
254
if url is not None and '/rest' in url :
@@ -469,6 +467,7 @@ def __repr__(self):
469
467
def __len__ (self ):
470
468
return 0
471
469
470
+
472
471
class CertificateClient (BaseClient ):
473
472
"""Client that works with a X509 Certificate for authentication.
474
473
@@ -479,42 +478,20 @@ class CertificateClient(BaseClient):
479
478
"""
480
479
481
480
def __init__ (self , auth = None , transport = None , config_file = None ):
482
- if config_file is None :
483
- config_file = CONFIG_FILE
484
- self .config_file = config_file
485
- self .settings = config .get_config (self .config_file )
481
+ BaseClient .__init__ (self , auth , transport , config_file )
482
+ self .__setAuth (auth )
486
483
484
+ def __setAuth (self , auth = None ):
485
+ """Prepares the authentication property"""
487
486
if auth is None :
488
487
auth_cert = self .settings ['softlayer' ].get ('auth_cert' )
489
488
serv_cert = self .settings ['softlayer' ].get ('server_cert' , None )
490
489
auth = slauth .X509Authentication (auth_cert , serv_cert )
491
- self .auth = auth
492
-
493
-
490
+ self .auth = auth
494
491
495
- if transport is None :
496
- url = self .settings ['softlayer' ].get ('endpoint_url' )
497
- if url is not None and '/rest' in url :
498
- # If this looks like a rest endpoint, use the rest transport
499
- transport = transports .RestTransport (
500
- endpoint_url = url ,
501
- proxy = self .settings ['softlayer' ].get ('proxy' ),
502
- # prevents an exception incase timeout is a float number.
503
- timeout = int (self .settings ['softlayer' ].getfloat ('timeout' , 0 )),
504
- user_agent = consts .USER_AGENT ,
505
- verify = self .settings ['softlayer' ].getboolean ('verify' ),
506
- )
507
- else :
508
- # Default the transport to use XMLRPC
509
- transport = transports .XmlRpcTransport (
510
- endpoint_url = url ,
511
- proxy = self .settings ['softlayer' ].get ('proxy' ),
512
- timeout = int (self .settings ['softlayer' ].getfloat ('timeout' , 0 )),
513
- user_agent = consts .USER_AGENT ,
514
- verify = self .settings ['softlayer' ].getboolean ('verify' ),
515
- )
492
+ def __repr__ (self ):
493
+ return "CertificateClient(transport=%r, auth=%r)" % (self .transport , self .auth )
516
494
517
- self .transport = transport
518
495
519
496
class IAMClient (BaseClient ):
520
497
"""IBM ID Client for using IAM authentication
@@ -711,9 +688,8 @@ def __init__(self, auth=None, transport=None, config_file=None, account_id=None)
711
688
BaseClient .__init__ (self , auth , transport , config_file )
712
689
self .account_id = account_id
713
690
714
-
715
- def authenticate_with_password (self , username , password , security_token = None ):
716
- """Performs IBM IAM Username/Password Authentication
691
+ def authenticate_with_internal (self , username , password , security_token = None ):
692
+ """Performs internal authentication
717
693
718
694
:param string username: your softlayer username
719
695
:param string password: your softlayer password
@@ -724,12 +700,11 @@ def authenticate_with_password(self, username, password, security_token=None):
724
700
if security_token is None :
725
701
security_token = input ("Enter your 2FA Token now: " )
726
702
if len (security_token ) != 6 :
727
- raise Exception ("Invalid security token: {}" .format (security_token ))
703
+ raise exceptions . SoftLayerAPIError ("Invalid security token: {}" .format (security_token ))
728
704
729
705
auth_result = self .call ('SoftLayer_User_Employee' , 'performExternalAuthentication' ,
730
706
username , password , security_token )
731
707
732
-
733
708
self .settings ['softlayer' ]['access_token' ] = auth_result ['hash' ]
734
709
self .settings ['softlayer' ]['userid' ] = str (auth_result ['userId' ])
735
710
# self.settings['softlayer']['refresh_token'] = tokens['refresh_token']
@@ -739,8 +714,6 @@ def authenticate_with_password(self, username, password, security_token=None):
739
714
740
715
return auth_result
741
716
742
-
743
-
744
717
def authenticate_with_hash (self , userId , access_token ):
745
718
"""Authenticates to the Internal SL API with an employee userid + token
746
719
@@ -791,6 +764,7 @@ def call(self, service, method, *args, **kwargs):
791
764
def __repr__ (self ):
792
765
return "EmployeeClient(transport=%r, auth=%r)" % (self .transport , self .auth )
793
766
767
+
794
768
class Service (object ):
795
769
"""A SoftLayer Service.
796
770
0 commit comments