blob: c05571a097b982147128f7036f582caaa6c92a0a [file] [log] [blame]
dwallecke62b9f02012-10-10 23:34:42 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Daryl Wallecke5b83d42011-11-10 14:39:02 -060018import json
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050019import urllib
Daryl Wallecke5b83d42011-11-10 14:39:02 -060020
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021from tempest.common.rest_client import RestClient
Matt Riedemannc00f3262013-12-14 12:03:55 -080022from tempest.common import waiters
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023from tempest import exceptions
24
Daryl Wallecke5b83d42011-11-10 14:39:02 -060025
Mauro S. M. Rodrigues6e373242012-08-27 18:59:19 -040026class ImagesClientJSON(RestClient):
Daryl Wallecke5b83d42011-11-10 14:39:02 -060027
Daryl Walleck587385b2012-03-03 13:00:26 -060028 def __init__(self, config, username, password, auth_url, tenant_name=None):
Mauro S. M. Rodrigues6e373242012-08-27 18:59:19 -040029 super(ImagesClientJSON, self).__init__(config, username, password,
30 auth_url, tenant_name)
chris fattarsi5098fa22012-04-17 13:27:00 -070031 self.service = self.config.compute.catalog_type
Daryl Walleck587385b2012-03-03 13:00:26 -060032 self.build_interval = self.config.compute.build_interval
33 self.build_timeout = self.config.compute.build_timeout
Daryl Wallecke5b83d42011-11-10 14:39:02 -060034
35 def create_image(self, server_id, name, meta=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050036 """Creates an image of the original server."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060037
38 post_body = {
39 'createImage': {
40 'name': name,
41 }
42 }
43
Zhongyue Luoe471d6e2012-09-17 17:02:43 +080044 if meta is not None:
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060045 post_body['createImage']['metadata'] = meta
Daryl Wallecke5b83d42011-11-10 14:39:02 -060046
47 post_body = json.dumps(post_body)
Zhongyue Luoe0884a32012-09-25 17:24:17 +080048 resp, body = self.post('servers/%s/action' % str(server_id),
49 post_body, self.headers)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060050 return resp, body
51
52 def list_images(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050053 """Returns a list of all images filtered by any parameters."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060054 url = 'images'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050055 if params:
56 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060057
chris fattarsi5098fa22012-04-17 13:27:00 -070058 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060059 body = json.loads(body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060060 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060061
62 def list_images_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050063 """Returns a detailed list of images filtered by any parameters."""
Daryl Wallecke5b83d42011-11-10 14:39:02 -060064 url = 'images/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050065 if params:
66 url += '?%s' % urllib.urlencode(params)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060067
chris fattarsi5098fa22012-04-17 13:27:00 -070068 resp, body = self.get(url)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060069 body = json.loads(body)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060070 return resp, body['images']
Daryl Wallecke5b83d42011-11-10 14:39:02 -060071
72 def get_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050073 """Returns the details of a single image."""
chris fattarsi5098fa22012-04-17 13:27:00 -070074 resp, body = self.get("images/%s" % str(image_id))
Attila Fazekas54a42862013-07-28 22:31:06 +020075 self.expected_success(200, resp)
Daryl Wallecke5b83d42011-11-10 14:39:02 -060076 body = json.loads(body)
77 return resp, body['image']
78
79 def delete_image(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050080 """Deletes the provided image."""
chris fattarsi5098fa22012-04-17 13:27:00 -070081 return self.delete("images/%s" % str(image_id))
Daryl Wallecke5b83d42011-11-10 14:39:02 -060082
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060083 def wait_for_image_status(self, image_id, status):
84 """Waits for an image to reach a given status."""
Matt Riedemannc00f3262013-12-14 12:03:55 -080085 waiters.wait_for_image_status(self, image_id, status)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060086
87 def list_image_metadata(self, image_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050088 """Lists all metadata items for an image."""
chris fattarsi5098fa22012-04-17 13:27:00 -070089 resp, body = self.get("images/%s/metadata" % str(image_id))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060090 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -060091 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060092
93 def set_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -050094 """Sets the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060095 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +080096 resp, body = self.put('images/%s/metadata' % str(image_id),
97 post_body, self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -060098 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -060099 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600100
101 def update_image_metadata(self, image_id, meta):
Sean Daguef237ccb2013-01-04 15:19:14 -0500102 """Updates the metadata for an image."""
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600103 post_body = json.dumps({'metadata': meta})
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800104 resp, body = self.post('images/%s/metadata' % str(image_id),
105 post_body, self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600106 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600107 return resp, body['metadata']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600108
109 def get_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -0500110 """Returns the value for a specific image metadata key."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800111 resp, body = self.get("images/%s/metadata/%s" % (str(image_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600112 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600113 return 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),
119 post_body, self.headers)
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600120 body = json.loads(body)
Daryl Walleck416af922011-11-22 22:28:33 -0600121 return resp, body['meta']
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600122
123 def delete_image_metadata_item(self, image_id, key):
Sean Daguef237ccb2013-01-04 15:19:14 -0500124 """Deletes a single image metadata key/value pair."""
chris fattarsi5098fa22012-04-17 13:27:00 -0700125 resp, body = self.delete("images/%s/metadata/%s" %
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800126 (str(image_id), key))
Daryl Walleck73a9e7a2011-11-15 17:43:31 -0600127 return resp, body
Matthew Treinish0d660492013-06-04 17:26:09 -0400128
129 def is_resource_deleted(self, id):
130 try:
131 self.get_image(id)
132 except exceptions.NotFound:
133 return True
134 return False