blob: 7411fb7a82d2f142a9260e511ecce170e57a2d44 [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
Yuiko Takadac3aa1102014-03-19 15:19:19 +000019from tempest.api_schema.compute.v2 import security_groups as schema
Haiwei Xuab924622014-03-05 04:33:51 +090020from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
Attila Fazekasaea56302013-03-26 23:06:44 +010022from tempest import exceptions
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023
Matthew Treinish684d8992014-01-30 16:27:40 +000024CONF = config.CONF
25
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080026
Haiwei Xuab924622014-03-05 04:33:51 +090027class SecurityGroupsClientJSON(rest_client.RestClient):
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080028
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000029 def __init__(self, auth_provider):
30 super(SecurityGroupsClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000031 self.service = CONF.compute.catalog_type
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080032
33 def list_security_groups(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050034 """List all security groups for a user."""
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080035
36 url = 'os-security-groups'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050037 if params:
38 url += '?%s' % urllib.urlencode(params)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080039
chris fattarsi5098fa22012-04-17 13:27:00 -070040 resp, body = self.get(url)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080041 body = json.loads(body)
Yuiko Takadac3aa1102014-03-19 15:19:19 +000042 self.validate_response(schema.list_security_groups, resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053043 return resp, body['security_groups']
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080044
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053045 def get_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050046 """Get the details of a Security Group."""
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053047 url = "os-security-groups/%s" % str(security_group_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070048 resp, body = self.get(url)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080049 body = json.loads(body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053050 return resp, body['security_group']
51
52 def create_security_group(self, name, description):
53 """
54 Creates a new security group.
55 name (Required): Name of security group.
56 description (Required): Description of security group.
57 """
58 post_body = {
59 'name': name,
60 'description': description,
61 }
62 post_body = json.dumps({'security_group': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020063 resp, body = self.post('os-security-groups', post_body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053064 body = json.loads(body)
65 return resp, body['security_group']
66
huangtianhua248a7bf2013-10-21 11:23:39 +080067 def update_security_group(self, security_group_id, name=None,
68 description=None):
69 """
70 Update a security group.
71 security_group_id: a security_group to update
72 name: new name of security group
73 description: new description of security group
74 """
75 post_body = {}
76 if name:
77 post_body['name'] = name
78 if description:
79 post_body['description'] = description
80 post_body = json.dumps({'security_group': post_body})
81 resp, body = self.put('os-security-groups/%s' % str(security_group_id),
vponomaryovf4c27f92014-02-18 10:56:42 +020082 post_body)
huangtianhua248a7bf2013-10-21 11:23:39 +080083 body = json.loads(body)
84 return resp, body['security_group']
85
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053086 def delete_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050087 """Deletes the provided Security Group."""
Zhongyue Luoe0884a32012-09-25 17:24:17 +080088 return self.delete('os-security-groups/%s' % str(security_group_id))
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053089
90 def create_security_group_rule(self, parent_group_id, ip_proto, from_port,
Zhongyue Luoe0884a32012-09-25 17:24:17 +080091 to_port, **kwargs):
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053092 """
93 Creating a new security group rules.
94 parent_group_id :ID of Security group
95 ip_protocol : ip_proto (icmp, tcp, udp).
96 from_port: Port at start of range.
97 to_port : Port at end of range.
98 Following optional keyword arguments are accepted:
99 cidr : CIDR for address range.
100 group_id : ID of the Source group
101 """
102 post_body = {
103 'parent_group_id': parent_group_id,
104 'ip_protocol': ip_proto,
105 'from_port': from_port,
106 'to_port': to_port,
107 'cidr': kwargs.get('cidr'),
108 'group_id': kwargs.get('group_id'),
109 }
110 post_body = json.dumps({'security_group_rule': post_body})
111 url = 'os-security-group-rules'
vponomaryovf4c27f92014-02-18 10:56:42 +0200112 resp, body = self.post(url, post_body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +0530113 body = json.loads(body)
Ghanshyamf79c7992014-04-01 12:20:12 +0900114 self.validate_response(schema.create_security_group_rule, resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +0530115 return resp, body['security_group_rule']
116
117 def delete_security_group_rule(self, group_rule_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500118 """Deletes the provided Security Group rule."""
Ghanshyamf79c7992014-04-01 12:20:12 +0900119 resp, body = self.delete('os-security-group-rules/%s' %
120 str(group_rule_id))
121 self.validate_response(schema.delete_security_group_rule, resp, body)
122 return resp, body
Leo Toyodace581f62013-03-07 16:16:06 +0900123
124 def list_security_group_rules(self, security_group_id):
125 """List all rules for a security group."""
126 resp, body = self.get('os-security-groups')
127 body = json.loads(body)
Yuiko Takadac3aa1102014-03-19 15:19:19 +0000128 self.validate_response(schema.list_security_groups, resp, body)
Leo Toyodace581f62013-03-07 16:16:06 +0900129 for sg in body['security_groups']:
130 if sg['id'] == security_group_id:
131 return resp, sg['rules']
132 raise exceptions.NotFound('No such Security Group')
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800133
134 def is_resource_deleted(self, id):
135 try:
136 self.get_security_group(id)
137 except exceptions.NotFound:
138 return True
139 return False