ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 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 | |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 16 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 17 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 18 | from tempest_lib import exceptions as lib_exc |
| 19 | |
ghanshyam | aa93b4b | 2015-03-20 11:03:44 +0900 | [diff] [blame] | 20 | from tempest.api_schema.response.compute.v2_1 import security_groups as schema |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 21 | from tempest.common import service_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 22 | |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 23 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 24 | class SecurityGroupsClient(service_client.ServiceClient): |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 25 | |
Ken'ichi Ohmichi | 118776d | 2015-07-01 08:15:00 +0000 | [diff] [blame] | 26 | def list_security_groups(self, **params): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 27 | """List all security groups for a user.""" |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 28 | |
| 29 | url = 'os-security-groups' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 30 | if params: |
| 31 | url += '?%s' % urllib.urlencode(params) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 32 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 33 | resp, body = self.get(url) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 34 | body = json.loads(body) |
Yuiko Takada | c3aa110 | 2014-03-19 15:19:19 +0000 | [diff] [blame] | 35 | self.validate_response(schema.list_security_groups, resp, body) |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 36 | return service_client.ResponseBodyList(resp, body['security_groups']) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 37 | |
Ken'ichi Ohmichi | 217f2f3 | 2015-06-17 02:52:44 +0000 | [diff] [blame] | 38 | def show_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 39 | """Get the details of a Security Group.""" |
Ken'ichi Ohmichi | 217f2f3 | 2015-06-17 02:52:44 +0000 | [diff] [blame] | 40 | url = "os-security-groups/%s" % security_group_id |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 41 | resp, body = self.get(url) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 42 | body = json.loads(body) |
Yuiko Takada | b34c161 | 2014-03-26 15:31:47 +0000 | [diff] [blame] | 43 | self.validate_response(schema.get_security_group, resp, body) |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 44 | return service_client.ResponseBody(resp, body['security_group']) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 45 | |
Ken'ichi Ohmichi | 34563cc | 2015-07-21 00:53:17 +0000 | [diff] [blame] | 46 | def create_security_group(self, **kwargs): |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 47 | """ |
| 48 | Creates a new security group. |
| 49 | name (Required): Name of security group. |
| 50 | description (Required): Description of security group. |
| 51 | """ |
Ken'ichi Ohmichi | 34563cc | 2015-07-21 00:53:17 +0000 | [diff] [blame] | 52 | post_body = json.dumps({'security_group': kwargs}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 53 | resp, body = self.post('os-security-groups', post_body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 54 | body = json.loads(body) |
Yuiko Takada | b34c161 | 2014-03-26 15:31:47 +0000 | [diff] [blame] | 55 | self.validate_response(schema.get_security_group, resp, body) |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 56 | return service_client.ResponseBody(resp, body['security_group']) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 57 | |
Ken'ichi Ohmichi | 34563cc | 2015-07-21 00:53:17 +0000 | [diff] [blame] | 58 | def update_security_group(self, security_group_id, **kwargs): |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 59 | """ |
| 60 | Update a security group. |
| 61 | security_group_id: a security_group to update |
| 62 | name: new name of security group |
| 63 | description: new description of security group |
| 64 | """ |
Ken'ichi Ohmichi | 34563cc | 2015-07-21 00:53:17 +0000 | [diff] [blame] | 65 | post_body = json.dumps({'security_group': kwargs}) |
Ken'ichi Ohmichi | 217f2f3 | 2015-06-17 02:52:44 +0000 | [diff] [blame] | 66 | resp, body = self.put('os-security-groups/%s' % security_group_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 67 | post_body) |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 68 | body = json.loads(body) |
Yuiko Takada | b34c161 | 2014-03-26 15:31:47 +0000 | [diff] [blame] | 69 | self.validate_response(schema.update_security_group, resp, body) |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 70 | return service_client.ResponseBody(resp, body['security_group']) |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 71 | |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 72 | def delete_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 73 | """Deletes the provided Security Group.""" |
Yuiko Takada | 80cc9f1 | 2014-04-04 16:21:21 +0000 | [diff] [blame] | 74 | resp, body = self.delete( |
Ken'ichi Ohmichi | 217f2f3 | 2015-06-17 02:52:44 +0000 | [diff] [blame] | 75 | 'os-security-groups/%s' % security_group_id) |
Yuiko Takada | 80cc9f1 | 2014-04-04 16:21:21 +0000 | [diff] [blame] | 76 | self.validate_response(schema.delete_security_group, resp, body) |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 77 | return service_client.ResponseBody(resp, body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 78 | |
Zhi Kun Liu | 02e7a7b | 2014-01-08 16:08:32 +0800 | [diff] [blame] | 79 | def is_resource_deleted(self, id): |
| 80 | try: |
Ken'ichi Ohmichi | 217f2f3 | 2015-06-17 02:52:44 +0000 | [diff] [blame] | 81 | self.show_security_group(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 82 | except lib_exc.NotFound: |
Zhi Kun Liu | 02e7a7b | 2014-01-08 16:08:32 +0800 | [diff] [blame] | 83 | return True |
| 84 | return False |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 85 | |
| 86 | @property |
| 87 | def resource_type(self): |
| 88 | """Returns the primary type of resource this client works with.""" |
| 89 | return 'security_group' |