ravikumar-venkatesan | 3052e94 | 2014-05-12 18:25:17 +0000 | [diff] [blame^] | 1 | # Copyright 2014 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 | import json |
| 17 | import urllib |
| 18 | |
| 19 | from tempest.common import rest_client |
| 20 | from tempest import config |
| 21 | |
| 22 | CONF = config.CONF |
| 23 | |
| 24 | |
| 25 | class RegionClientJSON(rest_client.RestClient): |
| 26 | |
| 27 | def __init__(self, auth_provider): |
| 28 | super(RegionClientJSON, self).__init__(auth_provider) |
| 29 | self.service = CONF.identity.catalog_type |
| 30 | self.endpoint_url = 'adminURL' |
| 31 | self.api_version = "v3" |
| 32 | |
| 33 | def create_region(self, description, **kwargs): |
| 34 | """Create region.""" |
| 35 | req_body = { |
| 36 | 'description': description, |
| 37 | } |
| 38 | if kwargs.get('parent_region_id'): |
| 39 | req_body['parent_region_id'] = kwargs.get('parent_region_id') |
| 40 | req_body = json.dumps({'region': req_body}) |
| 41 | if kwargs.get('unique_region_id'): |
| 42 | resp, body = self.put( |
| 43 | 'regions/%s' % kwargs.get('unique_region_id'), req_body) |
| 44 | else: |
| 45 | resp, body = self.post('regions', req_body) |
| 46 | body = json.loads(body) |
| 47 | return resp, body['region'] |
| 48 | |
| 49 | def update_region(self, region_id, **kwargs): |
| 50 | """Updates a region.""" |
| 51 | post_body = {} |
| 52 | if 'description' in kwargs: |
| 53 | post_body['description'] = kwargs.get('description') |
| 54 | if 'parent_region_id' in kwargs: |
| 55 | post_body['parent_region_id'] = kwargs.get('parent_region_id') |
| 56 | post_body = json.dumps({'region': post_body}) |
| 57 | resp, body = self.patch('regions/%s' % region_id, post_body) |
| 58 | body = json.loads(body) |
| 59 | return resp, body['region'] |
| 60 | |
| 61 | def get_region(self, region_id): |
| 62 | """Get region.""" |
| 63 | url = 'regions/%s' % region_id |
| 64 | resp, body = self.get(url) |
| 65 | body = json.loads(body) |
| 66 | return resp, body['region'] |
| 67 | |
| 68 | def list_regions(self, params=None): |
| 69 | """List regions.""" |
| 70 | url = 'regions' |
| 71 | if params: |
| 72 | url += '?%s' % urllib.urlencode(params) |
| 73 | resp, body = self.get(url) |
| 74 | body = json.loads(body) |
| 75 | return resp, body['regions'] |
| 76 | |
| 77 | def delete_region(self, region_id): |
| 78 | """Delete region.""" |
| 79 | resp, body = self.delete('regions/%s' % region_id) |
| 80 | return resp, body |