blob: c0b667b1154ebe0c97be1a5113a99bd1a8a7d4de [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
Matthew Treinish21905512015-07-13 10:33:35 -040016from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040017from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090018from tempest_lib import exceptions as lib_exc
19
ghanshyamaa93b4b2015-03-20 11:03:44 +090020from tempest.api_schema.response.compute.v2_1 import security_groups as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000021from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000022
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080023
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000024class SecurityGroupsClient(service_client.ServiceClient):
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080025
Ken'ichi Ohmichi118776d2015-07-01 08:15:00 +000026 def list_security_groups(self, **params):
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)
David Kranz9964b4e2015-02-06 15:45:29 -050036 return service_client.ResponseBodyList(resp, body['security_groups'])
Ravikumar Venkatesaneeb9caa2012-01-12 16:42:36 -080037
Ken'ichi Ohmichi217f2f32015-06-17 02:52:44 +000038 def show_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050039 """Get the details of a Security Group."""
Ken'ichi Ohmichi217f2f32015-06-17 02:52:44 +000040 url = "os-security-groups/%s" % 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)
David Kranz9964b4e2015-02-06 15:45:29 -050044 return service_client.ResponseBody(resp, body['security_group'])
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053045
Ken'ichi Ohmichi34563cc2015-07-21 00:53:17 +000046 def create_security_group(self, **kwargs):
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053047 """
48 Creates a new security group.
49 name (Required): Name of security group.
50 description (Required): Description of security group.
51 """
Ken'ichi Ohmichi34563cc2015-07-21 00:53:17 +000052 post_body = json.dumps({'security_group': kwargs})
vponomaryovf4c27f92014-02-18 10:56:42 +020053 resp, body = self.post('os-security-groups', post_body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053054 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000055 self.validate_response(schema.get_security_group, resp, body)
David Kranz9964b4e2015-02-06 15:45:29 -050056 return service_client.ResponseBody(resp, body['security_group'])
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053057
Ken'ichi Ohmichi34563cc2015-07-21 00:53:17 +000058 def update_security_group(self, security_group_id, **kwargs):
huangtianhua248a7bf2013-10-21 11:23:39 +080059 """
60 Update a security group.
61 security_group_id: a security_group to update
62 name: new name of security group
63 description: new description of security group
64 """
Ken'ichi Ohmichi34563cc2015-07-21 00:53:17 +000065 post_body = json.dumps({'security_group': kwargs})
Ken'ichi Ohmichi217f2f32015-06-17 02:52:44 +000066 resp, body = self.put('os-security-groups/%s' % security_group_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020067 post_body)
huangtianhua248a7bf2013-10-21 11:23:39 +080068 body = json.loads(body)
Yuiko Takadab34c1612014-03-26 15:31:47 +000069 self.validate_response(schema.update_security_group, resp, body)
David Kranz9964b4e2015-02-06 15:45:29 -050070 return service_client.ResponseBody(resp, body['security_group'])
huangtianhua248a7bf2013-10-21 11:23:39 +080071
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053072 def delete_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050073 """Deletes the provided Security Group."""
Yuiko Takada80cc9f12014-04-04 16:21:21 +000074 resp, body = self.delete(
Ken'ichi Ohmichi217f2f32015-06-17 02:52:44 +000075 'os-security-groups/%s' % security_group_id)
Yuiko Takada80cc9f12014-04-04 16:21:21 +000076 self.validate_response(schema.delete_security_group, resp, body)
David Kranz9964b4e2015-02-06 15:45:29 -050077 return service_client.ResponseBody(resp, body)
rajalakshmi-ganesan37b32b62012-02-06 17:21:20 +053078
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +080079 def is_resource_deleted(self, id):
80 try:
Ken'ichi Ohmichi217f2f32015-06-17 02:52:44 +000081 self.show_security_group(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090082 except lib_exc.NotFound:
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +080083 return True
84 return False
Matt Riedemannd2b96512014-10-13 10:18:16 -070085
86 @property
87 def resource_type(self):
88 """Returns the primary type of resource this client works with."""
89 return 'security_group'