blob: d288982116022740b798f92813993490b5518a1a [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
Daryl Wallecke5b83d42011-11-10 14:39:02 -060016import json
Daryl Wallecke5b83d42011-11-10 14:39:02 -060017
Matthew Treinish89128142015-04-23 10:44:30 -040018from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090019from tempest_lib import exceptions as lib_exc
20
ghanshyamaa93b4b2015-03-20 11:03:44 +090021from tempest.api_schema.response.compute.v2_1 import images as schema
David Kranza5299eb2015-01-15 17:24:05 -050022from tempest.common import service_client
Matt Riedemannc00f3262013-12-14 12:03:55 -080023from tempest.common import waiters
Matthew Treinish684d8992014-01-30 16:27:40 +000024
Daryl Wallecke5b83d42011-11-10 14:39:02 -060025
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000026class ImagesClientJSON(service_client.ServiceClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060027
28 def create_image(self, server_id, name, meta=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050029 """Creates an image of the original server."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060030
31 post_body = {
32 'createImage': {
33 'name': name,
34 }
35 }
36
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080037 if meta is not None:
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060038 post_body['createImage']['metadata'] = meta
Daryl Wallecke5b83d42011-11-10 14:39:02 -060039
40 post_body = json.dumps(post_body)
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000041 resp, body = self.post('servers/%s/action' % server_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020042 post_body)
Ghanshyamfa9d39f2014-03-28 12:38:43 +090043 self.validate_response(schema.create_image, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050044 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060045
Ken'ichi Ohmichi0943d9b2015-06-17 02:27:05 +000046 def list_images(self, detail=False, **params):
Sean Daguef237ccb2013-01-04 15:19:14 -050047 """Returns a list of all images filtered by any parameters."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060048 url = 'images'
Ken'ichi Ohmichi0943d9b2015-06-17 02:27:05 +000049 _schema = schema.list_images
50 if detail:
51 url += '/detail'
52 _schema = schema.list_images_details
53
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050054 if params:
55 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060056
chris fattarsi5098fa22012-04-17 13:27:00 -070057 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060058 body = json.loads(body)
Ken'ichi Ohmichi0943d9b2015-06-17 02:27:05 +000059 self.validate_response(_schema, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050060 return service_client.ResponseBodyList(resp, body['images'])
Daryl Wallecke5b83d42011-11-10 14:39:02 -060061
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000062 def show_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050063 """Returns the details of a single image."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000064 resp, body = self.get("images/%s" % image_id)
TimurNurlygayanov8f798502014-08-08 15:09:32 +040065 self.expected_success(200, resp.status)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060066 body = json.loads(body)
Ghanshyamd34326c2014-03-19 12:18:53 +090067 self.validate_response(schema.get_image, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050068 return service_client.ResponseBody(resp, body['image'])
Daryl Wallecke5b83d42011-11-10 14:39:02 -060069
70 def delete_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050071 """Deletes the provided image."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000072 resp, body = self.delete("images/%s" % image_id)
Ghanshyamfa9d39f2014-03-28 12:38:43 +090073 self.validate_response(schema.delete, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050074 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060075
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060076 def wait_for_image_status(self, image_id, status):
77 """Waits for an image to reach a given status."""
Matt Riedemannc00f3262013-12-14 12:03:55 -080078 waiters.wait_for_image_status(self, image_id, status)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060079
80 def list_image_metadata(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050081 """Lists all metadata items for an image."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000082 resp, body = self.get("images/%s/metadata" % image_id)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060083 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +090084 self.validate_response(schema.image_metadata, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050085 return service_client.ResponseBody(resp, body['metadata'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060086
87 def set_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -050088 """Sets the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060089 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000090 resp, body = self.put('images/%s/metadata' % image_id, post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060091 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +090092 self.validate_response(schema.image_metadata, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050093 return service_client.ResponseBody(resp, body['metadata'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060094
95 def update_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -050096 """Updates the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060097 post_body = json.dumps({'metadata': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000098 resp, body = self.post('images/%s/metadata' % image_id, post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060099 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +0900100 self.validate_response(schema.image_metadata, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500101 return service_client.ResponseBody(resp, body['metadata'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600102
Ken'ichi Ohmichi0943d9b2015-06-17 02:27:05 +0000103 def show_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -0500104 """Returns the value for a specific image metadata key."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000105 resp, body = self.get("images/%s/metadata/%s" % (image_id, key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600106 body = json.loads(body)
Ghanshyam76c00f02014-03-28 13:02:22 +0900107 self.validate_response(schema.image_meta_item, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500108 return service_client.ResponseBody(resp, body['meta'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600109
110 def set_image_metadata_item(self, image_id, key, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -0500111 """Sets the value for a specific image metadata key."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600112 post_body = json.dumps({'meta': meta})
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000113 resp, body = self.put('images/%s/metadata/%s' % (image_id, key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200114 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600115 body = json.loads(body)
Ghanshyam76c00f02014-03-28 13:02:22 +0900116 self.validate_response(schema.image_meta_item, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500117 return service_client.ResponseBody(resp, body['meta'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600118
119 def delete_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -0500120 """Deletes a single image metadata key/value pair."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700121 resp, body = self.delete("images/%s/metadata/%s" %
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000122 (image_id, key))
Ghanshyamfa9d39f2014-03-28 12:38:43 +0900123 self.validate_response(schema.delete, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500124 return service_client.ResponseBody(resp, body)
Matthew Treinish0d660492013-06-04 17:26:09 -0400125
126 def is_resource_deleted(self, id):
127 try:
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +0000128 self.show_image(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900129 except lib_exc.NotFound:
Matthew Treinish0d660492013-06-04 17:26:09 -0400130 return True
131 return False
Matt Riedemannd2b96512014-10-13 10:18:16 -0700132
133 @property
134 def resource_type(self):
135 """Returns the primary type of resource this client works with."""
136 return 'image'