blob: 0ef8e22005d07a366dce28e25a69b2c348cbae05 [file] [log] [blame]
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +09001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2013 NEC Corporation.
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
19
20from tempest.common.rest_client import RestClientXML
21from tempest import exceptions
22from tempest.services.compute.xml.common import Document
23from tempest.services.compute.xml.common import Element
24from tempest.services.compute.xml.common import xml_to_json
25
26
27class AggregatesClientXML(RestClientXML):
28
29 def __init__(self, config, username, password, auth_url, tenant_name=None):
30 super(AggregatesClientXML, self).__init__(config, username, password,
31 auth_url, tenant_name)
32 self.service = self.config.compute.catalog_type
33
34 def _format_aggregate(self, g):
35 agg = xml_to_json(g)
36 aggregate = {}
37 for key, value in agg.items():
38 if key == 'hosts':
39 aggregate['hosts'] = []
40 for k, v in value.items():
41 aggregate['hosts'].append(v)
42 elif key == 'availability_zone':
43 aggregate[key] = None if value == 'None' else value
44 else:
45 aggregate[key] = value
46 return aggregate
47
48 def _parse_array(self, node):
49 return [self._format_aggregate(x) for x in node]
50
51 def list_aggregates(self):
52 """Get aggregate list."""
53 resp, body = self.get("os-aggregates", self.headers)
54 aggregates = self._parse_array(etree.fromstring(body))
55 return resp, aggregates
56
57 def get_aggregate(self, aggregate_id):
58 """Get details of the given aggregate."""
59 resp, body = self.get("os-aggregates/%s" % str(aggregate_id),
60 self.headers)
61 aggregate = self._format_aggregate(etree.fromstring(body))
62 return resp, aggregate
63
64 def create_aggregate(self, name, availability_zone=None):
65 """Creates a new aggregate."""
66 post_body = Element("aggregate",
67 name=name,
68 availability_zone=availability_zone)
69 resp, body = self.post('os-aggregates',
70 str(Document(post_body)),
71 self.headers)
72 aggregate = self._format_aggregate(etree.fromstring(body))
73 return resp, aggregate
74
75 def delete_aggregate(self, aggregate_id):
76 """Deletes the given aggregate."""
77 return self.delete("os-aggregates/%s" % str(aggregate_id),
78 self.headers)
79
80 def is_resource_deleted(self, id):
81 try:
82 self.get_aggregate(id)
83 except exceptions.NotFound:
84 return True
85 return False
86
87 def add_host(self, aggregate_id, host):
88 """Adds a host to the given aggregate."""
89 post_body = Element("add_host", host=host)
90 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
91 str(Document(post_body)),
92 self.headers)
93 aggregate = self._format_aggregate(etree.fromstring(body))
94 return resp, aggregate
95
96 def remove_host(self, aggregate_id, host):
97 """Removes a host from the given aggregate."""
98 post_body = Element("remove_host", host=host)
99 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
100 str(Document(post_body)),
101 self.headers)
102 aggregate = self._format_aggregate(etree.fromstring(body))
103 return resp, aggregate