-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigidroplet.py
48 lines (40 loc) · 1.78 KB
/
digidroplet.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
46
47
48
import requests
import json
import time
import argparse
import configparser
import sys
class OceanApi:
def __init__(self, conf_file='conf.cfg'):
config = configparser.ConfigParser()
config.readfp(open(conf_file))
token = config.get('auth', 'token')
self.auth_headers = {'Authorization': 'Bearer {}'.format(token),
'Content-Type': 'application/json'}
def droplets(self):
response = requests.get(url='https://api.digitalocean.com/v2/droplets', headers=self.auth_headers)
return json.loads(response.text)
def id_droplet(self):
response = requests.get(url='https://api.digitalocean.com/v2/droplets', headers=self.auth_headers)
droplets = json.loads(response.text)
if droplets['droplets']:
if len(droplets['droplets']) > 1:
print('You have more than one droplet')
return droplets['droplets'][0]['id']
else:
return None
def droplet(self, droplet_id):
response = requests.get(url='https://api.digitalocean.com/v2/droplets/{}'.format(droplet_id), headers=self.auth_headers)
return json.loads(response.text)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--status', action='store_true', dest='status', default=False)
args = parser.parse_args()
api = OceanApi()
if args.status:
droplet_id = api.id_droplet()
if droplet_id:
_droplet = api.droplet(droplet_id)['droplet']
print('DROPLET: {}, STATUS:{}, IP:{}, REGION: {}, DIST: {}'.format(_droplet['name'], _droplet['status'].upper(), _droplet['networks']['v4'][0]['ip_address'], _droplet['region']['slug'].upper(), _droplet['image']['slug']))
else:
print('No droplets')