dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 3 | # Copyright 2012 OpenStack Foundation |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 18 | import json |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 19 | import urllib |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 20 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | from tempest.common.rest_client import RestClient |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 22 | from tempest.common import waiters |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 23 | from tempest import exceptions |
| 24 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 25 | |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 26 | class ImagesClientJSON(RestClient): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 27 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 28 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
Mauro S. M. Rodrigues | 6e37324 | 2012-08-27 18:59:19 -0400 | [diff] [blame] | 29 | super(ImagesClientJSON, self).__init__(config, username, password, |
| 30 | auth_url, tenant_name) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 31 | self.service = self.config.compute.catalog_type |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 32 | self.build_interval = self.config.compute.build_interval |
| 33 | self.build_timeout = self.config.compute.build_timeout |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 34 | |
| 35 | def create_image(self, server_id, name, meta=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 36 | """Creates an image of the original server.""" |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 37 | |
| 38 | post_body = { |
| 39 | 'createImage': { |
| 40 | 'name': name, |
| 41 | } |
| 42 | } |
| 43 | |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 44 | if meta is not None: |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 45 | post_body['createImage']['metadata'] = meta |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 46 | |
| 47 | post_body = json.dumps(post_body) |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 48 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 49 | post_body, self.headers) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 50 | return resp, body |
| 51 | |
| 52 | def list_images(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 53 | """Returns a list of all images filtered by any parameters.""" |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 54 | url = 'images' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 55 | if params: |
| 56 | url += '?%s' % urllib.urlencode(params) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 57 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 58 | resp, body = self.get(url) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 59 | body = json.loads(body) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 60 | return resp, body['images'] |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 61 | |
| 62 | def list_images_with_detail(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 63 | """Returns a detailed list of images filtered by any parameters.""" |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 64 | url = 'images/detail' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 65 | if params: |
| 66 | url += '?%s' % urllib.urlencode(params) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 67 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 68 | resp, body = self.get(url) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 69 | body = json.loads(body) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 70 | return resp, body['images'] |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 71 | |
| 72 | def get_image(self, image_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 73 | """Returns the details of a single image.""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 74 | resp, body = self.get("images/%s" % str(image_id)) |
Attila Fazekas | 54a4286 | 2013-07-28 22:31:06 +0200 | [diff] [blame] | 75 | self.expected_success(200, resp) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 76 | body = json.loads(body) |
| 77 | return resp, body['image'] |
| 78 | |
| 79 | def delete_image(self, image_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 80 | """Deletes the provided image.""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 81 | return self.delete("images/%s" % str(image_id)) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 82 | |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 83 | def wait_for_image_status(self, image_id, status): |
| 84 | """Waits for an image to reach a given status.""" |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 85 | waiters.wait_for_image_status(self, image_id, status) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 86 | |
| 87 | def list_image_metadata(self, image_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 88 | """Lists all metadata items for an image.""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 89 | resp, body = self.get("images/%s/metadata" % str(image_id)) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 90 | body = json.loads(body) |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 91 | return resp, body['metadata'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 92 | |
| 93 | def set_image_metadata(self, image_id, meta): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 94 | """Sets the metadata for an image.""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 95 | post_body = json.dumps({'metadata': meta}) |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 96 | resp, body = self.put('images/%s/metadata' % str(image_id), |
| 97 | post_body, self.headers) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 98 | body = json.loads(body) |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 99 | return resp, body['metadata'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 100 | |
| 101 | def update_image_metadata(self, image_id, meta): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 102 | """Updates the metadata for an image.""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 103 | post_body = json.dumps({'metadata': meta}) |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 104 | resp, body = self.post('images/%s/metadata' % str(image_id), |
| 105 | post_body, self.headers) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 106 | body = json.loads(body) |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 107 | return resp, body['metadata'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 108 | |
| 109 | def get_image_metadata_item(self, image_id, key): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 110 | """Returns the value for a specific image metadata key.""" |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 111 | resp, body = self.get("images/%s/metadata/%s" % (str(image_id), key)) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 112 | body = json.loads(body) |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 113 | return resp, body['meta'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 114 | |
| 115 | def set_image_metadata_item(self, image_id, key, meta): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 116 | """Sets the value for a specific image metadata key.""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 117 | post_body = json.dumps({'meta': meta}) |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 118 | resp, body = self.put('images/%s/metadata/%s' % (str(image_id), key), |
| 119 | post_body, self.headers) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 120 | body = json.loads(body) |
Daryl Walleck | 416af92 | 2011-11-22 22:28:33 -0600 | [diff] [blame] | 121 | return resp, body['meta'] |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 122 | |
| 123 | def delete_image_metadata_item(self, image_id, key): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 124 | """Deletes a single image metadata key/value pair.""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 125 | resp, body = self.delete("images/%s/metadata/%s" % |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 126 | (str(image_id), key)) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 127 | return resp, body |
Matthew Treinish | 0d66049 | 2013-06-04 17:26:09 -0400 | [diff] [blame] | 128 | |
| 129 | def is_resource_deleted(self, id): |
| 130 | try: |
| 131 | self.get_image(id) |
| 132 | except exceptions.NotFound: |
| 133 | return True |
| 134 | return False |