Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 1 | # Copyright 2012 IBM Corp. |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [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 | |
| 16 | from lxml import etree |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 17 | import urllib |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 18 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 19 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
Attila Fazekas | aea5630 | 2013-03-26 23:06:44 +0100 | [diff] [blame] | 21 | from tempest import exceptions |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 22 | from tempest.services.compute.xml.common import Document |
| 23 | from tempest.services.compute.xml.common import Element |
| 24 | from tempest.services.compute.xml.common import Text |
| 25 | from tempest.services.compute.xml.common import xml_to_json |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 26 | from tempest.services.compute.xml.common import XMLNS_11 |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 27 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 28 | CONF = config.CONF |
| 29 | |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 30 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 31 | class SecurityGroupsClientXML(rest_client.RestClient): |
| 32 | TYPE = "xml" |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 33 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 34 | def __init__(self, auth_provider): |
| 35 | super(SecurityGroupsClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 36 | self.service = CONF.compute.catalog_type |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 37 | |
| 38 | def _parse_array(self, node): |
| 39 | array = [] |
| 40 | for child in node.getchildren(): |
| 41 | array.append(xml_to_json(child)) |
| 42 | return array |
| 43 | |
| 44 | def _parse_body(self, body): |
| 45 | json = xml_to_json(body) |
| 46 | return json |
| 47 | |
| 48 | def list_security_groups(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 49 | """List all security groups for a user.""" |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 50 | |
| 51 | url = 'os-security-groups' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 52 | if params: |
| 53 | url += '?%s' % urllib.urlencode(params) |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 54 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 55 | resp, body = self.get(url) |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 56 | body = self._parse_array(etree.fromstring(body)) |
| 57 | return resp, body |
| 58 | |
| 59 | def get_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 60 | """Get the details of a Security Group.""" |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 61 | url = "os-security-groups/%s" % str(security_group_id) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 62 | resp, body = self.get(url) |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 63 | body = self._parse_body(etree.fromstring(body)) |
| 64 | return resp, body |
| 65 | |
| 66 | def create_security_group(self, name, description): |
| 67 | """ |
| 68 | Creates a new security group. |
| 69 | name (Required): Name of security group. |
| 70 | description (Required): Description of security group. |
| 71 | """ |
| 72 | security_group = Element("security_group", name=name) |
| 73 | des = Element("description") |
| 74 | des.append(Text(content=description)) |
| 75 | security_group.append(des) |
| 76 | resp, body = self.post('os-security-groups', |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 77 | str(Document(security_group))) |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 78 | body = self._parse_body(etree.fromstring(body)) |
| 79 | return resp, body |
| 80 | |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 81 | def update_security_group(self, security_group_id, name=None, |
| 82 | description=None): |
| 83 | """ |
| 84 | Update a security group. |
| 85 | security_group_id: a security_group to update |
| 86 | name: new name of security group |
| 87 | description: new description of security group |
| 88 | """ |
| 89 | security_group = Element("security_group") |
| 90 | if name: |
| 91 | sg_name = Element("name") |
| 92 | sg_name.append(Text(content=name)) |
| 93 | security_group.append(sg_name) |
| 94 | if description: |
| 95 | des = Element("description") |
| 96 | des.append(Text(content=description)) |
| 97 | security_group.append(des) |
| 98 | resp, body = self.put('os-security-groups/%s' % |
| 99 | str(security_group_id), |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 100 | str(Document(security_group))) |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 101 | body = self._parse_body(etree.fromstring(body)) |
| 102 | return resp, body |
| 103 | |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 104 | def delete_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 105 | """Deletes the provided Security Group.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 106 | return self.delete('os-security-groups/%s' % str(security_group_id)) |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 107 | |
| 108 | def create_security_group_rule(self, parent_group_id, ip_proto, from_port, |
| 109 | to_port, **kwargs): |
| 110 | """ |
| 111 | Creating a new security group rules. |
| 112 | parent_group_id :ID of Security group |
| 113 | ip_protocol : ip_proto (icmp, tcp, udp). |
| 114 | from_port: Port at start of range. |
| 115 | to_port : Port at end of range. |
| 116 | Following optional keyword arguments are accepted: |
| 117 | cidr : CIDR for address range. |
| 118 | group_id : ID of the Source group |
| 119 | """ |
| 120 | group_rule = Element("security_group_rule") |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 121 | |
Matt Riedemann | 2ef090c | 2013-06-20 12:23:26 -0700 | [diff] [blame] | 122 | elements = dict() |
| 123 | elements['cidr'] = kwargs.get('cidr') |
| 124 | elements['group_id'] = kwargs.get('group_id') |
Andrea Frittoli | 59f0028 | 2013-06-19 18:08:44 +0100 | [diff] [blame] | 125 | elements['parent_group_id'] = parent_group_id |
| 126 | elements['ip_protocol'] = ip_proto |
| 127 | elements['from_port'] = from_port |
| 128 | elements['to_port'] = to_port |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 129 | |
Andrea Frittoli | 59f0028 | 2013-06-19 18:08:44 +0100 | [diff] [blame] | 130 | for k, v in elements.items(): |
| 131 | if v is not None: |
| 132 | element = Element(k) |
| 133 | element.append(Text(content=str(v))) |
| 134 | group_rule.append(element) |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 135 | |
| 136 | url = 'os-security-group-rules' |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 137 | resp, body = self.post(url, str(Document(group_rule))) |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 138 | body = self._parse_body(etree.fromstring(body)) |
| 139 | return resp, body |
| 140 | |
| 141 | def delete_security_group_rule(self, group_rule_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 142 | """Deletes the provided Security Group rule.""" |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 143 | return self.delete('os-security-group-rules/%s' % |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 144 | str(group_rule_id)) |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 145 | |
| 146 | def list_security_group_rules(self, security_group_id): |
| 147 | """List all rules for a security group.""" |
| 148 | url = "os-security-groups" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 149 | resp, body = self.get(url) |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 150 | body = etree.fromstring(body) |
| 151 | secgroups = body.getchildren() |
| 152 | for secgroup in secgroups: |
| 153 | if secgroup.get('id') == security_group_id: |
| 154 | node = secgroup.find('{%s}rules' % XMLNS_11) |
| 155 | rules = [xml_to_json(x) for x in node.getchildren()] |
| 156 | return resp, rules |
| 157 | raise exceptions.NotFound('No such Security Group') |
Zhi Kun Liu | 02e7a7b | 2014-01-08 16:08:32 +0800 | [diff] [blame] | 158 | |
| 159 | def is_resource_deleted(self, id): |
| 160 | try: |
| 161 | self.get_security_group(id) |
| 162 | except exceptions.NotFound: |
| 163 | return True |
| 164 | return False |