Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 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 | |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 16 | import functools |
| 17 | |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 18 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 19 | from six.moves.urllib import parse as urllib |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 20 | |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 21 | from tempest.lib.common import rest_client |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 22 | from tempest.lib import exceptions as lib_exc |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 24 | CHUNKSIZE = 1024 * 64 # 64kB |
| 25 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 26 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 27 | class ImagesClient(rest_client.RestClient): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 28 | api_version = "v2" |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 29 | |
Sergey Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 30 | def update_image(self, image_id, patch): |
Ken'ichi Ohmichi | 25b3016 | 2015-11-30 11:27:13 +0000 | [diff] [blame] | 31 | """Update an image. |
| 32 | |
| 33 | Available params: see http://developer.openstack.org/ |
| 34 | api-ref-image-v2.html#updateImage-v2 |
| 35 | """ |
Sergey Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 36 | data = json.dumps(patch) |
Sergey Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 37 | headers = {"Content-Type": "application/openstack-images-v2.0" |
| 38 | "-json-patch"} |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 39 | resp, body = self.patch('images/%s' % image_id, data, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 40 | self.expected_success(200, resp.status) |
ghanshyam | 08a73f9 | 2015-08-31 17:32:49 +0900 | [diff] [blame] | 41 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 42 | return rest_client.ResponseBody(resp, body) |
Sergey Nikitin | c6b2ee8 | 2014-02-03 17:13:50 +0400 | [diff] [blame] | 43 | |
Ken'ichi Ohmichi | 25b3016 | 2015-11-30 11:27:13 +0000 | [diff] [blame] | 44 | def create_image(self, **kwargs): |
| 45 | """Create an image. |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 46 | |
Ken'ichi Ohmichi | 25b3016 | 2015-11-30 11:27:13 +0000 | [diff] [blame] | 47 | Available params: see http://developer.openstack.org/ |
| 48 | api-ref-image-v2.html#createImage-v2 |
| 49 | """ |
| 50 | data = json.dumps(kwargs) |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 51 | resp, body = self.post('images', data) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 52 | self.expected_success(201, resp.status) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 53 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 54 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 55 | |
bkopilov | 81aaae7 | 2015-05-15 23:46:25 +0300 | [diff] [blame] | 56 | def deactivate_image(self, image_id): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 57 | url = 'images/%s/actions/deactivate' % image_id |
bkopilov | 81aaae7 | 2015-05-15 23:46:25 +0300 | [diff] [blame] | 58 | resp, body = self.post(url, None) |
| 59 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 60 | return rest_client.ResponseBody(resp, body) |
bkopilov | 81aaae7 | 2015-05-15 23:46:25 +0300 | [diff] [blame] | 61 | |
| 62 | def reactivate_image(self, image_id): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 63 | url = 'images/%s/actions/reactivate' % image_id |
bkopilov | 81aaae7 | 2015-05-15 23:46:25 +0300 | [diff] [blame] | 64 | resp, body = self.post(url, None) |
| 65 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 66 | return rest_client.ResponseBody(resp, body) |
bkopilov | 81aaae7 | 2015-05-15 23:46:25 +0300 | [diff] [blame] | 67 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 68 | def delete_image(self, image_id): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 69 | url = 'images/%s' % image_id |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 70 | resp, _ = self.delete(url) |
| 71 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 72 | return rest_client.ResponseBody(resp) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 73 | |
Ken'ichi Ohmichi | e3acc12 | 2015-05-22 00:32:54 +0000 | [diff] [blame] | 74 | def list_images(self, params=None): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 75 | url = 'images' |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 76 | |
| 77 | if params: |
| 78 | url += '?%s' % urllib.urlencode(params) |
| 79 | |
| 80 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 81 | self.expected_success(200, resp.status) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 82 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 83 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 84 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 85 | def show_image(self, image_id): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 86 | url = 'images/%s' % image_id |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 87 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 88 | self.expected_success(200, resp.status) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 89 | body = json.loads(body) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 90 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 91 | |
| 92 | def is_resource_deleted(self, id): |
| 93 | try: |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 94 | self.show_image(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 95 | except lib_exc.NotFound: |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 96 | return True |
| 97 | return False |
| 98 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 99 | @property |
| 100 | def resource_type(self): |
| 101 | """Returns the primary type of resource this client works with.""" |
| 102 | return 'image' |
| 103 | |
Ken'ichi Ohmichi | 66494e9 | 2015-06-08 04:28:10 +0000 | [diff] [blame] | 104 | def store_image_file(self, image_id, data): |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 105 | url = 'images/%s/file' % image_id |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 106 | |
| 107 | # We are going to do chunked transfert, so split the input data |
| 108 | # info fixed-sized chunks. |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 109 | headers = {'Content-Type': 'application/octet-stream'} |
Jordan Pittier | 9a573d9 | 2016-04-29 17:04:39 +0200 | [diff] [blame] | 110 | data = iter(functools.partial(data.read, CHUNKSIZE), b'') |
| 111 | |
| 112 | resp, body = self.request('PUT', url, headers=headers, |
| 113 | body=data, chunked=True) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 114 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 115 | return rest_client.ResponseBody(resp, body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 116 | |
Ken'ichi Ohmichi | 6bd6f20 | 2015-11-20 05:29:38 +0000 | [diff] [blame] | 117 | def show_image_file(self, image_id): |
bkopilov | cb1c629 | 2016-06-30 09:20:34 +0300 | [diff] [blame] | 118 | """Show an image file. |
| 119 | |
| 120 | Available params: http://developer.openstack.org/ |
| 121 | api-ref-image-v2.html#showImageFile-v2 |
| 122 | """ |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 123 | url = 'images/%s/file' % image_id |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 124 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 125 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 126 | return rest_client.ResponseBodyData(resp, body) |
Anju Tiwari | c495298 | 2013-10-20 07:10:02 +0530 | [diff] [blame] | 127 | |
| 128 | def add_image_tag(self, image_id, tag): |
bkopilov | cb1c629 | 2016-06-30 09:20:34 +0300 | [diff] [blame] | 129 | """Add an image tag. |
| 130 | |
| 131 | Available params: http://developer.openstack.org/ |
| 132 | api-ref-image-v2.html#addImageTag-v2 |
| 133 | """ |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 134 | url = 'images/%s/tags/%s' % (image_id, tag) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 135 | resp, body = self.put(url, body=None) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 136 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 137 | return rest_client.ResponseBody(resp, body) |
Anju Tiwari | c495298 | 2013-10-20 07:10:02 +0530 | [diff] [blame] | 138 | |
| 139 | def delete_image_tag(self, image_id, tag): |
bkopilov | cb1c629 | 2016-06-30 09:20:34 +0300 | [diff] [blame] | 140 | """Delete an image tag. |
| 141 | |
| 142 | Available params: http://developer.openstack.org/ |
| 143 | api-ref-image-v2.html#deleteImageTag-v2 |
| 144 | """ |
Andrea Frittoli | cf40a86 | 2016-06-07 14:45:32 +0900 | [diff] [blame] | 145 | url = 'images/%s/tags/%s' % (image_id, tag) |
Anju Tiwari | c495298 | 2013-10-20 07:10:02 +0530 | [diff] [blame] | 146 | resp, _ = self.delete(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 147 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | e76510d | 2016-03-02 10:33:48 -0800 | [diff] [blame] | 148 | return rest_client.ResponseBody(resp) |