blob: 1ac52afaa6efbd4ff002020b13fd5744bcfc3d82 [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
Marc Koderer6fbd74f2014-08-04 09:38:19 +020019from tempest.api_schema.response.compute.v2 import security_groups as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000020from tempest.common import service_client
Attila Fazekasaea56302013-03-26 23:06:44 +010021from tempest import exceptions
Matthew Treinish684d8992014-01-30 16:27:40 +000022
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080023
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000024class SecurityGroupsClientJSON(service_client.ServiceClient):
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080025
26 def list_security_groups(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050027 """List all security groups for a user."""
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080028
29 url = 'os-security-groups'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050030 if params:
31 url += '?%s' % urllib.urlencode(params)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080032
chris fattarsi5098fa22012-04-17 13:27:00 -070033 resp, body = self.get(url)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080034 body = json.loads(body)
Yuiko Takadac3aa1102014-03-19 15:19:19 +000035 self.validate_response(schema.list_security_groups, resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053036 return resp, body['security_groups']
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080037
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053038 def get_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050039 """Get the details of a Security Group."""
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053040 url = "os-security-groups/%s" % str(security_group_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070041 resp, body = self.get(url)
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080042 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000043 self.validate_response(schema.get_security_group, resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053044 return resp, body['security_group']
45
46 def create_security_group(self, name, description):
47 """
48 Creates a new security group.
49 name (Required): Name of security group.
50 description (Required): Description of security group.
51 """
52 post_body = {
53 'name': name,
54 'description': description,
55 }
56 post_body = json.dumps({'security_group': post_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020057 resp, body = self.post('os-security-groups', post_body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053058 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000059 self.validate_response(schema.get_security_group, resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053060 return resp, body['security_group']
61
huangtianhua248a7bf2013-10-21 11:23:39 +080062 def update_security_group(self, security_group_id, name=None,
63 description=None):
64 """
65 Update a security group.
66 security_group_id: a security_group to update
67 name: new name of security group
68 description: new description of security group
69 """
70 post_body = {}
71 if name:
72 post_body['name'] = name
73 if description:
74 post_body['description'] = description
75 post_body = json.dumps({'security_group': post_body})
76 resp, body = self.put('os-security-groups/%s' % str(security_group_id),
vponomaryovf4c27f92014-02-18 10:56:42 +020077 post_body)
huangtianhua248a7bf2013-10-21 11:23:39 +080078 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000079 self.validate_response(schema.update_security_group, resp, body)
huangtianhua248a7bf2013-10-21 11:23:39 +080080 return resp, body['security_group']
81
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053082 def delete_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050083 """Deletes the provided Security Group."""
Yuiko Takada80cc9f12014-04-04 16:21:21 +000084 resp, body = self.delete(
85 'os-security-groups/%s' % str(security_group_id))
86 self.validate_response(schema.delete_security_group, resp, body)
87 return resp, body
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053088
89 def create_security_group_rule(self, parent_group_id, ip_proto, from_port,
Zhongyue Luoe0884a32012-09-25 17:24:17 +080090 to_port, **kwargs):
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053091 """
92 Creating a new security group rules.
93 parent_group_id :ID of Security group
94 ip_protocol : ip_proto (icmp, tcp, udp).
95 from_port: Port at start of range.
96 to_port : Port at end of range.
97 Following optional keyword arguments are accepted:
98 cidr : CIDR for address range.
99 group_id : ID of the Source group
100 """
101 post_body = {
102 'parent_group_id': parent_group_id,
103 'ip_protocol': ip_proto,
104 'from_port': from_port,
105 'to_port': to_port,
106 'cidr': kwargs.get('cidr'),
107 'group_id': kwargs.get('group_id'),
108 }
109 post_body = json.dumps({'security_group_rule': post_body})
110 url = 'os-security-group-rules'
vponomaryovf4c27f92014-02-18 10:56:42 +0200111 resp, body = self.post(url, post_body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +0530112 body = json.loads(body)
Ghanshyamf79c7992014-04-01 12:20:12 +0900113 self.validate_response(schema.create_security_group_rule, resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +0530114 return resp, body['security_group_rule']
115
116 def delete_security_group_rule(self, group_rule_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500117 """Deletes the provided Security Group rule."""
Ghanshyamf79c7992014-04-01 12:20:12 +0900118 resp, body = self.delete('os-security-group-rules/%s' %
119 str(group_rule_id))
120 self.validate_response(schema.delete_security_group_rule, resp, body)
121 return resp, body
Leo Toyodace581f62013-03-07 16:16:06 +0900122
123 def list_security_group_rules(self, security_group_id):
124 """List all rules for a security group."""
125 resp, body = self.get('os-security-groups')
126 body = json.loads(body)
Yuiko Takadac3aa1102014-03-19 15:19:19 +0000127 self.validate_response(schema.list_security_groups, resp, body)
Leo Toyodace581f62013-03-07 16:16:06 +0900128 for sg in body['security_groups']:
129 if sg['id'] == security_group_id:
130 return resp, sg['rules']
131 raise exceptions.NotFound('No such Security Group')
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800132
133 def is_resource_deleted(self, id):
134 try:
135 self.get_security_group(id)
136 except exceptions.NotFound:
137 return True
138 return False
Matt Riedemannd2b96512014-10-13 10:18:16 -0700139
140 @property
141 def resource_type(self):
142 """Returns the primary type of resource this client works with."""
143 return 'security_group'