blob: a86f3df2a9d74d9d45e9a60253dcbc976f6fab7c [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)
Yuiko Takadab34c1612014-03-26 15:31:47 +000050 self.validate_response(schema.get_security_group, resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053051 return resp, body['security_group']
52
53 def create_security_group(self, name, description):
54 """
55 Creates a new security group.
56 name (Required): Name of security group.
57 description (Required): Description of security group.
58 """
59 post_body = {
60 'name': name,
61 'description': description,
62 }
63 post_body = json.dumps({'security_group': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020064 resp, body = self.post('os-security-groups', post_body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053065 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000066 self.validate_response(schema.get_security_group, resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053067 return resp, body['security_group']
68
huangtianhua248a7bf2013-10-21 11:23:39 +080069 def update_security_group(self, security_group_id, name=None,
70 description=None):
71 """
72 Update a security group.
73 security_group_id: a security_group to update
74 name: new name of security group
75 description: new description of security group
76 """
77 post_body = {}
78 if name:
79 post_body['name'] = name
80 if description:
81 post_body['description'] = description
82 post_body = json.dumps({'security_group': post_body})
83 resp, body = self.put('os-security-groups/%s' % str(security_group_id),
vponomaryovf4c27f92014-02-18 10:56:42 +020084 post_body)
huangtianhua248a7bf2013-10-21 11:23:39 +080085 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000086 self.validate_response(schema.update_security_group, resp, body)
huangtianhua248a7bf2013-10-21 11:23:39 +080087 return resp, body['security_group']
88
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053089 def delete_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050090 """Deletes the provided Security Group."""
Yuiko Takada80cc9f12014-04-04 16:21:21 +000091 resp, body = self.delete(
92 'os-security-groups/%s' % str(security_group_id))
93 self.validate_response(schema.delete_security_group, resp, body)
94 return resp, body
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053095
96 def create_security_group_rule(self, parent_group_id, ip_proto, from_port,
Zhongyue Luoe0884a32012-09-25 17:24:17 +080097 to_port, **kwargs):
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053098 """
99 Creating a new security group rules.
100 parent_group_id :ID of Security group
101 ip_protocol : ip_proto (icmp, tcp, udp).
102 from_port: Port at start of range.
103 to_port : Port at end of range.
104 Following optional keyword arguments are accepted:
105 cidr : CIDR for address range.
106 group_id : ID of the Source group
107 """
108 post_body = {
109 'parent_group_id': parent_group_id,
110 'ip_protocol': ip_proto,
111 'from_port': from_port,
112 'to_port': to_port,
113 'cidr': kwargs.get('cidr'),
114 'group_id': kwargs.get('group_id'),
115 }
116 post_body = json.dumps({'security_group_rule': post_body})
117 url = 'os-security-group-rules'
vponomaryovf4c27f92014-02-18 10:56:42 +0200118 resp, body = self.post(url, post_body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +0530119 body = json.loads(body)
Ghanshyamf79c7992014-04-01 12:20:12 +0900120 self.validate_response(schema.create_security_group_rule, resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +0530121 return resp, body['security_group_rule']
122
123 def delete_security_group_rule(self, group_rule_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500124 """Deletes the provided Security Group rule."""
Ghanshyamf79c7992014-04-01 12:20:12 +0900125 resp, body = self.delete('os-security-group-rules/%s' %
126 str(group_rule_id))
127 self.validate_response(schema.delete_security_group_rule, resp, body)
128 return resp, body
Leo Toyodace581f62013-03-07 16:16:06 +0900129
130 def list_security_group_rules(self, security_group_id):
131 """List all rules for a security group."""
132 resp, body = self.get('os-security-groups')
133 body = json.loads(body)
Yuiko Takadac3aa1102014-03-19 15:19:19 +0000134 self.validate_response(schema.list_security_groups, resp, body)
Leo Toyodace581f62013-03-07 16:16:06 +0900135 for sg in body['security_groups']:
136 if sg['id'] == security_group_id:
137 return resp, sg['rules']
138 raise exceptions.NotFound('No such Security Group')
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800139
140 def is_resource_deleted(self, id):
141 try:
142 self.get_security_group(id)
143 except exceptions.NotFound:
144 return True
145 return False