ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 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 | |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 16 | import json |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 17 | import urllib |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 18 | |
Yuiko Takada | c3aa110 | 2014-03-19 15:19:19 +0000 | [diff] [blame] | 19 | from tempest.api_schema.compute.v2 import security_groups as schema |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 20 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 21 | from tempest import config |
Attila Fazekas | aea5630 | 2013-03-26 23:06:44 +0100 | [diff] [blame] | 22 | from tempest import exceptions |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 23 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 24 | CONF = config.CONF |
| 25 | |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 26 | |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 27 | class SecurityGroupsClientJSON(rest_client.RestClient): |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 28 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 29 | def __init__(self, auth_provider): |
| 30 | super(SecurityGroupsClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 31 | self.service = CONF.compute.catalog_type |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 32 | |
| 33 | def list_security_groups(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 34 | """List all security groups for a user.""" |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 35 | |
| 36 | url = 'os-security-groups' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 37 | if params: |
| 38 | url += '?%s' % urllib.urlencode(params) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 39 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 40 | resp, body = self.get(url) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 41 | body = json.loads(body) |
Yuiko Takada | c3aa110 | 2014-03-19 15:19:19 +0000 | [diff] [blame] | 42 | self.validate_response(schema.list_security_groups, resp, body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 43 | return resp, body['security_groups'] |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 44 | |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 45 | def get_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 46 | """Get the details of a Security Group.""" |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 47 | url = "os-security-groups/%s" % str(security_group_id) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 48 | resp, body = self.get(url) |
Ravikumar Venkatesan | eeb9caa | 2012-01-12 16:42:36 -0800 | [diff] [blame] | 49 | body = json.loads(body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 50 | return resp, body['security_group'] |
| 51 | |
| 52 | def create_security_group(self, name, description): |
| 53 | """ |
| 54 | Creates a new security group. |
| 55 | name (Required): Name of security group. |
| 56 | description (Required): Description of security group. |
| 57 | """ |
| 58 | post_body = { |
| 59 | 'name': name, |
| 60 | 'description': description, |
| 61 | } |
| 62 | post_body = json.dumps({'security_group': post_body}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 63 | resp, body = self.post('os-security-groups', post_body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 64 | body = json.loads(body) |
| 65 | return resp, body['security_group'] |
| 66 | |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 67 | def update_security_group(self, security_group_id, name=None, |
| 68 | description=None): |
| 69 | """ |
| 70 | Update a security group. |
| 71 | security_group_id: a security_group to update |
| 72 | name: new name of security group |
| 73 | description: new description of security group |
| 74 | """ |
| 75 | post_body = {} |
| 76 | if name: |
| 77 | post_body['name'] = name |
| 78 | if description: |
| 79 | post_body['description'] = description |
| 80 | post_body = json.dumps({'security_group': post_body}) |
| 81 | resp, body = self.put('os-security-groups/%s' % str(security_group_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 82 | post_body) |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 83 | body = json.loads(body) |
| 84 | return resp, body['security_group'] |
| 85 | |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 86 | def delete_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 87 | """Deletes the provided Security Group.""" |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 88 | return self.delete('os-security-groups/%s' % str(security_group_id)) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 89 | |
| 90 | def create_security_group_rule(self, parent_group_id, ip_proto, from_port, |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 91 | to_port, **kwargs): |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 92 | """ |
| 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' |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 112 | resp, body = self.post(url, post_body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 113 | body = json.loads(body) |
Ghanshyam | f79c799 | 2014-04-01 12:20:12 +0900 | [diff] [blame^] | 114 | self.validate_response(schema.create_security_group_rule, resp, body) |
rajalakshmi-ganesan | 37b32b6 | 2012-02-06 17:21:20 +0530 | [diff] [blame] | 115 | return resp, body['security_group_rule'] |
| 116 | |
| 117 | def delete_security_group_rule(self, group_rule_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 118 | """Deletes the provided Security Group rule.""" |
Ghanshyam | f79c799 | 2014-04-01 12:20:12 +0900 | [diff] [blame^] | 119 | 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) |
| 122 | return resp, body |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 123 | |
| 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 Takada | c3aa110 | 2014-03-19 15:19:19 +0000 | [diff] [blame] | 128 | self.validate_response(schema.list_security_groups, resp, body) |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 129 | for sg in body['security_groups']: |
| 130 | if sg['id'] == security_group_id: |
| 131 | return resp, sg['rules'] |
| 132 | raise exceptions.NotFound('No such Security Group') |
Zhi Kun Liu | 02e7a7b | 2014-01-08 16:08:32 +0800 | [diff] [blame] | 133 | |
| 134 | def is_resource_deleted(self, id): |
| 135 | try: |
| 136 | self.get_security_group(id) |
| 137 | except exceptions.NotFound: |
| 138 | return True |
| 139 | return False |