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