blob: da8a61eb504ff6daf39e83f83f03e8712839036a [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
20from tempest.lib.common import rest_client
21from tempest.lib import exceptions as lib_exc
Ghanshyamee9af302016-02-25 06:12:43 +090022from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050023
24
Ghanshyamee9af302016-02-25 06:12:43 +090025class ImagesClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050026
27 def create_image(self, server_id, **kwargs):
28 """Create an image of the original server.
29
30 Available params: see http://developer.openstack.org/
31 api-ref-compute-v2.1.html#createImage
32 """
33
34 post_body = {'createImage': kwargs}
35 post_body = json.dumps(post_body)
36 resp, body = self.post('servers/%s/action' % server_id,
37 post_body)
38 self.validate_response(schema.create_image, resp, body)
39 return rest_client.ResponseBody(resp, body)
40
41 def list_images(self, detail=False, **params):
42 """Return a list of all images filtered by any parameter.
43
44 Available params: see http://developer.openstack.org/
45 api-ref-compute-v2.1.html#listImages
46 """
47 url = 'images'
48 _schema = schema.list_images
49 if detail:
50 url += '/detail'
51 _schema = schema.list_images_details
52
53 if params:
54 url += '?%s' % urllib.urlencode(params)
55
56 resp, body = self.get(url)
57 body = json.loads(body)
58 self.validate_response(_schema, resp, body)
59 return rest_client.ResponseBody(resp, body)
60
61 def show_image(self, image_id):
62 """Return the details of a single image."""
63 resp, body = self.get("images/%s" % image_id)
64 self.expected_success(200, resp.status)
65 body = json.loads(body)
66 self.validate_response(schema.get_image, resp, body)
67 return rest_client.ResponseBody(resp, body)
68
69 def delete_image(self, image_id):
70 """Delete the provided image."""
71 resp, body = self.delete("images/%s" % image_id)
72 self.validate_response(schema.delete, resp, body)
73 return rest_client.ResponseBody(resp, body)
74
75 def list_image_metadata(self, image_id):
76 """List all metadata items for an image."""
77 resp, body = self.get("images/%s/metadata" % image_id)
78 body = json.loads(body)
79 self.validate_response(schema.image_metadata, resp, body)
80 return rest_client.ResponseBody(resp, body)
81
82 def set_image_metadata(self, image_id, meta):
83 """Set the metadata for an image.
84
85 Available params: see http://developer.openstack.org/
86 api-ref-compute-v2.1.html#createImageMetadata
87 """
88 post_body = json.dumps({'metadata': meta})
89 resp, body = self.put('images/%s/metadata' % image_id, post_body)
90 body = json.loads(body)
91 self.validate_response(schema.image_metadata, resp, body)
92 return rest_client.ResponseBody(resp, body)
93
94 def update_image_metadata(self, image_id, meta):
95 """Update the metadata for an image.
96
97 Available params: see http://developer.openstack.org/
98 api-ref-compute-v2.1.html#updateImageMetadata
99 """
100 post_body = json.dumps({'metadata': meta})
101 resp, body = self.post('images/%s/metadata' % image_id, post_body)
102 body = json.loads(body)
103 self.validate_response(schema.image_metadata, resp, body)
104 return rest_client.ResponseBody(resp, body)
105
106 def show_image_metadata_item(self, image_id, key):
107 """Return the value for a specific image metadata key."""
108 resp, body = self.get("images/%s/metadata/%s" % (image_id, key))
109 body = json.loads(body)
110 self.validate_response(schema.image_meta_item, resp, body)
111 return rest_client.ResponseBody(resp, body)
112
113 def set_image_metadata_item(self, image_id, key, meta):
114 """Set the value for a specific image metadata key.
115
116 Available params: see http://developer.openstack.org/
117 api-ref-compute-v2.1.html#setImageMetadataItem
118 """
119 post_body = json.dumps({'meta': meta})
120 resp, body = self.put('images/%s/metadata/%s' % (image_id, key),
121 post_body)
122 body = json.loads(body)
123 self.validate_response(schema.image_meta_item, resp, body)
124 return rest_client.ResponseBody(resp, body)
125
126 def delete_image_metadata_item(self, image_id, key):
127 """Delete a single image metadata key/value pair."""
128 resp, body = self.delete("images/%s/metadata/%s" %
129 (image_id, key))
130 self.validate_response(schema.delete, resp, body)
131 return rest_client.ResponseBody(resp, body)
132
133 def is_resource_deleted(self, id):
Joshua White95dcef22016-04-11 06:17:42 -0700134 # Added status check for user with admin role
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500135 try:
Joshua White95dcef22016-04-11 06:17:42 -0700136 if self.show_image(id)['image']['status'] == 'DELETED':
137 return True
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500138 except lib_exc.NotFound:
139 return True
140 return False
141
142 @property
143 def resource_type(self):
144 """Return the primary type of resource this client works with."""
145 return 'image'