blob: a5755da45a1e9f7a99110d94f0c87f02975c75be [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
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050017import urllib
Daryl Wallecke5b83d42011-11-10 14:39:02 -060018
Marc Koderer6fbd74f2014-08-04 09:38:19 +020019from tempest.api_schema.response.compute.v2 import images as schema
David Kranza5299eb2015-01-15 17:24:05 -050020from tempest.common import service_client
Matt Riedemannc00f3262013-12-14 12:03:55 -080021from tempest.common import waiters
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022from tempest import exceptions
Matthew Treinish684d8992014-01-30 16:27:40 +000023
Daryl Wallecke5b83d42011-11-10 14:39:02 -060024
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000025class ImagesClientJSON(service_client.ServiceClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060026
27 def create_image(self, server_id, name, meta=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050028 """Creates an image of the original server."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060029
30 post_body = {
31 'createImage': {
32 'name': name,
33 }
34 }
35
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080036 if meta is not None:
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060037 post_body['createImage']['metadata'] = meta
Daryl Wallecke5b83d42011-11-10 14:39:02 -060038
39 post_body = json.dumps(post_body)
Zhongyue Luoe0884a32012-09-25 17:24:17 +080040 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +020041 post_body)
Ghanshyamfa9d39f2014-03-28 12:38:43 +090042 self.validate_response(schema.create_image, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050043 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060044
45 def list_images(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050046 """Returns a list of all images filtered by any parameters."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060047 url = 'images'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050048 if params:
49 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060050
chris fattarsi5098fa22012-04-17 13:27:00 -070051 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060052 body = json.loads(body)
Ghanshyamfb67f062014-03-24 10:20:57 +090053 self.validate_response(schema.list_images, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050054 return service_client.ResponseBodyList(resp, body['images'])
Daryl Wallecke5b83d42011-11-10 14:39:02 -060055
56 def list_images_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050057 """Returns a detailed list of images filtered by any parameters."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060058 url = 'images/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050059 if params:
60 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060061
chris fattarsi5098fa22012-04-17 13:27:00 -070062 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060063 body = json.loads(body)
Ghanshyam2b14d422014-03-24 10:46:33 +090064 self.validate_response(schema.list_images_details, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050065 return service_client.ResponseBodyList(resp, body['images'])
Daryl Wallecke5b83d42011-11-10 14:39:02 -060066
67 def get_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050068 """Returns the details of a single image."""
chris fattarsi5098fa22012-04-17 13:27:00 -070069 resp, body = self.get("images/%s" % str(image_id))
TimurNurlygayanov8f798502014-08-08 15:09:32 +040070 self.expected_success(200, resp.status)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060071 body = json.loads(body)
Ghanshyamd34326c2014-03-19 12:18:53 +090072 self.validate_response(schema.get_image, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050073 return service_client.ResponseBody(resp, body['image'])
Daryl Wallecke5b83d42011-11-10 14:39:02 -060074
75 def delete_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050076 """Deletes the provided image."""
Ghanshyamfa9d39f2014-03-28 12:38:43 +090077 resp, body = self.delete("images/%s" % str(image_id))
78 self.validate_response(schema.delete, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050079 return service_client.ResponseBody(resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060080
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060081 def wait_for_image_status(self, image_id, status):
82 """Waits for an image to reach a given status."""
Matt Riedemannc00f3262013-12-14 12:03:55 -080083 waiters.wait_for_image_status(self, image_id, status)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060084
85 def list_image_metadata(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050086 """Lists all metadata items for an image."""
chris fattarsi5098fa22012-04-17 13:27:00 -070087 resp, body = self.get("images/%s/metadata" % str(image_id))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060088 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +090089 self.validate_response(schema.image_metadata, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050090 return service_client.ResponseBody(resp, body['metadata'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060091
92 def set_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -050093 """Sets the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060094 post_body = json.dumps({'metadata': meta})
vponomaryovf4c27f92014-02-18 10:56:42 +020095 resp, body = self.put('images/%s/metadata' % str(image_id), post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060096 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +090097 self.validate_response(schema.image_metadata, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -050098 return service_client.ResponseBody(resp, body['metadata'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060099
100 def update_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -0500101 """Updates the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600102 post_body = json.dumps({'metadata': meta})
vponomaryovf4c27f92014-02-18 10:56:42 +0200103 resp, body = self.post('images/%s/metadata' % str(image_id), post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600104 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +0900105 self.validate_response(schema.image_metadata, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500106 return service_client.ResponseBody(resp, body['metadata'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600107
108 def get_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -0500109 """Returns the value for a specific image metadata key."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800110 resp, body = self.get("images/%s/metadata/%s" % (str(image_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600111 body = json.loads(body)
Ghanshyam76c00f02014-03-28 13:02:22 +0900112 self.validate_response(schema.image_meta_item, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500113 return service_client.ResponseBody(resp, body['meta'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600114
115 def set_image_metadata_item(self, image_id, key, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -0500116 """Sets the value for a specific image metadata key."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600117 post_body = json.dumps({'meta': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800118 resp, body = self.put('images/%s/metadata/%s' % (str(image_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200119 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600120 body = json.loads(body)
Ghanshyam76c00f02014-03-28 13:02:22 +0900121 self.validate_response(schema.image_meta_item, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500122 return service_client.ResponseBody(resp, body['meta'])
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600123
124 def delete_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -0500125 """Deletes a single image metadata key/value pair."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700126 resp, body = self.delete("images/%s/metadata/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800127 (str(image_id), key))
Ghanshyamfa9d39f2014-03-28 12:38:43 +0900128 self.validate_response(schema.delete, resp, body)
David Kranza5299eb2015-01-15 17:24:05 -0500129 return service_client.ResponseBody(resp, body)
Matthew Treinish0d660492013-06-04 17:26:09 -0400130
131 def is_resource_deleted(self, id):
132 try:
133 self.get_image(id)
134 except exceptions.NotFound:
135 return True
136 return False
Matt Riedemannd2b96512014-10-13 10:18:16 -0700137
138 @property
139 def resource_type(self):
140 """Returns the primary type of resource this client works with."""
141 return 'image'