-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_instances.py
executable file
·78 lines (55 loc) · 2.05 KB
/
list_instances.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
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
#!/usr/bin/env python
"""This Python snippet uses the Scaleway API to list instances. If you have more than 50 instances, the response is
paginated. This script consumes pagination.
Run the script with --help to get usage.
The environment variable SCW_TOKEN must be set. To get your API token, visit the credentials page of the web console. It
is absolutely essential that you keep your token private as it gives access to everything in your Scaleway account.
Dependencies:
- requests ; install with
`pip install requests`
or (Ubuntu / Python 2) `apt-get install -y python-requests`
or (Ubuntu / Python 3) `apt-get install -y python3-requests`
"""
from __future__ import print_function
import argparse
import logging
import os
import sys
import requests
COMPUTE_REGIONS = (
'ams1',
'par1',
)
def list_instances(region, auth_token):
base_url = 'https://cp-%s.scaleway.com' % region
path = '/servers'
while True:
resp = requests.get(
base_url + path,
headers={'X-Auth-Token': auth_token},
)
resp.raise_for_status()
for server in resp.json()['servers']:
print ('%s %-10s %-30s state=%-10s public IP=%-15s private IP=%-15s' % (
server['id'],
server['commercial_type'],
server['name'],
server['state'],
(server['public_ip'] or {}).get('address', ''),
server['private_ip'] or ''
))
if 'next' not in resp.links:
break
path = resp.links['next']['url']
def main():
logging.basicConfig(format='[%(levelname)s] %(message)s')
auth_token = os.getenv('SCW_TOKEN')
if not auth_token:
logging.error('Environment variable SCW_TOKEN required')
sys.exit(1)
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--region', choices=COMPUTE_REGIONS, help='Region to list servers from')
args = parser.parse_args()
list_instances(args.region, auth_token)
if __name__ == '__main__':
main()