blob: 83072be40db486068213bf987f1335965bb4d559 [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
19from tempest.common.rest_client import RestClientXML
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
31class SecurityGroupsClientXML(RestClientXML):
32
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000033 def __init__(self, auth_provider):
34 super(SecurityGroupsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000035 self.service = CONF.compute.catalog_type
Vincent Houead03dc2012-08-24 21:35:11 +080036
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
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),
100 str(Document(security_group)),
101 self.headers)
102 body = self._parse_body(etree.fromstring(body))
103 return resp, body
104
Vincent Houead03dc2012-08-24 21:35:11 +0800105 def delete_security_group(self, security_group_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500106 """Deletes the provided Security Group."""
Vincent Houead03dc2012-08-24 21:35:11 +0800107 return self.delete('os-security-groups/%s' %
108 str(security_group_id), self.headers)
109
110 def create_security_group_rule(self, parent_group_id, ip_proto, from_port,
111 to_port, **kwargs):
112 """
113 Creating a new security group rules.
114 parent_group_id :ID of Security group
115 ip_protocol : ip_proto (icmp, tcp, udp).
116 from_port: Port at start of range.
117 to_port : Port at end of range.
118 Following optional keyword arguments are accepted:
119 cidr : CIDR for address range.
120 group_id : ID of the Source group
121 """
122 group_rule = Element("security_group_rule")
Vincent Houead03dc2012-08-24 21:35:11 +0800123
Matt Riedemann2ef090c2013-06-20 12:23:26 -0700124 elements = dict()
125 elements['cidr'] = kwargs.get('cidr')
126 elements['group_id'] = kwargs.get('group_id')
Andrea Frittoli59f00282013-06-19 18:08:44 +0100127 elements['parent_group_id'] = parent_group_id
128 elements['ip_protocol'] = ip_proto
129 elements['from_port'] = from_port
130 elements['to_port'] = to_port
Vincent Houead03dc2012-08-24 21:35:11 +0800131
Andrea Frittoli59f00282013-06-19 18:08:44 +0100132 for k, v in elements.items():
133 if v is not None:
134 element = Element(k)
135 element.append(Text(content=str(v)))
136 group_rule.append(element)
Vincent Houead03dc2012-08-24 21:35:11 +0800137
138 url = 'os-security-group-rules'
139 resp, body = self.post(url, str(Document(group_rule)), self.headers)
140 body = self._parse_body(etree.fromstring(body))
141 return resp, body
142
143 def delete_security_group_rule(self, group_rule_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500144 """Deletes the provided Security Group rule."""
Vincent Houead03dc2012-08-24 21:35:11 +0800145 return self.delete('os-security-group-rules/%s' %
146 str(group_rule_id), self.headers)
Leo Toyodace581f62013-03-07 16:16:06 +0900147
148 def list_security_group_rules(self, security_group_id):
149 """List all rules for a security group."""
150 url = "os-security-groups"
151 resp, body = self.get(url, self.headers)
152 body = etree.fromstring(body)
153 secgroups = body.getchildren()
154 for secgroup in secgroups:
155 if secgroup.get('id') == security_group_id:
156 node = secgroup.find('{%s}rules' % XMLNS_11)
157 rules = [xml_to_json(x) for x in node.getchildren()]
158 return resp, rules
159 raise exceptions.NotFound('No such Security Group')