blob: 7db60a11b66e5bbd9fd943d191960cf3fe1698d4 [file] [log] [blame]
Vincent Houead03dc2012-08-24 21:35:11 +08001# 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
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
dwallecke62b9f02012-10-10 23:34:42 -050022from tempest.services.compute.xml.common import Document
23from tempest.services.compute.xml.common import Element
24from tempest.services.compute.xml.common import Text
25from tempest.services.compute.xml.common import xml_to_json
Leo Toyodace581f62013-03-07 16:16:06 +090026from tempest.services.compute.xml.common import XMLNS_11
Vincent Houead03dc2012-08-24 21:35:11 +080027
28
29class 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 Daguef237ccb2013-01-04 15:19:14 -050048 """List all security groups for a user."""
Vincent Houead03dc2012-08-24 21:35:11 +080049
50 url = 'os-security-groups'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050051 if params:
52 url += '?%s' % urllib.urlencode(params)
Vincent Houead03dc2012-08-24 21:35:11 +080053
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 Daguef237ccb2013-01-04 15:19:14 -050059 """Get the details of a Security Group."""
Vincent Houead03dc2012-08-24 21:35:11 +080060 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 Daguef237ccb2013-01-04 15:19:14 -050082 """Deletes the provided Security Group."""
Vincent Houead03dc2012-08-24 21:35:11 +080083 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 Daguef237ccb2013-01-04 15:19:14 -0500129 """Deletes the provided Security Group rule."""
Vincent Houead03dc2012-08-24 21:35:11 +0800130 return self.delete('os-security-group-rules/%s' %
131 str(group_rule_id), self.headers)
Leo Toyodace581f62013-03-07 16:16:06 +0900132
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')