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