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