blob: 43226be6385fff4b186cbe4fcde63874b8bb85f7 [file] [log] [blame]
ravikumar-venkatesanacf99d72014-07-09 14:58:25 +00001# Copyright 2014 Hewlett-Packard Development Company, L.P
ravikumar-venkatesan3052e942014-05-12 18:25:17 +00002# 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
Matthew Treinish21905512015-07-13 10:33:35 -040016from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040017from six.moves.urllib import parse as urllib
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000018
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000019from tempest.common import service_client
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000020
21
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000022class RegionClient(service_client.ServiceClient):
ghanshyamd26b5cd2015-02-09 14:48:58 +090023 api_version = "v3"
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000024
25 def create_region(self, description, **kwargs):
26 """Create region."""
27 req_body = {
28 'description': description,
29 }
30 if kwargs.get('parent_region_id'):
31 req_body['parent_region_id'] = kwargs.get('parent_region_id')
32 req_body = json.dumps({'region': req_body})
33 if kwargs.get('unique_region_id'):
34 resp, body = self.put(
35 'regions/%s' % kwargs.get('unique_region_id'), req_body)
36 else:
37 resp, body = self.post('regions', req_body)
David Kranz2aaf5312014-08-29 09:22:10 -040038 self.expected_success(201, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000039 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000040 return service_client.ResponseBody(resp, body['region'])
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000041
42 def update_region(self, region_id, **kwargs):
43 """Updates a region."""
44 post_body = {}
45 if 'description' in kwargs:
46 post_body['description'] = kwargs.get('description')
47 if 'parent_region_id' in kwargs:
48 post_body['parent_region_id'] = kwargs.get('parent_region_id')
49 post_body = json.dumps({'region': post_body})
50 resp, body = self.patch('regions/%s' % region_id, post_body)
David Kranz2aaf5312014-08-29 09:22:10 -040051 self.expected_success(200, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000052 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000053 return service_client.ResponseBody(resp, body['region'])
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000054
55 def get_region(self, region_id):
56 """Get region."""
57 url = 'regions/%s' % region_id
58 resp, body = self.get(url)
David Kranz2aaf5312014-08-29 09:22:10 -040059 self.expected_success(200, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000060 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000061 return service_client.ResponseBody(resp, body['region'])
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000062
63 def list_regions(self, params=None):
64 """List regions."""
65 url = 'regions'
66 if params:
67 url += '?%s' % urllib.urlencode(params)
68 resp, body = self.get(url)
David Kranz2aaf5312014-08-29 09:22:10 -040069 self.expected_success(200, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000070 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000071 return service_client.ResponseBodyList(resp, body['regions'])
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000072
73 def delete_region(self, region_id):
74 """Delete region."""
75 resp, body = self.delete('regions/%s' % region_id)
David Kranz2aaf5312014-08-29 09:22:10 -040076 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000077 return service_client.ResponseBody(resp, body)