ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 16 | import json |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 17 | |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 18 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 19 | from tempest_lib import exceptions as lib_exc |
| 20 | |
ghanshyam | aa93b4b | 2015-03-20 11:03:44 +0900 | [diff] [blame] | 21 | from tempest.api_schema.response.compute.v2_1 import images as schema |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 22 | from tempest.common import service_client |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 23 | from tempest.common import waiters |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 24 | |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 25 | |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 26 | class ImagesClientJSON(service_client.ServiceClient): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 27 | |
| 28 | def create_image(self, server_id, name, meta=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 29 | """Creates an image of the original server.""" |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 30 | |
| 31 | post_body = { |
| 32 | 'createImage': { |
| 33 | 'name': name, |
| 34 | } |
| 35 | } |
| 36 | |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 37 | if meta is not None: |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 38 | post_body['createImage']['metadata'] = meta |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 39 | |
| 40 | post_body = json.dumps(post_body) |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 41 | resp, body = self.post('servers/%s/action' % server_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 42 | post_body) |
Ghanshyam | fa9d39f | 2014-03-28 12:38:43 +0900 | [diff] [blame] | 43 | self.validate_response(schema.create_image, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 44 | return service_client.ResponseBody(resp, body) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 45 | |
Ken'ichi Ohmichi | 0943d9b | 2015-06-17 02:27:05 +0000 | [diff] [blame] | 46 | def list_images(self, detail=False, **params): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 47 | """Returns a list of all images filtered by any parameters.""" |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 48 | url = 'images' |
Ken'ichi Ohmichi | 0943d9b | 2015-06-17 02:27:05 +0000 | [diff] [blame] | 49 | _schema = schema.list_images |
| 50 | if detail: |
| 51 | url += '/detail' |
| 52 | _schema = schema.list_images_details |
| 53 | |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 54 | if params: |
| 55 | url += '?%s' % urllib.urlencode(params) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 56 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 57 | resp, body = self.get(url) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 58 | body = json.loads(body) |
Ken'ichi Ohmichi | 0943d9b | 2015-06-17 02:27:05 +0000 | [diff] [blame] | 59 | self.validate_response(_schema, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 60 | return service_client.ResponseBodyList(resp, body['images']) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 61 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 62 | def show_image(self, image_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 63 | """Returns the details of a single image.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 64 | resp, body = self.get("images/%s" % image_id) |
TimurNurlygayanov | 8f79850 | 2014-08-08 15:09:32 +0400 | [diff] [blame] | 65 | self.expected_success(200, resp.status) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 66 | body = json.loads(body) |
Ghanshyam | d34326c | 2014-03-19 12:18:53 +0900 | [diff] [blame] | 67 | self.validate_response(schema.get_image, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 68 | return service_client.ResponseBody(resp, body['image']) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 69 | |
| 70 | def delete_image(self, image_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 71 | """Deletes the provided image.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 72 | resp, body = self.delete("images/%s" % image_id) |
Ghanshyam | fa9d39f | 2014-03-28 12:38:43 +0900 | [diff] [blame] | 73 | self.validate_response(schema.delete, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 74 | return service_client.ResponseBody(resp, body) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 75 | |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 76 | def wait_for_image_status(self, image_id, status): |
| 77 | """Waits for an image to reach a given status.""" |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 78 | waiters.wait_for_image_status(self, image_id, status) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 79 | |
| 80 | def list_image_metadata(self, image_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 81 | """Lists all metadata items for an image.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 82 | resp, body = self.get("images/%s/metadata" % image_id) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 83 | body = json.loads(body) |
Ghanshyam | f7873b0 | 2014-03-28 12:50:57 +0900 | [diff] [blame] | 84 | self.validate_response(schema.image_metadata, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 85 | return service_client.ResponseBody(resp, body['metadata']) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 86 | |
| 87 | def set_image_metadata(self, image_id, meta): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 88 | """Sets the metadata for an image.""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 89 | post_body = json.dumps({'metadata': meta}) |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 90 | resp, body = self.put('images/%s/metadata' % image_id, post_body) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 91 | body = json.loads(body) |
Ghanshyam | f7873b0 | 2014-03-28 12:50:57 +0900 | [diff] [blame] | 92 | self.validate_response(schema.image_metadata, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 93 | return service_client.ResponseBody(resp, body['metadata']) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 94 | |
| 95 | def update_image_metadata(self, image_id, meta): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 96 | """Updates the metadata for an image.""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 97 | post_body = json.dumps({'metadata': meta}) |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 98 | resp, body = self.post('images/%s/metadata' % image_id, post_body) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 99 | body = json.loads(body) |
Ghanshyam | f7873b0 | 2014-03-28 12:50:57 +0900 | [diff] [blame] | 100 | self.validate_response(schema.image_metadata, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 101 | return service_client.ResponseBody(resp, body['metadata']) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 102 | |
Ken'ichi Ohmichi | 0943d9b | 2015-06-17 02:27:05 +0000 | [diff] [blame] | 103 | def show_image_metadata_item(self, image_id, key): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 104 | """Returns the value for a specific image metadata key.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 105 | resp, body = self.get("images/%s/metadata/%s" % (image_id, key)) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 106 | body = json.loads(body) |
Ghanshyam | 76c00f0 | 2014-03-28 13:02:22 +0900 | [diff] [blame] | 107 | self.validate_response(schema.image_meta_item, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 108 | return service_client.ResponseBody(resp, body['meta']) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 109 | |
| 110 | def set_image_metadata_item(self, image_id, key, meta): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 111 | """Sets the value for a specific image metadata key.""" |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 112 | post_body = json.dumps({'meta': meta}) |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 113 | resp, body = self.put('images/%s/metadata/%s' % (image_id, key), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 114 | post_body) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 115 | body = json.loads(body) |
Ghanshyam | 76c00f0 | 2014-03-28 13:02:22 +0900 | [diff] [blame] | 116 | self.validate_response(schema.image_meta_item, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 117 | return service_client.ResponseBody(resp, body['meta']) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 118 | |
| 119 | def delete_image_metadata_item(self, image_id, key): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 120 | """Deletes a single image metadata key/value pair.""" |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 121 | resp, body = self.delete("images/%s/metadata/%s" % |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 122 | (image_id, key)) |
Ghanshyam | fa9d39f | 2014-03-28 12:38:43 +0900 | [diff] [blame] | 123 | self.validate_response(schema.delete, resp, body) |
David Kranz | a5299eb | 2015-01-15 17:24:05 -0500 | [diff] [blame] | 124 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | 0d66049 | 2013-06-04 17:26:09 -0400 | [diff] [blame] | 125 | |
| 126 | def is_resource_deleted(self, id): |
| 127 | try: |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 128 | self.show_image(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 129 | except lib_exc.NotFound: |
Matthew Treinish | 0d66049 | 2013-06-04 17:26:09 -0400 | [diff] [blame] | 130 | return True |
| 131 | return False |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 132 | |
| 133 | @property |
| 134 | def resource_type(self): |
| 135 | """Returns the primary type of resource this client works with.""" |
| 136 | return 'image' |