forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
103 lines (84 loc) · 3.25 KB
/
config.py
File metadata and controls
103 lines (84 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
SoftLayer.config
~~~~~~~~~~~~~~~~
Handles different methods for loading configuration for the API bindings
:license: MIT, see LICENSE for more details.
"""
import os
import os.path
from SoftLayer import auth
from SoftLayer import utils
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 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_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
SETTING_RESOLVERS = [get_client_settings_args,
get_client_settings_env,
get_client_settings_config_file]
def get_client_settings(**kwargs):
"""Parse client settings.
Parses settings from various input methods, preferring earlier values
to later ones. Once an 'auth' value is found, it returns the gathered
settings. The settings currently come from explicit user arguments,
environmental variables and config files.
:param \\*\\*kwargs: Arguments that are passed into the client instance
"""
all_settings = {}
for setting_method in SETTING_RESOLVERS:
settings = setting_method(**kwargs)
if settings:
settings.update((k, v) for k, v in all_settings.items() if v)
all_settings = settings
if all_settings.get('auth'):
break
return all_settings