blob: e5871cba9a986eed988c3cc59b8747a7c75b792d [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
Daryl Wallecked8bef32011-12-05 23:02:08 -06004import tempest.config
Daryl Wallecke5b83d42011-11-10 14:39:02 -06005import time
6
7
8class ImagesClient(object):
9
Jay Pipes7f757632011-12-02 15:53:32 -050010 def __init__(self, config, username, key, auth_url, tenant_name=None):
11 self.config = config
12 self.client = rest_client.RestClient(config, username, key,
Daryl Wallecke5b83d42011-11-10 14:39:02 -060013 auth_url, tenant_name)
Daryl Wallecked8bef32011-12-05 23:02:08 -060014
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060015 self.build_interval = self.config.nova.build_interval
16 self.build_timeout = self.config.nova.build_timeout
Daryl Wallecke5b83d42011-11-10 14:39:02 -060017 self.headers = {'Content-Type': 'application/json',
18 'Accept': 'application/json'}
19
20 def create_image(self, server_id, name, meta=None):
21 """Creates an image of the original server"""
22
23 post_body = {
24 'createImage': {
25 'name': name,
26 }
27 }
28
29 if meta != None:
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060030 post_body['createImage']['metadata'] = meta
Daryl Wallecke5b83d42011-11-10 14:39:02 -060031
32 post_body = json.dumps(post_body)
33 resp, body = self.client.post('servers/%s/action' %
34 str(server_id), post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060035 return resp, body
36
37 def list_images(self, params=None):
38 """Returns a list of all images filtered by any parameters"""
39 url = 'images'
40 if params != None:
41 param_list = []
42 for param, value in params.iteritems():
43 param_list.append("%s=%s&" % (param, value))
44
45 url = "images?" + "".join(param_list)
46
47 resp, body = self.client.get(url)
48 body = json.loads(body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060049 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060050
51 def list_images_with_detail(self, params=None):
52 """Returns a detailed list of images filtered by any parameters"""
53 url = 'images/detail'
54 if params != None:
55 param_list = []
56 for param, value in params.iteritems():
57 param_list.append("%s=%s&" % (param, value))
58
59 url = "images/detail?" + "".join(param_list)
60
61 resp, body = self.client.get(url)
62 body = json.loads(body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060063 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060064
65 def get_image(self, image_id):
66 """Returns the details of a single image"""
67 resp, body = self.client.get("images/%s" % str(image_id))
68 body = json.loads(body)
69 return resp, body['image']
70
71 def delete_image(self, image_id):
72 """Deletes the provided image"""
73 return self.client.delete("images/%s" % str(image_id))
74
Daryl Walleck416af922011-11-22 22:28:33 -060075 def wait_for_image_resp_code(self, image_id, code):
76 """
77 Waits until the HTTP response code for the request matches the
78 expected value
79 """
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060080 resp, body = self.client.get("images/%s" % str(image_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -060081 start = int(time.time())
82
Daryl Walleck416af922011-11-22 22:28:33 -060083 while resp.status != code:
Daryl Wallecke5b83d42011-11-10 14:39:02 -060084 time.sleep(self.build_interval)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060085 resp, body = self.client.get("images/%s" % str(image_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -060086
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060087 if int(time.time()) - start >= self.build_timeout:
88 raise exceptions.BuildErrorException
89
90 def wait_for_image_status(self, image_id, status):
91 """Waits for an image to reach a given status."""
92 resp, image = self.get_image(image_id)
93 start = int(time.time())
94
95 while image['status'] != status:
96 time.sleep(self.build_interval)
97 resp, image = self.get_image(image_id)
98
99 if image['status'] == 'ERROR':
Daryl Wallecke5b83d42011-11-10 14:39:02 -0600100 raise exceptions.TimeoutException
101
102 if int(time.time()) - start >= self.build_timeout:
103 raise exceptions.BuildErrorException
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600104
105 def list_image_metadata(self, image_id):
Daryl Walleck416af922011-11-22 22:28:33 -0600106 """Lists all metadata items for an image"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600107 resp, body = self.client.get("images/%s/metadata" % str(image_id))
108 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600109 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600110
111 def set_image_metadata(self, image_id, meta):
Daryl Walleck416af922011-11-22 22:28:33 -0600112 """Sets the metadata for an image"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600113 post_body = json.dumps({'metadata': meta})
114 resp, body = self.client.put('images/%s/metadata' %
115 str(image_id), post_body, self.headers)
116 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600117 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600118
119 def update_image_metadata(self, image_id, meta):
Daryl Walleck416af922011-11-22 22:28:33 -0600120 """Updates the metadata for an image"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600121 post_body = json.dumps({'metadata': meta})
122 resp, body = self.client.post('images/%s/metadata' %
123 str(image_id), post_body, self.headers)
124 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600125 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600126
127 def get_image_metadata_item(self, image_id, key):
Daryl Walleck416af922011-11-22 22:28:33 -0600128 """Returns the value for a specific image metadata key"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600129 resp, body = self.client.get("images/%s/metadata/%s" %
130 (str(image_id), key))
131 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600132 return resp, body['meta']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600133
134 def set_image_metadata_item(self, image_id, key, meta):
Daryl Walleck416af922011-11-22 22:28:33 -0600135 """Sets the value for a specific image metadata key"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600136 post_body = json.dumps({'meta': meta})
Daryl Walleck416af922011-11-22 22:28:33 -0600137 resp, body = self.client.put('images/%s/metadata/%s' %
138 (str(image_id), key), post_body,
139 self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600140 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600141 return resp, body['meta']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600142
143 def delete_image_metadata_item(self, image_id, key):
Daryl Walleck416af922011-11-22 22:28:33 -0600144 """Deletes a single image metadata key/value pair"""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600145 resp, body = self.client.delete("images/%s/metadata/%s" %
146 (str(image_id), key))
147 return resp, body