Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # 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 | |
| 16 | from oslo_serialization import jsonutils as json |
| 17 | from six.moves.urllib import parse as urllib |
| 18 | |
| 19 | from tempest.lib.api_schema.response.compute.v2_1 import images as schema |
Matt Riedemann | f110a4b | 2018-01-08 15:03:36 -0500 | [diff] [blame] | 20 | from tempest.lib.api_schema.response.compute.v2_45 import images as schemav245 |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 21 | from tempest.lib.common import rest_client |
| 22 | from tempest.lib import exceptions as lib_exc |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 23 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 24 | |
| 25 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 26 | class ImagesClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 27 | |
Matt Riedemann | f110a4b | 2018-01-08 15:03:36 -0500 | [diff] [blame] | 28 | schema_versions_info = [ |
| 29 | {'min': None, 'max': '2.44', 'schema': schema}, |
| 30 | {'min': '2.45', 'max': None, 'schema': schemav245}] |
| 31 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 32 | def create_image(self, server_id, **kwargs): |
| 33 | """Create an image of the original server. |
| 34 | |
OTSUKA, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 35 | For a full list of available parameters, please refer to the official |
| 36 | API reference: |
zhufl | 126a66d | 2017-03-08 15:32:21 +0800 | [diff] [blame] | 37 | https://developer.openstack.org/api-ref/compute/#create-image-createimage-action |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 38 | """ |
| 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 Riedemann | f110a4b | 2018-01-08 15:03:36 -0500 | [diff] [blame] | 44 | _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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 48 | 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 53 | For a full list of available parameters, please refer to the official |
| 54 | API reference: |
zhufl | 126a66d | 2017-03-08 15:32:21 +0800 | [diff] [blame] | 55 | https://developer.openstack.org/api-ref/compute/#list-images |
| 56 | https://developer.openstack.org/api-ref/compute/#list-images-with-details |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 57 | """ |
| 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 75 | 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 95 | For a full list of available parameters, please refer to the official |
| 96 | API reference: |
zhufl | 126a66d | 2017-03-08 15:32:21 +0800 | [diff] [blame] | 97 | https://developer.openstack.org/api-ref/compute/#update-image-metadata |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 98 | """ |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 108 | For a full list of available parameters, please refer to the official |
| 109 | API reference: |
zhufl | 126a66d | 2017-03-08 15:32:21 +0800 | [diff] [blame] | 110 | https://developer.openstack.org/api-ref/compute/#create-image-metadata |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 111 | """ |
| 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, Yuanying | faac571 | 2016-09-15 13:53:55 +0900 | [diff] [blame] | 128 | For a full list of available parameters, please refer to the official |
| 129 | API reference: |
zhufl | 126a66d | 2017-03-08 15:32:21 +0800 | [diff] [blame] | 130 | https://developer.openstack.org/api-ref/compute/#create-or-update-image-metadata-item |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 131 | """ |
| 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 White | 95dcef2 | 2016-04-11 06:17:42 -0700 | [diff] [blame] | 147 | # Added status check for user with admin role |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 148 | try: |
Joshua White | 95dcef2 | 2016-04-11 06:17:42 -0700 | [diff] [blame] | 149 | if self.show_image(id)['image']['status'] == 'DELETED': |
| 150 | return True |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 151 | 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' |