blob: aebeb4d91b5f7d6b61b7add3789f8f80bfd0993b [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__(
Sean Dague14c68182013-04-14 15:34:30 -040034 config, username, password,
35 auth_url, tenant_name)
Vincent Houead03dc2012-08-24 21:35:11 +080036 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
huangtianhua248a7bf2013-10-21 11:23:39 +080082 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 Houead03dc2012-08-24 21:35:11 +0800106 def delete_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500107 """Deletes the provided Security Group."""
Vincent Houead03dc2012-08-24 21:35:11 +0800108 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 Houead03dc2012-08-24 21:35:11 +0800124
Matt Riedemann2ef090c2013-06-20 12:23:26 -0700125 elements = dict()
126 elements['cidr'] = kwargs.get('cidr')
127 elements['group_id'] = kwargs.get('group_id')
Andrea Frittoli59f00282013-06-19 18:08:44 +0100128 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 Houead03dc2012-08-24 21:35:11 +0800132
Andrea Frittoli59f00282013-06-19 18:08:44 +0100133 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 Houead03dc2012-08-24 21:35:11 +0800138
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 Daguef237ccb2013-01-04 15:19:14 -0500145 """Deletes the provided Security Group rule."""
Vincent Houead03dc2012-08-24 21:35:11 +0800146 return self.delete('os-security-group-rules/%s' %
147 str(group_rule_id), self.headers)
Leo Toyodace581f62013-03-07 16:16:06 +0900148
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')