blob: 0f4eb42cb3ff9a458623c28feeda09e674058214 [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Copyright 2012 OpenStack Foundation
2# 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
16from oslo_serialization import jsonutils as json
17from six.moves.urllib import parse as urllib
18
19from tempest.lib.api_schema.response.compute.v2_1 import images as schema
Matt Riedemannf110a4b2018-01-08 15:03:36 -050020from tempest.lib.api_schema.response.compute.v2_45 import images as schemav245
Matthew Treinish9e26ca82016-02-23 11:43:20 -050021from tempest.lib.common import rest_client
22from tempest.lib import exceptions as lib_exc
Ghanshyamee9af302016-02-25 06:12:43 +090023from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050024
25
Ghanshyamee9af302016-02-25 06:12:43 +090026class ImagesClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050027
Matt Riedemannf110a4b2018-01-08 15:03:36 -050028 schema_versions_info = [
29 {'min': None, 'max': '2.44', 'schema': schema},
30 {'min': '2.45', 'max': None, 'schema': schemav245}]
31
Matthew Treinish9e26ca82016-02-23 11:43:20 -050032 def create_image(self, server_id, **kwargs):
33 """Create an image of the original server.
34
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090035 For a full list of available parameters, please refer to the official
36 API reference:
zhufl126a66d2017-03-08 15:32:21 +080037 https://developer.openstack.org/api-ref/compute/#create-image-createimage-action
Matthew Treinish9e26ca82016-02-23 11:43:20 -050038 """
39
40 post_body = {'createImage': kwargs}
41 post_body = json.dumps(post_body)
42 resp, body = self.post('servers/%s/action' % server_id,
43 post_body)
Matt Riedemannf110a4b2018-01-08 15:03:36 -050044 _schema = self.get_schema(self.schema_versions_info)
45 if body:
46 body = json.loads(body)
47 self.validate_response(_schema.create_image, resp, body)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050048 return rest_client.ResponseBody(resp, body)
49
50 def list_images(self, detail=False, **params):
51 """Return a list of all images filtered by any parameter.
52
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090053 For a full list of available parameters, please refer to the official
54 API reference:
zhufl126a66d2017-03-08 15:32:21 +080055 https://developer.openstack.org/api-ref/compute/#list-images
56 https://developer.openstack.org/api-ref/compute/#list-images-with-details
Matthew Treinish9e26ca82016-02-23 11:43:20 -050057 """
58 url = 'images'
59 _schema = schema.list_images
60 if detail:
61 url += '/detail'
62 _schema = schema.list_images_details
63
64 if params:
65 url += '?%s' % urllib.urlencode(params)
66
67 resp, body = self.get(url)
68 body = json.loads(body)
69 self.validate_response(_schema, resp, body)
70 return rest_client.ResponseBody(resp, body)
71
72 def show_image(self, image_id):
73 """Return the details of a single image."""
74 resp, body = self.get("images/%s" % image_id)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050075 body = json.loads(body)
76 self.validate_response(schema.get_image, resp, body)
77 return rest_client.ResponseBody(resp, body)
78
79 def delete_image(self, image_id):
80 """Delete the provided image."""
81 resp, body = self.delete("images/%s" % image_id)
82 self.validate_response(schema.delete, resp, body)
83 return rest_client.ResponseBody(resp, body)
84
85 def list_image_metadata(self, image_id):
86 """List all metadata items for an image."""
87 resp, body = self.get("images/%s/metadata" % image_id)
88 body = json.loads(body)
89 self.validate_response(schema.image_metadata, resp, body)
90 return rest_client.ResponseBody(resp, body)
91
92 def set_image_metadata(self, image_id, meta):
93 """Set the metadata for an image.
94
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090095 For a full list of available parameters, please refer to the official
96 API reference:
zhufl126a66d2017-03-08 15:32:21 +080097 https://developer.openstack.org/api-ref/compute/#update-image-metadata
Matthew Treinish9e26ca82016-02-23 11:43:20 -050098 """
99 post_body = json.dumps({'metadata': meta})
100 resp, body = self.put('images/%s/metadata' % image_id, post_body)
101 body = json.loads(body)
102 self.validate_response(schema.image_metadata, resp, body)
103 return rest_client.ResponseBody(resp, body)
104
105 def update_image_metadata(self, image_id, meta):
106 """Update the metadata for an image.
107
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +0900108 For a full list of available parameters, please refer to the official
109 API reference:
zhufl126a66d2017-03-08 15:32:21 +0800110 https://developer.openstack.org/api-ref/compute/#create-image-metadata
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500111 """
112 post_body = json.dumps({'metadata': meta})
113 resp, body = self.post('images/%s/metadata' % image_id, post_body)
114 body = json.loads(body)
115 self.validate_response(schema.image_metadata, resp, body)
116 return rest_client.ResponseBody(resp, body)
117
118 def show_image_metadata_item(self, image_id, key):
119 """Return the value for a specific image metadata key."""
120 resp, body = self.get("images/%s/metadata/%s" % (image_id, key))
121 body = json.loads(body)
122 self.validate_response(schema.image_meta_item, resp, body)
123 return rest_client.ResponseBody(resp, body)
124
125 def set_image_metadata_item(self, image_id, key, meta):
126 """Set the value for a specific image metadata key.
127
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +0900128 For a full list of available parameters, please refer to the official
129 API reference:
zhufl126a66d2017-03-08 15:32:21 +0800130 https://developer.openstack.org/api-ref/compute/#create-or-update-image-metadata-item
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500131 """
132 post_body = json.dumps({'meta': meta})
133 resp, body = self.put('images/%s/metadata/%s' % (image_id, key),
134 post_body)
135 body = json.loads(body)
136 self.validate_response(schema.image_meta_item, resp, body)
137 return rest_client.ResponseBody(resp, body)
138
139 def delete_image_metadata_item(self, image_id, key):
140 """Delete a single image metadata key/value pair."""
141 resp, body = self.delete("images/%s/metadata/%s" %
142 (image_id, key))
143 self.validate_response(schema.delete, resp, body)
144 return rest_client.ResponseBody(resp, body)
145
146 def is_resource_deleted(self, id):
Joshua White95dcef22016-04-11 06:17:42 -0700147 # Added status check for user with admin role
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500148 try:
Joshua White95dcef22016-04-11 06:17:42 -0700149 if self.show_image(id)['image']['status'] == 'DELETED':
150 return True
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500151 except lib_exc.NotFound:
152 return True
153 return False
154
155 @property
156 def resource_type(self):
157 """Return the primary type of resource this client works with."""
158 return 'image'