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 | |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 16 | import json |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 17 | import urllib |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 18 | |
Marc Koderer | 6fbd74f | 2014-08-04 09:38:19 +0200 | [diff] [blame] | 19 | from tempest.api_schema.response.compute.v2 import security_groups as schema |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame^] | 20 | from tempest.common import service_client |
Attila Fazekas | aea5630 | 2013-03-26 23:06:44 +0100 | [diff] [blame] | 21 | from tempest import exceptions |
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 | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame^] | 24 | class SecurityGroupsClientJSON(service_client.ServiceClient): |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 25 | |
| 26 | def list_security_groups(self, params=None): |
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) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 36 | return resp, body['security_groups'] |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 37 | |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 38 | def get_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.""" |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 40 | url = "os-security-groups/%s" % str(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) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 44 | return resp, body['security_group'] |
| 45 | |
| 46 | def create_security_group(self, name, description): |
| 47 | """ |
| 48 | Creates a new security group. |
| 49 | name (Required): Name of security group. |
| 50 | description (Required): Description of security group. |
| 51 | """ |
| 52 | post_body = { |
| 53 | 'name': name, |
| 54 | 'description': description, |
| 55 | } |
| 56 | post_body = json.dumps({'security_group': post_body}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 57 | resp, body = self.post('os-security-groups', post_body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 58 | body = json.loads(body) |
Yuiko Takada | b34c161 | 2014-03-26 15:31:47 +0000 | [diff] [blame] | 59 | self.validate_response(schema.get_security_group, resp, body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 60 | return resp, body['security_group'] |
| 61 | |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 62 | def update_security_group(self, security_group_id, name=None, |
| 63 | description=None): |
| 64 | """ |
| 65 | Update a security group. |
| 66 | security_group_id: a security_group to update |
| 67 | name: new name of security group |
| 68 | description: new description of security group |
| 69 | """ |
| 70 | post_body = {} |
| 71 | if name: |
| 72 | post_body['name'] = name |
| 73 | if description: |
| 74 | post_body['description'] = description |
| 75 | post_body = json.dumps({'security_group': post_body}) |
| 76 | resp, body = self.put('os-security-groups/%s' % str(security_group_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 77 | post_body) |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 78 | body = json.loads(body) |
Yuiko Takada | b34c161 | 2014-03-26 15:31:47 +0000 | [diff] [blame] | 79 | self.validate_response(schema.update_security_group, resp, body) |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 80 | return resp, body['security_group'] |
| 81 | |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 82 | def delete_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 83 | """Deletes the provided Security Group.""" |
Yuiko Takada | 80cc9f1 | 2014-04-04 16:21:21 +0000 | [diff] [blame] | 84 | resp, body = self.delete( |
| 85 | 'os-security-groups/%s' % str(security_group_id)) |
| 86 | self.validate_response(schema.delete_security_group, resp, body) |
| 87 | return resp, body |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 88 | |
| 89 | def create_security_group_rule(self, parent_group_id, ip_proto, from_port, |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 90 | to_port, **kwargs): |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 91 | """ |
| 92 | Creating a new security group rules. |
| 93 | parent_group_id :ID of Security group |
| 94 | ip_protocol : ip_proto (icmp, tcp, udp). |
| 95 | from_port: Port at start of range. |
| 96 | to_port : Port at end of range. |
| 97 | Following optional keyword arguments are accepted: |
| 98 | cidr : CIDR for address range. |
| 99 | group_id : ID of the Source group |
| 100 | """ |
| 101 | post_body = { |
| 102 | 'parent_group_id': parent_group_id, |
| 103 | 'ip_protocol': ip_proto, |
| 104 | 'from_port': from_port, |
| 105 | 'to_port': to_port, |
| 106 | 'cidr': kwargs.get('cidr'), |
| 107 | 'group_id': kwargs.get('group_id'), |
| 108 | } |
| 109 | post_body = json.dumps({'security_group_rule': post_body}) |
| 110 | url = 'os-security-group-rules' |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 111 | resp, body = self.post(url, post_body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 112 | body = json.loads(body) |
Ghanshyam | f79c799 | 2014-04-01 12:20:12 +0900 | [diff] [blame] | 113 | self.validate_response(schema.create_security_group_rule, resp, body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 114 | return resp, body['security_group_rule'] |
| 115 | |
| 116 | def delete_security_group_rule(self, group_rule_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 117 | """Deletes the provided Security Group rule.""" |
Ghanshyam | f79c799 | 2014-04-01 12:20:12 +0900 | [diff] [blame] | 118 | resp, body = self.delete('os-security-group-rules/%s' % |
| 119 | str(group_rule_id)) |
| 120 | self.validate_response(schema.delete_security_group_rule, resp, body) |
| 121 | return resp, body |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 122 | |
| 123 | def list_security_group_rules(self, security_group_id): |
| 124 | """List all rules for a security group.""" |
| 125 | resp, body = self.get('os-security-groups') |
| 126 | body = json.loads(body) |
Yuiko Takada | c3aa110 | 2014-03-19 15:19:19 +0000 | [diff] [blame] | 127 | self.validate_response(schema.list_security_groups, resp, body) |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 128 | for sg in body['security_groups']: |
| 129 | if sg['id'] == security_group_id: |
| 130 | return resp, sg['rules'] |
| 131 | raise exceptions.NotFound('No such Security Group') |
Zhi Kun Liu | 02e7a7b | 2014-01-08 16:08:32 +0800 | [diff] [blame] | 132 | |
| 133 | def is_resource_deleted(self, id): |
| 134 | try: |
| 135 | self.get_security_group(id) |
| 136 | except exceptions.NotFound: |
| 137 | return True |
| 138 | return False |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 139 | |
| 140 | @property |
| 141 | def resource_type(self): |
| 142 | """Returns the primary type of resource this client works with.""" |
| 143 | return 'security_group' |