blob: edaf4a3a8255691651261982aaefa6f662058cb9 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05002# 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 Venkatesaneeb9caa2012-01-12 16:42:36 -080016import json
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050017import urllib
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080018
Matthew Treinisha83a16e2012-12-07 13:44:02 -050019from tempest.common.rest_client import RestClient
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
Attila Fazekasaea56302013-03-26 23:06:44 +010021from tempest import exceptions
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022
Matthew Treinish684d8992014-01-30 16:27:40 +000023CONF = config.CONF
24
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080025
Vincent Houead03dc2012-08-24 21:35:11 +080026class SecurityGroupsClientJSON(RestClient):
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080027
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000028 def __init__(self, auth_provider):
29 super(SecurityGroupsClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.compute.catalog_type
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080031
32 def list_security_groups(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050033 """List all security groups for a user."""
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080034
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):
Sean Daguef237ccb2013-01-04 15:19:14 -050044 """Get the details of a Security Group."""
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053045 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
huangtianhua248a7bf2013-10-21 11:23:39 +080065 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-ganesan37b32b62012-02-06 17:21:20 +053084 def delete_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050085 """Deletes the provided Security Group."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080086 return self.delete('os-security-groups/%s' % str(security_group_id))
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053087
88 def create_security_group_rule(self, parent_group_id, ip_proto, from_port,
Zhongyue Luoe0884a32012-09-25 17:24:17 +080089 to_port, **kwargs):
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053090 """
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 fattarsi5098fa22012-04-17 13:27:00 -0700110 resp, body = self.post(url, post_body, self.headers)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +0530111 body = json.loads(body)
112 return resp, body['security_group_rule']
113
114 def delete_security_group_rule(self, group_rule_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500115 """Deletes the provided Security Group rule."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800116 return self.delete('os-security-group-rules/%s' % str(group_rule_id))
Leo Toyodace581f62013-03-07 16:16:06 +0900117
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')