blob: 4fccc29718a39e4fd37f33742a050ceb65c40699 [file] [log] [blame]
Vincent Houead03dc2012-08-24 21:35:11 +08001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04003# Copyright 2012 IBM Corp.
Vincent Houead03dc2012-08-24 21:35:11 +08004# 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
18from lxml import etree
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050019import urllib
Vincent Houead03dc2012-08-24 21:35:11 +080020
21from tempest.common.rest_client import RestClientXML
Attila Fazekasaea56302013-03-26 23:06:44 +010022from tempest import exceptions
dwallecke62b9f02012-10-10 23:34:42 -050023from tempest.services.compute.xml.common import Document
24from tempest.services.compute.xml.common import Element
25from tempest.services.compute.xml.common import Text
26from tempest.services.compute.xml.common import xml_to_json
Leo Toyodace581f62013-03-07 16:16:06 +090027from tempest.services.compute.xml.common import XMLNS_11
Vincent Houead03dc2012-08-24 21:35:11 +080028
29
30class 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 Daguef237ccb2013-01-04 15:19:14 -050049 """List all security groups for a user."""
Vincent Houead03dc2012-08-24 21:35:11 +080050
51 url = 'os-security-groups'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050052 if params:
53 url += '?%s' % urllib.urlencode(params)
Vincent Houead03dc2012-08-24 21:35:11 +080054
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 Daguef237ccb2013-01-04 15:19:14 -050060 """Get the details of a Security Group."""
Vincent Houead03dc2012-08-24 21:35:11 +080061 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 Daguef237ccb2013-01-04 15:19:14 -050083 """Deletes the provided Security Group."""
Vincent Houead03dc2012-08-24 21:35:11 +080084 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 Daguef237ccb2013-01-04 15:19:14 -0500130 """Deletes the provided Security Group rule."""
Vincent Houead03dc2012-08-24 21:35:11 +0800131 return self.delete('os-security-group-rules/%s' %
132 str(group_rule_id), self.headers)
Leo Toyodace581f62013-03-07 16:16:06 +0900133
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')