blob: f2586e58d931b0cc65d2b9a57f74b18544bb2367 [file] [log] [blame]
dwallecke62b9f02012-10-10 23:34:42 -05001# 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 Venkatesaneeb9caa2012-01-12 16:42:36 -080018import json
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050019import urllib
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080020
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021from tempest.common.rest_client import RestClient
22
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080023
Vincent Houead03dc2012-08-24 21:35:11 +080024class SecurityGroupsClientJSON(RestClient):
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080025
Daryl Walleck587385b2012-03-03 13:00:26 -060026 def __init__(self, config, username, password, auth_url, tenant_name=None):
Vincent Houead03dc2012-08-24 21:35:11 +080027 super(SecurityGroupsClientJSON, self).__init__(config, username,
28 password, auth_url,
29 tenant_name)
chris fattarsi5098fa22012-04-17 13:27:00 -070030 self.service = self.config.compute.catalog_type
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080031
32 def list_security_groups(self, params=None):
33 """List all security groups for a user"""
34
35 url = 'os-security-groups'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050036 if params:
37 url += '?%s' % urllib.urlencode(params)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080038
chris fattarsi5098fa22012-04-17 13:27:00 -070039 resp, body = self.get(url)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080040 body = json.loads(body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053041 return resp, body['security_groups']
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080042
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053043 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 fattarsi5098fa22012-04-17 13:27:00 -070046 resp, body = self.get(url)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080047 body = json.loads(body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053048 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 Luoe0884a32012-09-25 17:24:17 +080061 resp, body = self.post('os-security-groups', post_body, self.headers)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053062 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 Luoe0884a32012-09-25 17:24:17 +080067 return self.delete('os-security-groups/%s' % str(security_group_id))
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053068
69 def create_security_group_rule(self, parent_group_id, ip_proto, from_port,
Zhongyue Luoe0884a32012-09-25 17:24:17 +080070 to_port, **kwargs):
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053071 """
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 fattarsi5098fa22012-04-17 13:27:00 -070091 resp, body = self.post(url, post_body, self.headers)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053092 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 Luoe0884a32012-09-25 17:24:17 +080097 return self.delete('os-security-group-rules/%s' % str(group_rule_id))