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__( |
| 34 | config, username, password, |
| 35 | auth_url, tenant_name) |
| 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 | |
| 82 | def delete_security_group(self, security_group_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 83 | """Deletes the provided Security Group.""" |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 84 | return self.delete('os-security-groups/%s' % |
| 85 | str(security_group_id), self.headers) |
| 86 | |
| 87 | def create_security_group_rule(self, parent_group_id, ip_proto, from_port, |
| 88 | to_port, **kwargs): |
| 89 | """ |
| 90 | Creating a new security group rules. |
| 91 | parent_group_id :ID of Security group |
| 92 | ip_protocol : ip_proto (icmp, tcp, udp). |
| 93 | from_port: Port at start of range. |
| 94 | to_port : Port at end of range. |
| 95 | Following optional keyword arguments are accepted: |
| 96 | cidr : CIDR for address range. |
| 97 | group_id : ID of the Source group |
| 98 | """ |
| 99 | group_rule = Element("security_group_rule") |
| 100 | parent_group = Element("parent_group_id") |
| 101 | parent_group.append(Text(content=parent_group_id)) |
| 102 | ip_protocol = Element("ip_protocol") |
| 103 | ip_protocol.append(Text(content=ip_proto)) |
| 104 | from_port_num = Element("from_port") |
| 105 | from_port_num.append(Text(content=str(from_port))) |
| 106 | to_port_num = Element("to_port") |
| 107 | to_port_num.append(Text(content=str(to_port))) |
| 108 | |
| 109 | cidr = kwargs.get('cidr') |
| 110 | if cidr is not None: |
| 111 | cidr_num = Element("cidr") |
| 112 | cidr_num.append(Text(content=cidr)) |
| 113 | |
| 114 | group_id = kwargs.get('group_id') |
| 115 | if group_id is not None: |
| 116 | group_id_num = Element("group_id") |
| 117 | group_id_num.append(Text(content=group_id)) |
| 118 | |
| 119 | group_rule.append(parent_group) |
| 120 | group_rule.append(ip_protocol) |
| 121 | group_rule.append(from_port_num) |
| 122 | group_rule.append(to_port_num) |
| 123 | |
| 124 | url = 'os-security-group-rules' |
| 125 | resp, body = self.post(url, str(Document(group_rule)), self.headers) |
| 126 | body = self._parse_body(etree.fromstring(body)) |
| 127 | return resp, body |
| 128 | |
| 129 | def delete_security_group_rule(self, group_rule_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 130 | """Deletes the provided Security Group rule.""" |
Vincent Hou | ead03dc | 2012-08-24 21:35:11 +0800 | [diff] [blame] | 131 | return self.delete('os-security-group-rules/%s' % |
| 132 | str(group_rule_id), self.headers) |
Leo Toyoda | ce581f6 | 2013-03-07 16:16:06 +0900 | [diff] [blame] | 133 | |
| 134 | def list_security_group_rules(self, security_group_id): |
| 135 | """List all rules for a security group.""" |
| 136 | url = "os-security-groups" |
| 137 | resp, body = self.get(url, self.headers) |
| 138 | body = etree.fromstring(body) |
| 139 | secgroups = body.getchildren() |
| 140 | for secgroup in secgroups: |
| 141 | if secgroup.get('id') == security_group_id: |
| 142 | node = secgroup.find('{%s}rules' % XMLNS_11) |
| 143 | rules = [xml_to_json(x) for x in node.getchildren()] |
| 144 | return resp, rules |
| 145 | raise exceptions.NotFound('No such Security Group') |