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