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