blob: 427684746bf79f756721016a64e3ba03c86de5d1 [file] [log] [blame]
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04001# Copyright 2013 IBM Corp.
Matthew Treinisha62347f2013-03-01 16:37:30 -05002# 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 Pittier9a573d92016-04-29 17:04:39 +020016import functools
17
Matthew Treinish21905512015-07-13 10:33:35 -040018from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040019from six.moves.urllib import parse as urllib
Matthew Treinisha62347f2013-03-01 16:37:30 -050020
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -080021from tempest.lib.common import rest_client
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050022from tempest.lib import exceptions as lib_exc
Matthew Treinish684d8992014-01-30 16:27:40 +000023
Jordan Pittier9a573d92016-04-29 17:04:39 +020024CHUNKSIZE = 1024 * 64 # 64kB
25
Matthew Treinisha62347f2013-03-01 16:37:30 -050026
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +030027class ImagesClient(rest_client.RestClient):
Andrea Frittolicf40a862016-06-07 14:45:32 +090028 api_version = "v2"
Matthew Treinisha62347f2013-03-01 16:37:30 -050029
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040030 def update_image(self, image_id, patch):
Ken'ichi Ohmichi25b30162015-11-30 11:27:13 +000031 """Update an image.
32
33 Available params: see http://developer.openstack.org/
34 api-ref-image-v2.html#updateImage-v2
35 """
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040036 data = json.dumps(patch)
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040037 headers = {"Content-Type": "application/openstack-images-v2.0"
38 "-json-patch"}
Andrea Frittolicf40a862016-06-07 14:45:32 +090039 resp, body = self.patch('images/%s' % image_id, data, headers)
David Kranz9c3b3b62014-06-19 16:05:53 -040040 self.expected_success(200, resp.status)
ghanshyam08a73f92015-08-31 17:32:49 +090041 body = json.loads(body)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -080042 return rest_client.ResponseBody(resp, body)
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040043
Ken'ichi Ohmichi25b30162015-11-30 11:27:13 +000044 def create_image(self, **kwargs):
45 """Create an image.
Matthew Treinishce3ef922013-03-11 14:02:46 -040046
Ken'ichi Ohmichi25b30162015-11-30 11:27:13 +000047 Available params: see http://developer.openstack.org/
48 api-ref-image-v2.html#createImage-v2
49 """
50 data = json.dumps(kwargs)
Andrea Frittolicf40a862016-06-07 14:45:32 +090051 resp, body = self.post('images', data)
David Kranz9c3b3b62014-06-19 16:05:53 -040052 self.expected_success(201, resp.status)
Matthew Treinisha62347f2013-03-01 16:37:30 -050053 body = json.loads(body)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -080054 return rest_client.ResponseBody(resp, body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050055
bkopilov81aaae72015-05-15 23:46:25 +030056 def deactivate_image(self, image_id):
Andrea Frittolicf40a862016-06-07 14:45:32 +090057 url = 'images/%s/actions/deactivate' % image_id
bkopilov81aaae72015-05-15 23:46:25 +030058 resp, body = self.post(url, None)
59 self.expected_success(204, resp.status)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -080060 return rest_client.ResponseBody(resp, body)
bkopilov81aaae72015-05-15 23:46:25 +030061
62 def reactivate_image(self, image_id):
Andrea Frittolicf40a862016-06-07 14:45:32 +090063 url = 'images/%s/actions/reactivate' % image_id
bkopilov81aaae72015-05-15 23:46:25 +030064 resp, body = self.post(url, None)
65 self.expected_success(204, resp.status)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -080066 return rest_client.ResponseBody(resp, body)
bkopilov81aaae72015-05-15 23:46:25 +030067
Matthew Treinisha62347f2013-03-01 16:37:30 -050068 def delete_image(self, image_id):
Andrea Frittolicf40a862016-06-07 14:45:32 +090069 url = 'images/%s' % image_id
David Kranz9c3b3b62014-06-19 16:05:53 -040070 resp, _ = self.delete(url)
71 self.expected_success(204, resp.status)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -080072 return rest_client.ResponseBody(resp)
Matthew Treinisha62347f2013-03-01 16:37:30 -050073
Ken'ichi Ohmichie3acc122015-05-22 00:32:54 +000074 def list_images(self, params=None):
Andrea Frittolicf40a862016-06-07 14:45:32 +090075 url = 'images'
Matthew Treinisha62347f2013-03-01 16:37:30 -050076
77 if params:
78 url += '?%s' % urllib.urlencode(params)
79
80 resp, body = self.get(url)
David Kranz9c3b3b62014-06-19 16:05:53 -040081 self.expected_success(200, resp.status)
Matthew Treinisha62347f2013-03-01 16:37:30 -050082 body = json.loads(body)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -080083 return rest_client.ResponseBody(resp, body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050084
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000085 def show_image(self, image_id):
Andrea Frittolicf40a862016-06-07 14:45:32 +090086 url = 'images/%s' % image_id
Matthew Treinisha62347f2013-03-01 16:37:30 -050087 resp, body = self.get(url)
David Kranz9c3b3b62014-06-19 16:05:53 -040088 self.expected_success(200, resp.status)
Matthew Treinisha62347f2013-03-01 16:37:30 -050089 body = json.loads(body)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -080090 return rest_client.ResponseBody(resp, body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050091
92 def is_resource_deleted(self, id):
93 try:
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000094 self.show_image(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090095 except lib_exc.NotFound:
Matthew Treinisha62347f2013-03-01 16:37:30 -050096 return True
97 return False
98
Matt Riedemannd2b96512014-10-13 10:18:16 -070099 @property
100 def resource_type(self):
101 """Returns the primary type of resource this client works with."""
102 return 'image'
103
Ken'ichi Ohmichi66494e92015-06-08 04:28:10 +0000104 def store_image_file(self, image_id, data):
Andrea Frittolicf40a862016-06-07 14:45:32 +0900105 url = 'images/%s/file' % image_id
Jordan Pittier9a573d92016-04-29 17:04:39 +0200106
107 # We are going to do chunked transfert, so split the input data
108 # info fixed-sized chunks.
Matthew Treinisha62347f2013-03-01 16:37:30 -0500109 headers = {'Content-Type': 'application/octet-stream'}
Jordan Pittier9a573d92016-04-29 17:04:39 +0200110 data = iter(functools.partial(data.read, CHUNKSIZE), b'')
111
112 resp, body = self.request('PUT', url, headers=headers,
113 body=data, chunked=True)
David Kranz9c3b3b62014-06-19 16:05:53 -0400114 self.expected_success(204, resp.status)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -0800115 return rest_client.ResponseBody(resp, body)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500116
Ken'ichi Ohmichi6bd6f202015-11-20 05:29:38 +0000117 def show_image_file(self, image_id):
bkopilovcb1c6292016-06-30 09:20:34 +0300118 """Show an image file.
119
120 Available params: http://developer.openstack.org/
121 api-ref-image-v2.html#showImageFile-v2
122 """
Andrea Frittolicf40a862016-06-07 14:45:32 +0900123 url = 'images/%s/file' % image_id
Matthew Treinisha62347f2013-03-01 16:37:30 -0500124 resp, body = self.get(url)
David Kranz9c3b3b62014-06-19 16:05:53 -0400125 self.expected_success(200, resp.status)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -0800126 return rest_client.ResponseBodyData(resp, body)
Anju Tiwaric4952982013-10-20 07:10:02 +0530127
128 def add_image_tag(self, image_id, tag):
bkopilovcb1c6292016-06-30 09:20:34 +0300129 """Add an image tag.
130
131 Available params: http://developer.openstack.org/
132 api-ref-image-v2.html#addImageTag-v2
133 """
Andrea Frittolicf40a862016-06-07 14:45:32 +0900134 url = 'images/%s/tags/%s' % (image_id, tag)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200135 resp, body = self.put(url, body=None)
David Kranz9c3b3b62014-06-19 16:05:53 -0400136 self.expected_success(204, resp.status)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -0800137 return rest_client.ResponseBody(resp, body)
Anju Tiwaric4952982013-10-20 07:10:02 +0530138
139 def delete_image_tag(self, image_id, tag):
bkopilovcb1c6292016-06-30 09:20:34 +0300140 """Delete an image tag.
141
142 Available params: http://developer.openstack.org/
143 api-ref-image-v2.html#deleteImageTag-v2
144 """
Andrea Frittolicf40a862016-06-07 14:45:32 +0900145 url = 'images/%s/tags/%s' % (image_id, tag)
Anju Tiwaric4952982013-10-20 07:10:02 +0530146 resp, _ = self.delete(url)
David Kranz9c3b3b62014-06-19 16:05:53 -0400147 self.expected_success(204, resp.status)
Ken'ichi Ohmichie76510d2016-03-02 10:33:48 -0800148 return rest_client.ResponseBody(resp)