Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # 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 | |
| 16 | from oslo_serialization import jsonutils as json |
| 17 | from six.moves.urllib import parse as urllib |
| 18 | |
| 19 | from tempest.lib.api_schema.response.compute.v2_1 import \ |
| 20 | security_groups as schema |
| 21 | from tempest.lib.common import rest_client |
| 22 | from tempest.lib import exceptions as lib_exc |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 23 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 24 | |
| 25 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 26 | class SecurityGroupsClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 27 | |
| 28 | def list_security_groups(self, **params): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 29 | """List all security groups for a user. |
| 30 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 31 | For a full list of available parameters, please refer to the official |
| 32 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 33 | https://docs.openstack.org/api-ref/compute/#list-security-groups |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 34 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 35 | |
| 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 46 | """Get the details of a Security Group. |
| 47 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 48 | For a full list of available parameters, please refer to the official |
| 49 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 50 | https://docs.openstack.org/api-ref/compute/#show-security-group-details |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 51 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 52 | 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 Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 61 | For a full list of available parameters, please refer to the official |
| 62 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 63 | https://docs.openstack.org/api-ref/compute/#create-security-group |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 64 | """ |
| 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 Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 74 | For a full list of available parameters, please refer to the official |
| 75 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 76 | https://docs.openstack.org/api-ref/compute/#update-security-group |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 77 | """ |
| 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 Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 86 | """Delete the provided Security Group. |
| 87 | |
Dong Ma | d12c233 | 2016-10-19 01:36:27 -0700 | [diff] [blame] | 88 | For a full list of available parameters, please refer to the official |
| 89 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 90 | https://docs.openstack.org/api-ref/compute/#delete-security-group |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 91 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 92 | 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' |