blob: 079a91e74893288fbac7daf1b70ce61245665e27 [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
Haiwei Xuab924622014-03-05 04:33:51 +090020from tempest.common import rest_client
Matt Riedemannc00f3262013-12-14 12:03:55 -080021from tempest.common import waiters
Matthew Treinish684d8992014-01-30 16:27:40 +000022from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023from tempest import exceptions
24
Matthew Treinish684d8992014-01-30 16:27:40 +000025CONF = config.CONF
26
Daryl Wallecke5b83d42011-11-10 14:39:02 -060027
Haiwei Xuab924622014-03-05 04:33:51 +090028class ImagesClientJSON(rest_client.RestClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060029
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000030 def __init__(self, auth_provider):
31 super(ImagesClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000032 self.service = CONF.compute.catalog_type
33 self.build_interval = CONF.compute.build_interval
34 self.build_timeout = CONF.compute.build_timeout
Daryl Wallecke5b83d42011-11-10 14:39:02 -060035
36 def create_image(self, server_id, name, meta=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050037 """Creates an image of the original server."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060038
39 post_body = {
40 'createImage': {
41 'name': name,
42 }
43 }
44
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080045 if meta is not None:
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060046 post_body['createImage']['metadata'] = meta
Daryl Wallecke5b83d42011-11-10 14:39:02 -060047
48 post_body = json.dumps(post_body)
Zhongyue Luoe0884a32012-09-25 17:24:17 +080049 resp, body = self.post('servers/%s/action' % str(server_id),
vponomaryovf4c27f92014-02-18 10:56:42 +020050 post_body)
Ghanshyamfa9d39f2014-03-28 12:38:43 +090051 self.validate_response(schema.create_image, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060052 return resp, body
53
54 def list_images(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050055 """Returns a list of all images filtered by any parameters."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060056 url = 'images'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050057 if params:
58 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060059
chris fattarsi5098fa22012-04-17 13:27:00 -070060 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060061 body = json.loads(body)
Ghanshyamfb67f062014-03-24 10:20:57 +090062 self.validate_response(schema.list_images, resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060063 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060064
65 def list_images_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050066 """Returns a detailed list of images filtered by any parameters."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060067 url = 'images/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050068 if params:
69 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060070
chris fattarsi5098fa22012-04-17 13:27:00 -070071 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060072 body = json.loads(body)
Ghanshyam2b14d422014-03-24 10:46:33 +090073 self.validate_response(schema.list_images_details, resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060074 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060075
76 def get_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050077 """Returns the details of a single image."""
chris fattarsi5098fa22012-04-17 13:27:00 -070078 resp, body = self.get("images/%s" % str(image_id))
TimurNurlygayanov8f798502014-08-08 15:09:32 +040079 self.expected_success(200, resp.status)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060080 body = json.loads(body)
Ghanshyamd34326c2014-03-19 12:18:53 +090081 self.validate_response(schema.get_image, resp, body)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060082 return resp, body['image']
83
84 def delete_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050085 """Deletes the provided image."""
Ghanshyamfa9d39f2014-03-28 12:38:43 +090086 resp, body = self.delete("images/%s" % str(image_id))
87 self.validate_response(schema.delete, resp, body)
88 return resp, body
Daryl Wallecke5b83d42011-11-10 14:39:02 -060089
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060090 def wait_for_image_status(self, image_id, status):
91 """Waits for an image to reach a given status."""
Matt Riedemannc00f3262013-12-14 12:03:55 -080092 waiters.wait_for_image_status(self, image_id, status)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060093
94 def list_image_metadata(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050095 """Lists all metadata items for an image."""
chris fattarsi5098fa22012-04-17 13:27:00 -070096 resp, body = self.get("images/%s/metadata" % str(image_id))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060097 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +090098 self.validate_response(schema.image_metadata, resp, body)
Daryl Walleck416af922011-11-22 22:28:33 -060099 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600100
101 def set_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -0500102 """Sets the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600103 post_body = json.dumps({'metadata': meta})
vponomaryovf4c27f92014-02-18 10:56:42 +0200104 resp, body = self.put('images/%s/metadata' % str(image_id), post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600105 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +0900106 self.validate_response(schema.image_metadata, resp, body)
Daryl Walleck416af922011-11-22 22:28:33 -0600107 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600108
109 def update_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -0500110 """Updates the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600111 post_body = json.dumps({'metadata': meta})
vponomaryovf4c27f92014-02-18 10:56:42 +0200112 resp, body = self.post('images/%s/metadata' % str(image_id), post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600113 body = json.loads(body)
Ghanshyamf7873b02014-03-28 12:50:57 +0900114 self.validate_response(schema.image_metadata, resp, body)
Daryl Walleck416af922011-11-22 22:28:33 -0600115 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600116
117 def get_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -0500118 """Returns the value for a specific image metadata key."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800119 resp, body = self.get("images/%s/metadata/%s" % (str(image_id), key))
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)
Daryl Walleck416af922011-11-22 22:28:33 -0600122 return resp, body['meta']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600123
124 def set_image_metadata_item(self, image_id, key, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -0500125 """Sets the value for a specific image metadata key."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600126 post_body = json.dumps({'meta': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800127 resp, body = self.put('images/%s/metadata/%s' % (str(image_id), key),
vponomaryovf4c27f92014-02-18 10:56:42 +0200128 post_body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600129 body = json.loads(body)
Ghanshyam76c00f02014-03-28 13:02:22 +0900130 self.validate_response(schema.image_meta_item, resp, body)
Daryl Walleck416af922011-11-22 22:28:33 -0600131 return resp, body['meta']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600132
133 def delete_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -0500134 """Deletes a single image metadata key/value pair."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700135 resp, body = self.delete("images/%s/metadata/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800136 (str(image_id), key))
Ghanshyamfa9d39f2014-03-28 12:38:43 +0900137 self.validate_response(schema.delete, resp, body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600138 return resp, body
Matthew Treinish0d660492013-06-04 17:26:09 -0400139
140 def is_resource_deleted(self, id):
141 try:
142 self.get_image(id)
143 except exceptions.NotFound:
144 return True
145 return False
Matt Riedemannd2b96512014-10-13 10:18:16 -0700146
147 @property
148 def resource_type(self):
149 """Returns the primary type of resource this client works with."""
150 return 'image'