-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovh_instance_list.py
133 lines (109 loc) · 4.5 KB
/
ovh_instance_list.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import ovh
import json
import os
import sys
def initialize_ovh_client():
endpoint = os.environ.get('OVH_ENDPOINT')
application_key = os.environ.get('OVH_APPLICATION_KEY')
application_secret = os.environ.get('OVH_APPLICATION_SECRET')
consumer_key = os.environ.get('OVH_CONSUMER_KEY')
if not all([endpoint, application_key, application_secret, consumer_key]):
print("Error: Missing OVH API credentials. Please ensure all required environment variables are set.")
print("Required variables: OVH_ENDPOINT, OVH_APPLICATION_KEY, OVH_APPLICATION_SECRET, OVH_CONSUMER_KEY")
sys.exit(1)
try:
client = ovh.Client(
endpoint=endpoint,
application_key=application_key,
application_secret=application_secret,
consumer_key=consumer_key
)
print(f"Successfully initialized OVH client with endpoint: {endpoint}")
return client
except ovh.exceptions.InvalidRegion as e:
print(f"Error: Invalid OVH endpoint. {str(e)}")
print("Please ensure the OVH_ENDPOINT environment variable is set to a valid endpoint.")
sys.exit(1)
except Exception as e:
print(f"Error initializing OVH client: {str(e)}")
sys.exit(1)
def get_flavors(client, service_name, region):
try:
flavors = client.get(f'/cloud/project/{service_name}/flavor', region=region)
print(f"Retrieved {len(flavors)} flavors for service {service_name}, region {region}")
return flavors
except Exception as e:
print(f"Error fetching flavors for service {service_name}, region {region}: {str(e)}")
return []
def get_images(client, service_name, region, flavor_id):
try:
images = client.get(f'/cloud/project/{service_name}/image', flavorType=flavor_id, osType='linux', region=region)
print(f"Retrieved {len(images)} images for service {service_name}, region {region}, flavor {flavor_id}")
return images
except Exception as e:
print(f"Error fetching images for service {service_name}, region {region}, flavor {flavor_id}: {str(e)}")
return []
def list_flavors():
client = initialize_ovh_client()
service_name = os.environ.get('OVH_SERVICE_NAME')
if not service_name:
print("Error: OVH_SERVICE_NAME environment variable is not set.")
sys.exit(1)
print(f"Using OVH_SERVICE_NAME: {service_name}")
result = {}
regions = ["GRA11", "GRA9"]
for region in regions:
result[region] = []
flavors = get_flavors(client, service_name, region)
for flavor in flavors:
result[region].append({
'id': flavor['id'],
'name': flavor['name'],
'region': region,
'ram': flavor['ram'],
'disk': flavor['disk'],
'vcpus': flavor['vcpus']
})
# Write results to a JSON file
with open('ovh_flavors.json', 'w') as f:
json.dump(result, f, indent=2)
# Print results to console
print("\nOVH Instance Flavors by Region:")
print(json.dumps(result, indent=2))
if result:
print("\nResults have been saved to ovh_flavors.json")
else:
print("\nNo data was retrieved. Please check your OVH account and permissions.")
def list_images_for_flavor(flavor_id):
client = initialize_ovh_client()
service_name = os.environ.get('OVH_SERVICE_NAME')
if not service_name:
print("Error: OVH_SERVICE_NAME environment variable is not set.")
sys.exit(1)
print(f"Using OVH_SERVICE_NAME: {service_name}")
result = {}
regions = ["GRA11", "GRA9"]
for region in regions:
images = get_images(client, service_name, region, flavor_id)
result[region] = [
{
'id': image['id'],
'name': image['name'],
'region': region
} for image in images
]
# Write results to a JSON file
with open(f'ovh_images_{flavor_id}.json', 'w') as f:
json.dump(result, f, indent=2)
# Print results to console
print(f"\nOVH Images for Flavor {flavor_id} by Region:")
print(json.dumps(result, indent=2))
if result:
print(f"\nResults have been saved to ovh_images_{flavor_id}.json")
else:
print("\nNo data was retrieved. Please check your OVH account and permissions.")
if __name__ == "__main__":
if len(sys.argv) > 1:
list_images_for_flavor(sys.argv[1])
else:
list_flavors()