Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 1 | from tempest.common import rest_client |
| 2 | from tempest import exceptions |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 3 | import json |
| 4 | import time |
| 5 | |
| 6 | |
| 7 | class ImagesClient(object): |
| 8 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 9 | 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 Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 12 | auth_url, tenant_name) |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 13 | |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 14 | self.build_interval = self.config.nova.build_interval |
| 15 | self.build_timeout = self.config.nova.build_timeout |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 16 | 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 Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 29 | post_body['createImage']['metadata'] = meta |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 30 | |
| 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 Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 34 | 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 Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 48 | return resp, body['images'] |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 49 | |
| 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 Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 62 | return resp, body['images'] |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 63 | |
| 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 Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 74 | 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 Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 79 | resp, body = self.client.get("images/%s" % str(image_id)) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 80 | start = int(time.time()) |
| 81 | |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 82 | while resp.status != code: |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 83 | time.sleep(self.build_interval) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 84 | resp, body = self.client.get("images/%s" % str(image_id)) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 85 | |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 86 | 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 Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 99 | raise exceptions.TimeoutException |
| 100 | |
| 101 | if int(time.time()) - start >= self.build_timeout: |
| 102 | raise exceptions.BuildErrorException |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 103 | |
| 104 | def list_image_metadata(self, image_id): |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 105 | """Lists all metadata items for an image""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 106 | resp, body = self.client.get("images/%s/metadata" % str(image_id)) |
| 107 | body = json.loads(body) |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 108 | return resp, body['metadata'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 109 | |
| 110 | def set_image_metadata(self, image_id, meta): |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 111 | """Sets the metadata for an image""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 112 | 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 Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 116 | return resp, body['metadata'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 117 | |
| 118 | def update_image_metadata(self, image_id, meta): |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 119 | """Updates the metadata for an image""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 120 | 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 Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 124 | return resp, body['metadata'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 125 | |
| 126 | def get_image_metadata_item(self, image_id, key): |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 127 | """Returns the value for a specific image metadata key""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 128 | resp, body = self.client.get("images/%s/metadata/%s" % |
| 129 | (str(image_id), key)) |
| 130 | body = json.loads(body) |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 131 | return resp, body['meta'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 132 | |
| 133 | def set_image_metadata_item(self, image_id, key, meta): |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 134 | """Sets the value for a specific image metadata key""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 135 | post_body = json.dumps({'meta': meta}) |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 136 | resp, body = self.client.put('images/%s/metadata/%s' % |
| 137 | (str(image_id), key), post_body, |
| 138 | self.headers) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 139 | body = json.loads(body) |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 140 | return resp, body['meta'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 141 | |
| 142 | def delete_image_metadata_item(self, image_id, key): |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 143 | """Deletes a single image metadata key/value pair""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 144 | resp, body = self.client.delete("images/%s/metadata/%s" % |
| 145 | (str(image_id), key)) |
| 146 | return resp, body |