This repository was archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
101 lines (77 loc) · 2.94 KB
/
__init__.py
File metadata and controls
101 lines (77 loc) · 2.94 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
import os
import configparser
from .client import RestAPI
from .log import logger
from .lib.server import Server
from .lib.server_group import ServerGroup
from .lib.volume import Volume
from .lib.flavor import Flavor
from .lib.floating_ip import FloatingIp
from .lib.image import Image
from .lib.region import Region
from .lib.network import Network
from .lib.subnet import Subnet
from .lib.objects_user import ObjectsUser
from .error import CloudscaleException, CloudscaleApiException # noqa F401
__version__ = '0.8.0'
APP_NAME = 'cloudscale-cli'
CLOUDSCALE_API_ENDPOINT = 'https://api.cloudscale.ch/v1'
CLOUDSCALE_CONFIG = 'cloudscale.ini'
class Cloudscale:
def __init__(self, api_token=None, profile=None):
if api_token and profile:
raise CloudscaleException("API token and profile are mutually exclusive")
# Read ini configs
self.config = self._read_from_configfile(profile=profile)
if api_token:
self.api_token = api_token
else:
self.api_token = self.config.get('api_token')
if not self.api_token:
raise CloudscaleException("Missing API key")
logger.debug("API token: {}...".format(self.api_token[:4]))
# Configre requests timeout
self.timeout = self.config.get('timeout', 60)
logger.debug("Timeout: {}".format(self.timeout))
self.service_classes = {
'server': Server,
'server_group': ServerGroup,
'volume': Volume,
'flavor': Flavor,
'floating_ip': FloatingIp,
'image': Image,
'region': Region,
'network': Network,
'subnet': Subnet,
'objects_user': ObjectsUser,
}
def _read_from_configfile(self, profile=None):
config_file = os.getenv('CLOUDSCALE_CONFIG', CLOUDSCALE_CONFIG)
paths = (
os.path.join(os.path.expanduser('~'), '.{}'.format(config_file)),
os.path.join(os.getcwd(), config_file),
)
conf = configparser.ConfigParser()
conf.read(paths)
if profile:
if profile not in conf._sections:
raise CloudscaleException("Profile not found in config files: {}, ({})".format(profile, ', '. join(paths)))
else:
profile = os.getenv('CLOUDSCALE_PROFILE', 'default')
logger.info("Profile: {}".format(profile))
if not conf._sections.get(profile):
return dict()
return dict(conf.items(profile))
def __getattr__(self, name):
try:
client = RestAPI(
api_token=self.api_token,
endpoint=CLOUDSCALE_API_ENDPOINT,
user_agent="python-cloudscale {}".format(__version__),
timeout=self.timeout,
)
obj = self.service_classes[name]()
obj._client = client
return obj
except NameError as e:
raise CloudscaleException(e)