aboutsummaryrefslogtreecommitdiff
path: root/python/get_initial_configuration_cloud_instance.py
blob: 41577163e96fe95f43d9993e59625e0a6f0a210c (plain) (blame)
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
# -*- encoding: utf-8 -*-
import json
try:
    from ovh import ovh
except ImportError:
    # In case it's installed globally
    import ovh
import sys

infos = {}

# Credentials are stored in ~/.ovh.conf
# See ovh/README.rst
client = ovh.Client()

projects_list = client.get('/cloud/project/')
if len(projects_list) > 1:
    print("More than one project is not supported, taking the first one")
project = projects_list[0]
instances_list = client.get('/cloud/project/{}/instance'.format(project))
instances = dict(map(lambda x: (x["id"], x), instances_list))
if sys.argv[-1] in instances:
    instance = instances[sys.argv[-1]]
else:
    print("Instance not in list:")
    for instance in instances_list:
        print("{}: {}".format(instance["name"], instance["id"]))
    sys.exit(1)

infos["ips"] = {}
for ip_infos in instance["ipAddresses"]:
    ip_infos["ipAddress"] = ip_infos.pop("ip")
    ip_infos["gateway"]   = ip_infos.pop("gatewayIp")

    if ip_infos["version"] == 4:
        infos["ips"]["v4"] = ip_infos
    else:
        infos["ips"]["v6"] = ip_infos
        infos["ips"]["v6"]["mask"] = 128

print(json.dumps(infos))