forked from deis/deis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinodeapi.py
45 lines (33 loc) · 1.43 KB
/
linodeapi.py
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
#!/usr/bin/env python
"""
Provides a class for Linode API commands
Usage: used by other files as a base class
"""
import requests
import threading
from colorama import Fore, Style
class LinodeApiCommand:
def __init__(self, arguments):
self._arguments = vars(arguments)
self._linode_api_key = arguments.linode_api_key if arguments.linode_api_key is not None else ''
def __getattr__(self, name):
return self._arguments.get(name)
def request(self, action, **kwargs):
data = ''
if self._linode_api_key:
kwargs['params'] = dict({'api_key': self._linode_api_key, 'api_action': action}.items() + kwargs.get('params', {}).items())
response = requests.request('get', 'https://api.linode.com/api/', **kwargs)
json = response.json()
errors = json.get('ERRORARRAY', [])
data = json.get('DATA')
if len(errors) > 0:
raise IOError(str(errors))
else:
self.info('Linode api key not provided. Please provide at the start of script to perform this function.')
return data
def run(self):
raise NotImplementedError
def info(self, message):
print(Fore.MAGENTA + threading.current_thread().name + ': ' + Fore.CYAN + message + Fore.RESET)
def success(self, message):
print(Fore.MAGENTA + threading.current_thread().name + ': ' + Fore.GREEN + message + Fore.RESET)