Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 1 | # Copyright 2016 Red Hat, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from oslo_serialization import jsonutils as json |
| 16 | from six.moves.urllib import parse as urllib |
| 17 | |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 18 | from tempest.lib.common import rest_client |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 19 | |
| 20 | |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 21 | class DomainsClient(rest_client.RestClient): |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 22 | api_version = "v3" |
| 23 | |
| 24 | def create_domain(self, name, **kwargs): |
| 25 | """Creates a domain.""" |
| 26 | description = kwargs.get('description', None) |
| 27 | en = kwargs.get('enabled', True) |
| 28 | post_body = { |
| 29 | 'description': description, |
| 30 | 'enabled': en, |
| 31 | 'name': name |
| 32 | } |
| 33 | post_body = json.dumps({'domain': post_body}) |
| 34 | resp, body = self.post('domains', post_body) |
| 35 | self.expected_success(201, resp.status) |
| 36 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 37 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 38 | |
| 39 | def delete_domain(self, domain_id): |
| 40 | """Deletes a domain.""" |
| 41 | resp, body = self.delete('domains/%s' % str(domain_id)) |
| 42 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 43 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 44 | |
| 45 | def list_domains(self, params=None): |
| 46 | """List Domains.""" |
| 47 | url = 'domains' |
| 48 | if params: |
| 49 | url += '?%s' % urllib.urlencode(params) |
| 50 | resp, body = self.get(url) |
| 51 | self.expected_success(200, resp.status) |
| 52 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 53 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 54 | |
| 55 | def update_domain(self, domain_id, **kwargs): |
| 56 | """Updates a domain.""" |
| 57 | body = self.show_domain(domain_id)['domain'] |
| 58 | description = kwargs.get('description', body['description']) |
| 59 | en = kwargs.get('enabled', body['enabled']) |
| 60 | name = kwargs.get('name', body['name']) |
| 61 | post_body = { |
| 62 | 'description': description, |
| 63 | 'enabled': en, |
| 64 | 'name': name |
| 65 | } |
| 66 | post_body = json.dumps({'domain': post_body}) |
| 67 | resp, body = self.patch('domains/%s' % domain_id, post_body) |
| 68 | self.expected_success(200, resp.status) |
| 69 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 70 | return rest_client.ResponseBody(resp, body) |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 71 | |
| 72 | def show_domain(self, domain_id): |
| 73 | """Get Domain details.""" |
| 74 | resp, body = self.get('domains/%s' % domain_id) |
| 75 | self.expected_success(200, resp.status) |
| 76 | body = json.loads(body) |
Ken'ichi Ohmichi | d35a133 | 2016-03-02 10:38:07 -0800 | [diff] [blame] | 77 | return rest_client.ResponseBody(resp, body) |