blob: 70902c88a234353d716b18bc49847cae57e3b9c8 [file] [log] [blame]
Daryl Wallecke5b83d42011-11-10 14:39:02 -06001from storm.common import rest_client
2import json
Daryl Walleck73a9e7a2011-11-15 17:43:31 -06003import storm.config
Daryl Wallecke5b83d42011-11-10 14:39:02 -06004import time
5
6
7class ImagesClient(object):
8
9 def __init__(self, username, key, auth_url, tenant_name=None):
10 self.client = rest_client.RestClient(username, key,
11 auth_url, tenant_name)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060012 self.config = storm.config.StormConfig()
13 self.build_interval = self.config.nova.build_interval
14 self.build_timeout = self.config.nova.build_timeout
Daryl Wallecke5b83d42011-11-10 14:39:02 -060015 self.headers = {'Content-Type': 'application/json',
16 'Accept': 'application/json'}
17
18 def create_image(self, server_id, name, meta=None):
19 """Creates an image of the original server"""
20
21 post_body = {
22 'createImage': {
23 'name': name,
24 }
25 }
26
27 if meta != None:
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060028 post_body['createImage']['metadata'] = meta
Daryl Wallecke5b83d42011-11-10 14:39:02 -060029
30 post_body = json.dumps(post_body)
31 resp, body = self.client.post('servers/%s/action' %
32 str(server_id), post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060033 return resp, body
34
35 def list_images(self, params=None):
36 """Returns a list of all images filtered by any parameters"""
37 url = 'images'
38 if params != None:
39 param_list = []
40 for param, value in params.iteritems():
41 param_list.append("%s=%s&" % (param, value))
42
43 url = "images?" + "".join(param_list)
44
45 resp, body = self.client.get(url)
46 body = json.loads(body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060047 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060048
49 def list_images_with_detail(self, params=None):
50 """Returns a detailed list of images filtered by any parameters"""
51 url = 'images/detail'
52 if params != None:
53 param_list = []
54 for param, value in params.iteritems():
55 param_list.append("%s=%s&" % (param, value))
56
57 url = "images/detail?" + "".join(param_list)
58
59 resp, body = self.client.get(url)
60 body = json.loads(body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060061 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060062
63 def get_image(self, image_id):
64 """Returns the details of a single image"""
65 resp, body = self.client.get("images/%s" % str(image_id))
66 body = json.loads(body)
67 return resp, body['image']
68
69 def delete_image(self, image_id):
70 """Deletes the provided image"""
71 return self.client.delete("images/%s" % str(image_id))
72
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060073 def wait_for_image_exists(self, image_id):
74 resp, body = self.client.get("images/%s" % str(image_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -060075 start = int(time.time())
76
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060077 while resp.status != 200:
Daryl Wallecke5b83d42011-11-10 14:39:02 -060078 time.sleep(self.build_interval)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060079 resp, body = self.client.get("images/%s" % str(image_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -060080
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060081 if int(time.time()) - start >= self.build_timeout:
82 raise exceptions.BuildErrorException
83
84 def wait_for_image_status(self, image_id, status):
85 """Waits for an image to reach a given status."""
86 resp, image = self.get_image(image_id)
87 start = int(time.time())
88
89 while image['status'] != status:
90 time.sleep(self.build_interval)
91 resp, image = self.get_image(image_id)
92
93 if image['status'] == 'ERROR':
Daryl Wallecke5b83d42011-11-10 14:39:02 -060094 raise exceptions.TimeoutException
95
96 if int(time.time()) - start >= self.build_timeout:
97 raise exceptions.BuildErrorException
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060098
99 def list_image_metadata(self, image_id):
100 resp, body = self.client.get("images/%s/metadata" % str(image_id))
101 body = json.loads(body)
102 return resp, body
103
104 def set_image_metadata(self, image_id, meta):
105 post_body = json.dumps({'metadata': meta})
106 resp, body = self.client.put('images/%s/metadata' %
107 str(image_id), post_body, self.headers)
108 body = json.loads(body)
109 return resp, body
110
111 def update_image_metadata(self, image_id, meta):
112 post_body = json.dumps({'metadata': meta})
113 resp, body = self.client.post('images/%s/metadata' %
114 str(image_id), post_body, self.headers)
115 body = json.loads(body)
116 return resp, body
117
118 def get_image_metadata_item(self, image_id, key):
119 resp, body = self.client.get("images/%s/metadata/%s" %
120 (str(image_id), key))
121 body = json.loads(body)
122 return resp, body
123
124 def set_image_metadata_item(self, image_id, key, meta):
125 post_body = json.dumps({'meta': meta})
126 resp, body = self.client.put('images/%s/metdata/%s' %
127 (str(image_id), key),
128 post_body, self.headers)
129 body = json.loads(body)
130 return resp, body
131
132 def delete_image_metadata_item(self, image_id, key):
133 resp, body = self.client.delete("images/%s/metadata/%s" %
134 (str(image_id), key))
135 return resp, body