Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | # |
Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 3 | # Copyright 2012 IBM Corp. |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | from lxml import etree |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 19 | import urllib |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 20 | |
| 21 | from tempest.common.rest_client import RestClientXML |
Attila Fazekas | aea5630 | 2013-03-26 23:06:44 +0100 | [diff] [blame] | 22 | from tempest import exceptions |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 23 | from tempest.services.compute.xml.common import Document |
| 24 | from tempest.services.compute.xml.common import Element |
| 25 | from tempest.services.compute.xml.common import Text |
| 26 | from tempest.services.compute.xml.common import xml_to_json |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 27 | from tempest.services.compute.xml.common import XMLNS_11 |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 28 | |
| 29 | |
| 30 | class SecurityGroupsClientXML(RestClientXML): |
| 31 | |
| 32 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 33 | super(SecurityGroupsClientXML, self).__init__( |
Sean Dague | 14c6818 | 2013-04-14 15:34:30 -0400 | [diff] [blame] | 34 | config, username, password, |
| 35 | auth_url, tenant_name) |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 36 | self.service = self.config.compute.catalog_type |
| 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 | |
| 55 | resp, body = self.get(url, self.headers) |
| 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) |
| 62 | resp, body = self.get(url, self.headers) |
| 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', |
| 77 | str(Document(security_group)), |
| 78 | self.headers) |
| 79 | body = self._parse_body(etree.fromstring(body)) |
| 80 | return resp, body |
| 81 | |
huangtianhua | 248a7bf | 2013-10-21 11:23:39 +0800 | [diff] [blame] | 82 | def update_security_group(self, security_group_id, name=None, |
| 83 | description=None): |
| 84 | """ |
| 85 | Update a security group. |
| 86 | security_group_id: a security_group to update |
| 87 | name: new name of security group |
| 88 | description: new description of security group |
| 89 | """ |
| 90 | security_group = Element("security_group") |
| 91 | if name: |
| 92 | sg_name = Element("name") |
| 93 | sg_name.append(Text(content=name)) |
| 94 | security_group.append(sg_name) |
| 95 | if description: |
| 96 | des = Element("description") |
| 97 | des.append(Text(content=description)) |
| 98 | security_group.append(des) |
| 99 | resp, body = self.put('os-security-groups/%s' % |
| 100 | str(security_group_id), |
| 101 | str(Document(security_group)), |
| 102 | self.headers) |
| 103 | body = self._parse_body(etree.fromstring(body)) |
| 104 | return resp, body |
| 105 | |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 106 | def delete_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 107 | """Deletes the provided Security Group.""" |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 108 | return self.delete('os-security-groups/%s' % |
| 109 | str(security_group_id), self.headers) |
| 110 | |
| 111 | def create_security_group_rule(self, parent_group_id, ip_proto, from_port, |
| 112 | to_port, **kwargs): |
| 113 | """ |
| 114 | Creating a new security group rules. |
| 115 | parent_group_id :ID of Security group |
| 116 | ip_protocol : ip_proto (icmp, tcp, udp). |
| 117 | from_port: Port at start of range. |
| 118 | to_port : Port at end of range. |
| 119 | Following optional keyword arguments are accepted: |
| 120 | cidr : CIDR for address range. |
| 121 | group_id : ID of the Source group |
| 122 | """ |
| 123 | group_rule = Element("security_group_rule") |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 124 | |
Matt Riedemann | 2ef090c | 2013-06-20 12:23:26 -0700 | [diff] [blame] | 125 | elements = dict() |
| 126 | elements['cidr'] = kwargs.get('cidr') |
| 127 | elements['group_id'] = kwargs.get('group_id') |
Andrea Frittoli | 59f0028 | 2013-06-19 18:08:44 +0100 | [diff] [blame] | 128 | elements['parent_group_id'] = parent_group_id |
| 129 | elements['ip_protocol'] = ip_proto |
| 130 | elements['from_port'] = from_port |
| 131 | elements['to_port'] = to_port |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 132 | |
Andrea Frittoli | 59f0028 | 2013-06-19 18:08:44 +0100 | [diff] [blame] | 133 | for k, v in elements.items(): |
| 134 | if v is not None: |
| 135 | element = Element(k) |
| 136 | element.append(Text(content=str(v))) |
| 137 | group_rule.append(element) |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 138 | |
| 139 | url = 'os-security-group-rules' |
| 140 | resp, body = self.post(url, str(Document(group_rule)), self.headers) |
| 141 | body = self._parse_body(etree.fromstring(body)) |
| 142 | return resp, body |
| 143 | |
| 144 | def delete_security_group_rule(self, group_rule_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 145 | """Deletes the provided Security Group rule.""" |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 146 | return self.delete('os-security-group-rules/%s' % |
| 147 | str(group_rule_id), self.headers) |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 148 | |
| 149 | def list_security_group_rules(self, security_group_id): |
| 150 | """List all rules for a security group.""" |
| 151 | url = "os-security-groups" |
| 152 | resp, body = self.get(url, self.headers) |
| 153 | body = etree.fromstring(body) |
| 154 | secgroups = body.getchildren() |
| 155 | for secgroup in secgroups: |
| 156 | if secgroup.get('id') == security_group_id: |
| 157 | node = secgroup.find('{%s}rules' % XMLNS_11) |
| 158 | rules = [xml_to_json(x) for x in node.getchildren()] |
| 159 | return resp, rules |
| 160 | raise exceptions.NotFound('No such Security Group') |