blob: 682e66f5fc71ec3bea7af5c9b10ded81c63ab574 [file] [log] [blame]
Daryl Wallecked8bef32011-12-05 23:02:08 -06001from tempest.common import rest_client
2from tempest import exceptions
Daryl Wallecke5b83d42011-11-10 14:39:02 -06003import json
4import time
5
6
7class ImagesClient(object):
8
Jay Pipes7f757632011-12-02 15:53:32 -05009 def __init__(self, config, username, key, auth_url, tenant_name=None):
10 self.config = config
11 self.client = rest_client.RestClient(config, username, key,
Daryl Wallecke5b83d42011-11-10 14:39:02 -060012 auth_url, tenant_name)
Daryl Wallecked8bef32011-12-05 23:02:08 -060013
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060014 self.build_interval = self.config.nova.build_interval
15 self.build_timeout = self.config.nova.build_timeout
Daryl Wallecke5b83d42011-11-10 14:39:02 -060016 self.headers = {'Content-Type': 'application/json',
17 'Accept': 'application/json'}
18
19 def create_image(self, server_id, name, meta=None):
20 """Creates an image of the original server"""
21
22 post_body = {
23 'createImage': {
24 'name': name,
25 }
26 }
27
28 if meta != None:
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060029 post_body['createImage']['metadata'] = meta
Daryl Wallecke5b83d42011-11-10 14:39:02 -060030
31 post_body = json.dumps(post_body)
32 resp, body = self.client.post('servers/%s/action' %
33 str(server_id), post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060034 return resp, body
35
36 def list_images(self, params=None):
37 """Returns a list of all images filtered by any parameters"""
38 url = 'images'
39 if params != None:
40 param_list = []
41 for param, value in params.iteritems():
42 param_list.append("%s=%s&" % (param, value))
43
44 url = "images?" + "".join(param_list)
45
46 resp, body = self.client.get(url)
47 body = json.loads(body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060048 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060049
50 def list_images_with_detail(self, params=None):
51 """Returns a detailed list of images filtered by any parameters"""
52 url = 'images/detail'
53 if params != None:
54 param_list = []
55 for param, value in params.iteritems():
56 param_list.append("%s=%s&" % (param, value))
57
58 url = "images/detail?" + "".join(param_list)
59
60 resp, body = self.client.get(url)
61 body = json.loads(body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060062 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060063
64 def get_image(self, image_id):
65 """Returns the details of a single image"""
66 resp, body = self.client.get("images/%s" % str(image_id))
67 body = json.loads(body)
68 return resp, body['image']
69
70 def delete_image(self, image_id):
71 """Deletes the provided image"""
72 return self.client.delete("images/%s" % str(image_id))
73
Daryl Walleck416af922011-11-22 22:28:33 -060074 def wait_for_image_resp_code(self, image_id, code):
75 """
76 Waits until the HTTP response code for the request matches the
77 expected value
78 """
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 start = int(time.time())
81
Daryl Walleck416af922011-11-22 22:28:33 -060082 while resp.status != code:
Daryl Wallecke5b83d42011-11-10 14:39:02 -060083 time.sleep(self.build_interval)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060084 resp, body = self.client.get("images/%s" % str(image_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -060085
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060086 if int(time.time()) - start >= self.build_timeout:
87 raise exceptions.BuildErrorException
88
89 def wait_for_image_status(self, image_id, status):
90 """Waits for an image to reach a given status."""
91 resp, image = self.get_image(image_id)
92 start = int(time.time())
93
94 while image['status'] != status:
95 time.sleep(self.build_interval)
96 resp, image = self.get_image(image_id)
97
98 if image['status'] == 'ERROR':
Daryl Wallecke5b83d42011-11-10 14:39:02 -060099 raise exceptions.TimeoutException
100
101 if int(time.time()) - start >= self.build_timeout:
102 raise exceptions.BuildErrorException
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600103
104 def list_image_metadata(self, image_id):
Daryl Walleck416af922011-11-22 22:28:33 -0600105 """Lists all metadata items for an image"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600106 resp, body = self.client.get("images/%s/metadata" % str(image_id))
107 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600108 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600109
110 def set_image_metadata(self, image_id, meta):
Daryl Walleck416af922011-11-22 22:28:33 -0600111 """Sets the metadata for an image"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600112 post_body = json.dumps({'metadata': meta})
113 resp, body = self.client.put('images/%s/metadata' %
114 str(image_id), post_body, self.headers)
115 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600116 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600117
118 def update_image_metadata(self, image_id, meta):
Daryl Walleck416af922011-11-22 22:28:33 -0600119 """Updates the metadata for an image"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600120 post_body = json.dumps({'metadata': meta})
121 resp, body = self.client.post('images/%s/metadata' %
122 str(image_id), post_body, self.headers)
123 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600124 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600125
126 def get_image_metadata_item(self, image_id, key):
Daryl Walleck416af922011-11-22 22:28:33 -0600127 """Returns the value for a specific image metadata key"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600128 resp, body = self.client.get("images/%s/metadata/%s" %
129 (str(image_id), key))
130 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600131 return resp, body['meta']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600132
133 def set_image_metadata_item(self, image_id, key, meta):
Daryl Walleck416af922011-11-22 22:28:33 -0600134 """Sets the value for a specific image metadata key"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600135 post_body = json.dumps({'meta': meta})
Daryl Walleck416af922011-11-22 22:28:33 -0600136 resp, body = self.client.put('images/%s/metadata/%s' %
137 (str(image_id), key), post_body,
138 self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600139 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600140 return resp, body['meta']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600141
142 def delete_image_metadata_item(self, image_id, key):
Daryl Walleck416af922011-11-22 22:28:33 -0600143 """Deletes a single image metadata key/value pair"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600144 resp, body = self.client.delete("images/%s/metadata/%s" %
145 (str(image_id), key))
146 return resp, body