dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 18 | import json |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 19 | import urllib |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 20 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | from tempest.common.rest_client import RestClient |
| 22 | |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 23 | |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 24 | class SecurityGroupsClientJSON(RestClient): |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 25 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 26 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 27 | super(SecurityGroupsClientJSON, self).__init__(config, username, |
| 28 | password, auth_url, |
| 29 | tenant_name) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 30 | self.service = self.config.compute.catalog_type |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 31 | |
| 32 | def list_security_groups(self, params=None): |
| 33 | """List all security groups for a user""" |
| 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): |
| 44 | """Get the details of a Security Group""" |
| 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 | |
| 65 | def delete_security_group(self, security_group_id): |
| 66 | """Deletes the provided Security Group""" |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 67 | return self.delete('os-security-groups/%s' % str(security_group_id)) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 68 | |
| 69 | 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] | 70 | to_port, **kwargs): |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 71 | """ |
| 72 | Creating a new security group rules. |
| 73 | parent_group_id :ID of Security group |
| 74 | ip_protocol : ip_proto (icmp, tcp, udp). |
| 75 | from_port: Port at start of range. |
| 76 | to_port : Port at end of range. |
| 77 | Following optional keyword arguments are accepted: |
| 78 | cidr : CIDR for address range. |
| 79 | group_id : ID of the Source group |
| 80 | """ |
| 81 | post_body = { |
| 82 | 'parent_group_id': parent_group_id, |
| 83 | 'ip_protocol': ip_proto, |
| 84 | 'from_port': from_port, |
| 85 | 'to_port': to_port, |
| 86 | 'cidr': kwargs.get('cidr'), |
| 87 | 'group_id': kwargs.get('group_id'), |
| 88 | } |
| 89 | post_body = json.dumps({'security_group_rule': post_body}) |
| 90 | url = 'os-security-group-rules' |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 91 | resp, body = self.post(url, post_body, self.headers) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 92 | body = json.loads(body) |
| 93 | return resp, body['security_group_rule'] |
| 94 | |
| 95 | def delete_security_group_rule(self, group_rule_id): |
| 96 | """Deletes the provided Security Group rule""" |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 97 | return self.delete('os-security-group-rules/%s' % str(group_rule_id)) |