blob: b0ce2dce275c29966a02e69ef00e3ac7d54597a8 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05002# 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
Matthew Treinish21905512015-07-13 10:33:35 -040016from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040017from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090018from tempest_lib import exceptions as lib_exc
19
ghanshyamaa93b4b2015-03-20 11:03:44 +090020from tempest.api_schema.response.compute.v2_1 import images as schema
David Kranza5299eb2015-01-15 17:24:05 -050021from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000022
Daryl Wallecke5b83d42011-11-10 14:39:02 -060023
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000024class ImagesClient(service_client.ServiceClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060025
26 def create_image(self, server_id, name, meta=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050027 """Creates an image of the original server."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060028
29 post_body = {
30 'createImage': {
31 'name': name,
32 }
33 }
34
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080035 if meta is not None:
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060036 post_body['createImage']['metadata'] = meta
Daryl Wallecke5b83d42011-11-10 14:39:02 -060037
38 post_body = json.dumps(post_body)
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000039 resp, body = self.post('servers/%s/action' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020040 post_body)
Ghanshyamfa9d39f2014-03-28 12:38:43 +090041 self.validate_response(schema.create_image, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050042 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060043
Ken'ichi Ohmichi0943d9b2015-06-17 02:27:05 +000044 def list_images(self, detail=False, **params):
Sean Daguef237ccb2013-01-04 15:19:14 -050045 """Returns a list of all images filtered by any parameters."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060046 url = 'images'
Ken'ichi Ohmichi0943d9b2015-06-17 02:27:05 +000047 _schema = schema.list_images
48 if detail:
49 url += '/detail'
50 _schema = schema.list_images_details
51
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050052 if params:
53 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060054
chris fattarsi5098fa22012-04-17 13:27:00 -070055 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060056 body = json.loads(body)
Ken'ichi Ohmichi0943d9b2015-06-17 02:27:05 +000057 self.validate_response(_schema, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050058 return service_client.ResponseBodyList(resp, body['images'])
Daryl Wallecke5b83d42011-11-10 14:39:02 -060059
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000060 def show_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050061 """Returns the details of a single image."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000062 resp, body = self.get("images/%s" % image_id)
TimurNurlygayanov8f798502014-08-08 15:09:32 +040063 self.expected_success(200, resp.status)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060064 body = json.loads(body)
Ghanshyamd34326c2014-03-19 12:18:53 +090065 self.validate_response(schema.get_image, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050066 return service_client.ResponseBody(resp, body['image'])
Daryl Wallecke5b83d42011-11-10 14:39:02 -060067
68 def delete_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050069 """Deletes the provided image."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000070 resp, body = self.delete("images/%s" % image_id)
Ghanshyamfa9d39f2014-03-28 12:38:43 +090071 self.validate_response(schema.delete, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050072 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060073
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060074 def list_image_metadata(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050075 """Lists all metadata items for an image."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000076 resp, body = self.get("images/%s/metadata" % image_id)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060077 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +090078 self.validate_response(schema.image_metadata, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050079 return service_client.ResponseBody(resp, body['metadata'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060080
81 def set_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -050082 """Sets the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060083 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000084 resp, body = self.put('images/%s/metadata' % image_id, post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060085 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +090086 self.validate_response(schema.image_metadata, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050087 return service_client.ResponseBody(resp, body['metadata'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060088
89 def update_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -050090 """Updates the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060091 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000092 resp, body = self.post('images/%s/metadata' % image_id, post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060093 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +090094 self.validate_response(schema.image_metadata, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050095 return service_client.ResponseBody(resp, body['metadata'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060096
Ken'ichi Ohmichi0943d9b2015-06-17 02:27:05 +000097 def show_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -050098 """Returns the value for a specific image metadata key."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000099 resp, body = self.get("images/%s/metadata/%s" % (image_id, key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600100 body = json.loads(body)
Ghanshyam76c00f02014-03-28 13:02:22 +0900101 self.validate_response(schema.image_meta_item, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500102 return service_client.ResponseBody(resp, body['meta'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600103
104 def set_image_metadata_item(self, image_id, key, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -0500105 """Sets the value for a specific image metadata key."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600106 post_body = json.dumps({'meta': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000107 resp, body = self.put('images/%s/metadata/%s' % (image_id, key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200108 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600109 body = json.loads(body)
Ghanshyam76c00f02014-03-28 13:02:22 +0900110 self.validate_response(schema.image_meta_item, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500111 return service_client.ResponseBody(resp, body['meta'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600112
113 def delete_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -0500114 """Deletes a single image metadata key/value pair."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700115 resp, body = self.delete("images/%s/metadata/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000116 (image_id, key))
Ghanshyamfa9d39f2014-03-28 12:38:43 +0900117 self.validate_response(schema.delete, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500118 return service_client.ResponseBody(resp, body)
Matthew Treinish0d660492013-06-04 17:26:09 -0400119
120 def is_resource_deleted(self, id):
121 try:
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +0000122 self.show_image(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900123 except lib_exc.NotFound:
Matthew Treinish0d660492013-06-04 17:26:09 -0400124 return True
125 return False
Matt Riedemannd2b96512014-10-13 10:18:16 -0700126
127 @property
128 def resource_type(self):
129 """Returns the primary type of resource this client works with."""
130 return 'image'