blob: e8a13267861c6829156f1c0449b66088b5309f23 [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 Walleck416af922011-11-22 22:28:33 -060073 def wait_for_image_resp_code(self, image_id, code):
74 """
75 Waits until the HTTP response code for the request matches the
76 expected value
77 """
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060078 resp, body = self.client.get("images/%s" % str(image_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -060079 start = int(time.time())
80
Daryl Walleck416af922011-11-22 22:28:33 -060081 while resp.status != code:
Daryl Wallecke5b83d42011-11-10 14:39:02 -060082 time.sleep(self.build_interval)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060083 resp, body = self.client.get("images/%s" % str(image_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -060084
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060085 if int(time.time()) - start >= self.build_timeout:
86 raise exceptions.BuildErrorException
87
88 def wait_for_image_status(self, image_id, status):
89 """Waits for an image to reach a given status."""
90 resp, image = self.get_image(image_id)
91 start = int(time.time())
92
93 while image['status'] != status:
94 time.sleep(self.build_interval)
95 resp, image = self.get_image(image_id)
96
97 if image['status'] == 'ERROR':
Daryl Wallecke5b83d42011-11-10 14:39:02 -060098 raise exceptions.TimeoutException
99
100 if int(time.time()) - start >= self.build_timeout:
101 raise exceptions.BuildErrorException
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600102
103 def list_image_metadata(self, image_id):
Daryl Walleck416af922011-11-22 22:28:33 -0600104 """Lists all metadata items for an image"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600105 resp, body = self.client.get("images/%s/metadata" % str(image_id))
106 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600107 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600108
109 def set_image_metadata(self, image_id, meta):
Daryl Walleck416af922011-11-22 22:28:33 -0600110 """Sets the metadata for an image"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600111 post_body = json.dumps({'metadata': meta})
112 resp, body = self.client.put('images/%s/metadata' %
113 str(image_id), post_body, self.headers)
114 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600115 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600116
117 def update_image_metadata(self, image_id, meta):
Daryl Walleck416af922011-11-22 22:28:33 -0600118 """Updates the metadata for an image"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600119 post_body = json.dumps({'metadata': meta})
120 resp, body = self.client.post('images/%s/metadata' %
121 str(image_id), post_body, self.headers)
122 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600123 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600124
125 def get_image_metadata_item(self, image_id, key):
Daryl Walleck416af922011-11-22 22:28:33 -0600126 """Returns the value for a specific image metadata key"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600127 resp, body = self.client.get("images/%s/metadata/%s" %
128 (str(image_id), key))
129 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600130 return resp, body['meta']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600131
132 def set_image_metadata_item(self, image_id, key, meta):
Daryl Walleck416af922011-11-22 22:28:33 -0600133 """Sets the value for a specific image metadata key"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600134 post_body = json.dumps({'meta': meta})
Daryl Walleck416af922011-11-22 22:28:33 -0600135 resp, body = self.client.put('images/%s/metadata/%s' %
136 (str(image_id), key), post_body,
137 self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600138 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600139 return resp, body['meta']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600140
141 def delete_image_metadata_item(self, image_id, key):
Daryl Walleck416af922011-11-22 22:28:33 -0600142 """Deletes a single image metadata key/value pair"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600143 resp, body = self.client.delete("images/%s/metadata/%s" %
144 (str(image_id), key))
145 return resp, body