blob: d53e8da07f279af4b8c2ab7a67e8b3e54c75e520 [file] [log] [blame]
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04001# Copyright 2012 IBM Corp.
Vincent Houead03dc2012-08-24 21:35:11 +08002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16from lxml import etree
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050017import urllib
Vincent Houead03dc2012-08-24 21:35:11 +080018
vponomaryov960eeb42014-02-22 18:25:25 +020019from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
Attila Fazekasaea56302013-03-26 23:06:44 +010021from tempest import exceptions
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
Matthew Treinish684d8992014-01-30 16:27:40 +000028CONF = config.CONF
29
Vincent Houead03dc2012-08-24 21:35:11 +080030
vponomaryov960eeb42014-02-22 18:25:25 +020031class SecurityGroupsClientXML(rest_client.RestClient):
32 TYPE = "xml"
Vincent Houead03dc2012-08-24 21:35:11 +080033
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000034 def __init__(self, auth_provider):
35 super(SecurityGroupsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000036 self.service = CONF.compute.catalog_type
Vincent Houead03dc2012-08-24 21:35:11 +080037
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
vponomaryovf4c27f92014-02-18 10:56:42 +020055 resp, body = self.get(url)
Vincent Houead03dc2012-08-24 21:35:11 +080056 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)
vponomaryovf4c27f92014-02-18 10:56:42 +020062 resp, body = self.get(url)
Vincent Houead03dc2012-08-24 21:35:11 +080063 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',
vponomaryovf4c27f92014-02-18 10:56:42 +020077 str(Document(security_group)))
Vincent Houead03dc2012-08-24 21:35:11 +080078 body = self._parse_body(etree.fromstring(body))
79 return resp, body
80
huangtianhua248a7bf2013-10-21 11:23:39 +080081 def update_security_group(self, security_group_id, name=None,
82 description=None):
83 """
84 Update a security group.
85 security_group_id: a security_group to update
86 name: new name of security group
87 description: new description of security group
88 """
89 security_group = Element("security_group")
90 if name:
91 sg_name = Element("name")
92 sg_name.append(Text(content=name))
93 security_group.append(sg_name)
94 if description:
95 des = Element("description")
96 des.append(Text(content=description))
97 security_group.append(des)
98 resp, body = self.put('os-security-groups/%s' %
99 str(security_group_id),
vponomaryovf4c27f92014-02-18 10:56:42 +0200100 str(Document(security_group)))
huangtianhua248a7bf2013-10-21 11:23:39 +0800101 body = self._parse_body(etree.fromstring(body))
102 return resp, body
103
Vincent Houead03dc2012-08-24 21:35:11 +0800104 def delete_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500105 """Deletes the provided Security Group."""
vponomaryovf4c27f92014-02-18 10:56:42 +0200106 return self.delete('os-security-groups/%s' % str(security_group_id))
Vincent Houead03dc2012-08-24 21:35:11 +0800107
108 def create_security_group_rule(self, parent_group_id, ip_proto, from_port,
109 to_port, **kwargs):
110 """
111 Creating a new security group rules.
112 parent_group_id :ID of Security group
113 ip_protocol : ip_proto (icmp, tcp, udp).
114 from_port: Port at start of range.
115 to_port : Port at end of range.
116 Following optional keyword arguments are accepted:
117 cidr : CIDR for address range.
118 group_id : ID of the Source group
119 """
120 group_rule = Element("security_group_rule")
Vincent Houead03dc2012-08-24 21:35:11 +0800121
Matt Riedemann2ef090c2013-06-20 12:23:26 -0700122 elements = dict()
123 elements['cidr'] = kwargs.get('cidr')
124 elements['group_id'] = kwargs.get('group_id')
Andrea Frittoli59f00282013-06-19 18:08:44 +0100125 elements['parent_group_id'] = parent_group_id
126 elements['ip_protocol'] = ip_proto
127 elements['from_port'] = from_port
128 elements['to_port'] = to_port
Vincent Houead03dc2012-08-24 21:35:11 +0800129
Andrea Frittoli59f00282013-06-19 18:08:44 +0100130 for k, v in elements.items():
131 if v is not None:
132 element = Element(k)
133 element.append(Text(content=str(v)))
134 group_rule.append(element)
Vincent Houead03dc2012-08-24 21:35:11 +0800135
136 url = 'os-security-group-rules'
vponomaryovf4c27f92014-02-18 10:56:42 +0200137 resp, body = self.post(url, str(Document(group_rule)))
Vincent Houead03dc2012-08-24 21:35:11 +0800138 body = self._parse_body(etree.fromstring(body))
139 return resp, body
140
141 def delete_security_group_rule(self, group_rule_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500142 """Deletes the provided Security Group rule."""
Vincent Houead03dc2012-08-24 21:35:11 +0800143 return self.delete('os-security-group-rules/%s' %
vponomaryovf4c27f92014-02-18 10:56:42 +0200144 str(group_rule_id))
Leo Toyodace581f62013-03-07 16:16:06 +0900145
146 def list_security_group_rules(self, security_group_id):
147 """List all rules for a security group."""
148 url = "os-security-groups"
vponomaryovf4c27f92014-02-18 10:56:42 +0200149 resp, body = self.get(url)
Leo Toyodace581f62013-03-07 16:16:06 +0900150 body = etree.fromstring(body)
151 secgroups = body.getchildren()
152 for secgroup in secgroups:
153 if secgroup.get('id') == security_group_id:
154 node = secgroup.find('{%s}rules' % XMLNS_11)
155 rules = [xml_to_json(x) for x in node.getchildren()]
156 return resp, rules
157 raise exceptions.NotFound('No such Security Group')
Zhi Kun Liu02e7a7b2014-01-08 16:08:32 +0800158
159 def is_resource_deleted(self, id):
160 try:
161 self.get_security_group(id)
162 except exceptions.NotFound:
163 return True
164 return False