]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - python/get_initial_configuration_cloud_instance.py
Merge branch 'dev'
[perso/Immae/Projets/Puppet.git] / python / get_initial_configuration_cloud_instance.py
1 # -*- encoding: utf-8 -*-
2 import json
3 try:
4 from ovh import ovh
5 except ImportError:
6 # In case it's installed globally
7 import ovh
8 import sys
9
10 infos = {}
11
12 # Credentials are stored in ~/.ovh.conf
13 # See ovh/README.rst
14 client = ovh.Client()
15
16 projects_list = client.get('/cloud/project/')
17 if len(projects_list) > 1:
18 print("More than one project is not supported, taking the first one")
19 project = projects_list[0]
20 instances_list = client.get('/cloud/project/{}/instance'.format(project))
21 instances = dict(map(lambda x: (x["id"], x), instances_list))
22 if sys.argv[-1] in instances:
23 instance = instances[sys.argv[-1]]
24 else:
25 print("Instance not in list:")
26 for instance in instances_list:
27 print("{}: {}".format(instance["name"], instance["id"]))
28 sys.exit(1)
29
30 infos["ips"] = {}
31 for ip_infos in instance["ipAddresses"]:
32 ip_infos["ipAddress"] = ip_infos.pop("ip")
33 ip_infos["gateway"] = ip_infos.pop("gatewayIp")
34
35 if ip_infos["version"] == 4:
36 infos["ips"]["v4"] = ip_infos
37 else:
38 infos["ips"]["v6"] = ip_infos
39 infos["ips"]["v6"]["mask"] = 128
40
41 print(json.dumps(infos))