blob: 02df3548c95304e58e891225b9341932ae86e091 [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
16import json
Matthew Treinish89128142015-04-23 10:44:30 -040017
18from six.moves.urllib import parse as urllib
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000019
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000020from tempest.common import service_client
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000021
22
ghanshyamd26b5cd2015-02-09 14:48:58 +090023class RegionClientJSON(service_client.ServiceClient):
24 api_version = "v3"
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000025
26 def create_region(self, description, **kwargs):
27 """Create region."""
28 req_body = {
29 'description': description,
30 }
31 if kwargs.get('parent_region_id'):
32 req_body['parent_region_id'] = kwargs.get('parent_region_id')
33 req_body = json.dumps({'region': req_body})
34 if kwargs.get('unique_region_id'):
35 resp, body = self.put(
36 'regions/%s' % kwargs.get('unique_region_id'), req_body)
37 else:
38 resp, body = self.post('regions', req_body)
David Kranz2aaf5312014-08-29 09:22:10 -040039 self.expected_success(201, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000040 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000041 return service_client.ResponseBody(resp, body['region'])
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000042
43 def update_region(self, region_id, **kwargs):
44 """Updates a region."""
45 post_body = {}
46 if 'description' in kwargs:
47 post_body['description'] = kwargs.get('description')
48 if 'parent_region_id' in kwargs:
49 post_body['parent_region_id'] = kwargs.get('parent_region_id')
50 post_body = json.dumps({'region': post_body})
51 resp, body = self.patch('regions/%s' % region_id, post_body)
David Kranz2aaf5312014-08-29 09:22:10 -040052 self.expected_success(200, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000053 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000054 return service_client.ResponseBody(resp, body['region'])
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000055
56 def get_region(self, region_id):
57 """Get region."""
58 url = 'regions/%s' % region_id
59 resp, body = self.get(url)
David Kranz2aaf5312014-08-29 09:22:10 -040060 self.expected_success(200, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000061 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000062 return service_client.ResponseBody(resp, body['region'])
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000063
64 def list_regions(self, params=None):
65 """List regions."""
66 url = 'regions'
67 if params:
68 url += '?%s' % urllib.urlencode(params)
69 resp, body = self.get(url)
David Kranz2aaf5312014-08-29 09:22:10 -040070 self.expected_success(200, resp.status)
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000071 body = json.loads(body)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000072 return service_client.ResponseBodyList(resp, body['regions'])
ravikumar-venkatesan3052e942014-05-12 18:25:17 +000073
74 def delete_region(self, region_id):
75 """Delete region."""
76 resp, body = self.delete('regions/%s' % region_id)
David Kranz2aaf5312014-08-29 09:22:10 -040077 self.expected_success(204, resp.status)
Ken'ichi Ohmichia6ac2422015-01-13 01:09:39 +000078 return service_client.ResponseBody(resp, body)