blob: 9493144ce25e3534e63ef31898cd791e0883fd0c [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Copyright 2012 OpenStack Foundation
2# 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
16from oslo_serialization import jsonutils as json
17from six.moves.urllib import parse as urllib
18
19from tempest.lib.api_schema.response.compute.v2_1 import \
20 security_groups as schema
21from tempest.lib.common import rest_client
22from tempest.lib import exceptions as lib_exc
Ghanshyamee9af302016-02-25 06:12:43 +090023from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050024
25
Ghanshyamee9af302016-02-25 06:12:43 +090026class SecurityGroupsClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050027
28 def list_security_groups(self, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080029 """List all security groups for a user.
30
Dong Mad12c2332016-10-19 01:36:27 -070031 For a full list of available parameters, please refer to the official
32 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020033 https://docs.openstack.org/api-ref/compute/#list-security-groups
Lv Fumei7e326332016-07-08 15:18:03 +080034 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050035
36 url = 'os-security-groups'
37 if params:
38 url += '?%s' % urllib.urlencode(params)
39
40 resp, body = self.get(url)
41 body = json.loads(body)
42 self.validate_response(schema.list_security_groups, resp, body)
43 return rest_client.ResponseBody(resp, body)
44
45 def show_security_group(self, security_group_id):
Lv Fumei7e326332016-07-08 15:18:03 +080046 """Get the details of a Security Group.
47
Dong Mad12c2332016-10-19 01:36:27 -070048 For a full list of available parameters, please refer to the official
49 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020050 https://docs.openstack.org/api-ref/compute/#show-security-group-details
Lv Fumei7e326332016-07-08 15:18:03 +080051 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050052 url = "os-security-groups/%s" % security_group_id
53 resp, body = self.get(url)
54 body = json.loads(body)
55 self.validate_response(schema.get_security_group, resp, body)
56 return rest_client.ResponseBody(resp, body)
57
58 def create_security_group(self, **kwargs):
59 """Create a new security group.
60
Dong Mad12c2332016-10-19 01:36:27 -070061 For a full list of available parameters, please refer to the official
62 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020063 https://docs.openstack.org/api-ref/compute/#create-security-group
Matthew Treinish9e26ca82016-02-23 11:43:20 -050064 """
65 post_body = json.dumps({'security_group': kwargs})
66 resp, body = self.post('os-security-groups', post_body)
67 body = json.loads(body)
68 self.validate_response(schema.get_security_group, resp, body)
69 return rest_client.ResponseBody(resp, body)
70
71 def update_security_group(self, security_group_id, **kwargs):
72 """Update a security group.
73
Dong Mad12c2332016-10-19 01:36:27 -070074 For a full list of available parameters, please refer to the official
75 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020076 https://docs.openstack.org/api-ref/compute/#update-security-group
Matthew Treinish9e26ca82016-02-23 11:43:20 -050077 """
78 post_body = json.dumps({'security_group': kwargs})
79 resp, body = self.put('os-security-groups/%s' % security_group_id,
80 post_body)
81 body = json.loads(body)
82 self.validate_response(schema.update_security_group, resp, body)
83 return rest_client.ResponseBody(resp, body)
84
85 def delete_security_group(self, security_group_id):
Lv Fumei7e326332016-07-08 15:18:03 +080086 """Delete the provided Security Group.
87
Dong Mad12c2332016-10-19 01:36:27 -070088 For a full list of available parameters, please refer to the official
89 API reference:
Andreas Jaegerbf30ae72019-07-22 19:22:57 +020090 https://docs.openstack.org/api-ref/compute/#delete-security-group
Lv Fumei7e326332016-07-08 15:18:03 +080091 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050092 resp, body = self.delete(
93 'os-security-groups/%s' % security_group_id)
94 self.validate_response(schema.delete_security_group, resp, body)
95 return rest_client.ResponseBody(resp, body)
96
97 def is_resource_deleted(self, id):
98 try:
99 self.show_security_group(id)
100 except lib_exc.NotFound:
101 return True
102 return False
103
104 @property
105 def resource_type(self):
106 """Return the primary type of resource this client works with."""
107 return 'security_group'