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 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 19 | from tempest.common.rest_client import RestClient |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
Attila Fazekas | aea5630 | 2013-03-26 23:06:44 +0100 | [diff] [blame] | 21 | from tempest import exceptions |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 22 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 25 | |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 26 | class SecurityGroupsClientJSON(RestClient): |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 27 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 28 | def __init__(self, auth_provider): |
| 29 | super(SecurityGroupsClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 30 | self.service = CONF.compute.catalog_type |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 31 | |
| 32 | def list_security_groups(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 33 | """List all security groups for a user.""" |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 34 | |
| 35 | url = 'os-security-groups' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 36 | if params: |
| 37 | url += '?%s' % urllib.urlencode(params) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 38 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 39 | resp, body = self.get(url) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 40 | body = json.loads(body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 41 | return resp, body['security_groups'] |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 42 | |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 43 | def get_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 44 | """Get the details of a Security Group.""" |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 45 | url = "os-security-groups/%s" % str(security_group_id) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 46 | resp, body = self.get(url) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 47 | body = json.loads(body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 48 | return resp, body['security_group'] |
| 49 | |
| 50 | def create_security_group(self, name, description): |
| 51 | """ |
| 52 | Creates a new security group. |
| 53 | name (Required): Name of security group. |
| 54 | description (Required): Description of security group. |
| 55 | """ |
| 56 | post_body = { |
| 57 | 'name': name, |
| 58 | 'description': description, |
| 59 | } |
| 60 | post_body = json.dumps({'security_group': post_body}) |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 61 | resp, body = self.post('os-security-groups', post_body, self.headers) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 62 | body = json.loads(body) |
| 63 | return resp, body['security_group'] |
| 64 | |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 65 | def update_security_group(self, security_group_id, name=None, |
| 66 | description=None): |
| 67 | """ |
| 68 | Update a security group. |
| 69 | security_group_id: a security_group to update |
| 70 | name: new name of security group |
| 71 | description: new description of security group |
| 72 | """ |
| 73 | post_body = {} |
| 74 | if name: |
| 75 | post_body['name'] = name |
| 76 | if description: |
| 77 | post_body['description'] = description |
| 78 | post_body = json.dumps({'security_group': post_body}) |
| 79 | resp, body = self.put('os-security-groups/%s' % str(security_group_id), |
| 80 | post_body, self.headers) |
| 81 | body = json.loads(body) |
| 82 | return resp, body['security_group'] |
| 83 | |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 84 | def delete_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 85 | """Deletes the provided Security Group.""" |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 86 | return self.delete('os-security-groups/%s' % str(security_group_id)) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 87 | |
| 88 | 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] | 89 | to_port, **kwargs): |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 90 | """ |
| 91 | Creating a new security group rules. |
| 92 | parent_group_id :ID of Security group |
| 93 | ip_protocol : ip_proto (icmp, tcp, udp). |
| 94 | from_port: Port at start of range. |
| 95 | to_port : Port at end of range. |
| 96 | Following optional keyword arguments are accepted: |
| 97 | cidr : CIDR for address range. |
| 98 | group_id : ID of the Source group |
| 99 | """ |
| 100 | post_body = { |
| 101 | 'parent_group_id': parent_group_id, |
| 102 | 'ip_protocol': ip_proto, |
| 103 | 'from_port': from_port, |
| 104 | 'to_port': to_port, |
| 105 | 'cidr': kwargs.get('cidr'), |
| 106 | 'group_id': kwargs.get('group_id'), |
| 107 | } |
| 108 | post_body = json.dumps({'security_group_rule': post_body}) |
| 109 | url = 'os-security-group-rules' |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 110 | resp, body = self.post(url, post_body, self.headers) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 111 | body = json.loads(body) |
| 112 | return resp, body['security_group_rule'] |
| 113 | |
| 114 | def delete_security_group_rule(self, group_rule_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 115 | """Deletes the provided Security Group rule.""" |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 116 | return self.delete('os-security-group-rules/%s' % str(group_rule_id)) |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 117 | |
| 118 | def list_security_group_rules(self, security_group_id): |
| 119 | """List all rules for a security group.""" |
| 120 | resp, body = self.get('os-security-groups') |
| 121 | body = json.loads(body) |
| 122 | for sg in body['security_groups']: |
| 123 | if sg['id'] == security_group_id: |
| 124 | return resp, sg['rules'] |
| 125 | raise exceptions.NotFound('No such Security Group') |