blob: 5aefa7b016c562d1d0fb1620c89305641ae6010a [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
Masayuki Igawabfa07602015-01-20 18:47:17 +090019from tempest_lib import exceptions as lib_exc
20
Marc Koderer6fbd74f2014-08-04 09:38:19 +020021from tempest.api_schema.response.compute.v2 import security_groups as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000022from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000023
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080024
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000025class SecurityGroupsClientJSON(service_client.ServiceClient):
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080026
27 def list_security_groups(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050028 """List all security groups for a user."""
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080029
30 url = 'os-security-groups'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050031 if params:
32 url += '?%s' % urllib.urlencode(params)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080033
chris fattarsi5098fa22012-04-17 13:27:00 -070034 resp, body = self.get(url)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080035 body = json.loads(body)
Yuiko Takadac3aa1102014-03-19 15:19:19 +000036 self.validate_response(schema.list_security_groups, resp, body)
David Kranz9964b4e2015-02-06 15:45:29 -050037 return service_client.ResponseBodyList(resp, body['security_groups'])
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080038
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053039 def get_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050040 """Get the details of a Security Group."""
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053041 url = "os-security-groups/%s" % str(security_group_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070042 resp, body = self.get(url)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080043 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000044 self.validate_response(schema.get_security_group, resp, body)
David Kranz9964b4e2015-02-06 15:45:29 -050045 return service_client.ResponseBody(resp, body['security_group'])
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053046
47 def create_security_group(self, name, description):
48 """
49 Creates a new security group.
50 name (Required): Name of security group.
51 description (Required): Description of security group.
52 """
53 post_body = {
54 'name': name,
55 'description': description,
56 }
57 post_body = json.dumps({'security_group': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020058 resp, body = self.post('os-security-groups', post_body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053059 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000060 self.validate_response(schema.get_security_group, resp, body)
David Kranz9964b4e2015-02-06 15:45:29 -050061 return service_client.ResponseBody(resp, body['security_group'])
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053062
huangtianhua248a7bf2013-10-21 11:23:39 +080063 def update_security_group(self, security_group_id, name=None,
64 description=None):
65 """
66 Update a security group.
67 security_group_id: a security_group to update
68 name: new name of security group
69 description: new description of security group
70 """
71 post_body = {}
72 if name:
73 post_body['name'] = name
74 if description:
75 post_body['description'] = description
76 post_body = json.dumps({'security_group': post_body})
77 resp, body = self.put('os-security-groups/%s' % str(security_group_id),
vponomaryovf4c27f92014-02-18 10:56:42 +020078 post_body)
huangtianhua248a7bf2013-10-21 11:23:39 +080079 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000080 self.validate_response(schema.update_security_group, resp, body)
David Kranz9964b4e2015-02-06 15:45:29 -050081 return service_client.ResponseBody(resp, body['security_group'])
huangtianhua248a7bf2013-10-21 11:23:39 +080082
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053083 def delete_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050084 """Deletes the provided Security Group."""
Yuiko Takada80cc9f12014-04-04 16:21:21 +000085 resp, body = self.delete(
86 'os-security-groups/%s' % str(security_group_id))
87 self.validate_response(schema.delete_security_group, resp, body)
David Kranz9964b4e2015-02-06 15:45:29 -050088 return service_client.ResponseBody(resp, body)
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)
David Kranz9964b4e2015-02-06 15:45:29 -0500115 return service_client.ResponseBody(resp, body['security_group_rule'])
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +0530116
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)
David Kranz9964b4e2015-02-06 15:45:29 -0500122 return service_client.ResponseBody(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:
David Kranz9964b4e2015-02-06 15:45:29 -0500131 return service_client.ResponseBodyList(resp, sg['rules'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900132 raise lib_exc.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)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900137 except lib_exc.NotFound:
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800138 return True
139 return False
Matt Riedemannd2b96512014-10-13 10:18:16 -0700140
141 @property
142 def resource_type(self):
143 """Returns the primary type of resource this client works with."""
144 return 'security_group'