blob: 5b250ee68f1d52211a1e24739129b72830d97923 [file] [log] [blame]
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +09001# Copyright 2013 NEC Corporation.
2# 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
17
vponomaryov960eeb42014-02-22 18:25:25 +020018from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000019from tempest import config
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090020from tempest import exceptions
21from tempest.services.compute.xml.common import Document
22from tempest.services.compute.xml.common import Element
ivan-zhu35e1f8e2013-10-18 15:51:16 +080023from tempest.services.compute.xml.common import Text
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090024from tempest.services.compute.xml.common import xml_to_json
25
Matthew Treinish684d8992014-01-30 16:27:40 +000026CONF = config.CONF
27
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090028
vponomaryov960eeb42014-02-22 18:25:25 +020029class AggregatesClientXML(rest_client.RestClient):
30 TYPE = "xml"
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090031
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000032 def __init__(self, auth_provider):
33 super(AggregatesClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000034 self.service = CONF.compute.catalog_type
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090035
36 def _format_aggregate(self, g):
37 agg = xml_to_json(g)
38 aggregate = {}
39 for key, value in agg.items():
40 if key == 'hosts':
41 aggregate['hosts'] = []
42 for k, v in value.items():
43 aggregate['hosts'].append(v)
44 elif key == 'availability_zone':
45 aggregate[key] = None if value == 'None' else value
46 else:
47 aggregate[key] = value
48 return aggregate
49
50 def _parse_array(self, node):
51 return [self._format_aggregate(x) for x in node]
52
53 def list_aggregates(self):
54 """Get aggregate list."""
vponomaryovf4c27f92014-02-18 10:56:42 +020055 resp, body = self.get("os-aggregates")
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090056 aggregates = self._parse_array(etree.fromstring(body))
57 return resp, aggregates
58
59 def get_aggregate(self, aggregate_id):
60 """Get details of the given aggregate."""
vponomaryovf4c27f92014-02-18 10:56:42 +020061 resp, body = self.get("os-aggregates/%s" % str(aggregate_id))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090062 aggregate = self._format_aggregate(etree.fromstring(body))
63 return resp, aggregate
64
65 def create_aggregate(self, name, availability_zone=None):
66 """Creates a new aggregate."""
67 post_body = Element("aggregate",
68 name=name,
69 availability_zone=availability_zone)
70 resp, body = self.post('os-aggregates',
vponomaryovf4c27f92014-02-18 10:56:42 +020071 str(Document(post_body)))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090072 aggregate = self._format_aggregate(etree.fromstring(body))
73 return resp, aggregate
74
Zhu Zhu7b5f6292013-09-22 15:47:54 +080075 def update_aggregate(self, aggregate_id, name, availability_zone=None):
76 """Update a aggregate."""
77 put_body = Element("aggregate",
78 name=name,
79 availability_zone=availability_zone)
80 resp, body = self.put('os-aggregates/%s' % str(aggregate_id),
vponomaryovf4c27f92014-02-18 10:56:42 +020081 str(Document(put_body)))
Zhu Zhu7b5f6292013-09-22 15:47:54 +080082 aggregate = self._format_aggregate(etree.fromstring(body))
83 return resp, aggregate
84
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090085 def delete_aggregate(self, aggregate_id):
86 """Deletes the given aggregate."""
vponomaryovf4c27f92014-02-18 10:56:42 +020087 return self.delete("os-aggregates/%s" % str(aggregate_id))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090088
89 def is_resource_deleted(self, id):
90 try:
91 self.get_aggregate(id)
92 except exceptions.NotFound:
93 return True
94 return False
95
96 def add_host(self, aggregate_id, host):
97 """Adds a host to the given aggregate."""
98 post_body = Element("add_host", host=host)
99 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200100 str(Document(post_body)))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900101 aggregate = self._format_aggregate(etree.fromstring(body))
102 return resp, aggregate
103
104 def remove_host(self, aggregate_id, host):
105 """Removes a host from the given aggregate."""
106 post_body = Element("remove_host", host=host)
107 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200108 str(Document(post_body)))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900109 aggregate = self._format_aggregate(etree.fromstring(body))
110 return resp, aggregate
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800111
112 def set_metadata(self, aggregate_id, meta):
113 """Replaces the aggregate's existing metadata with new metadata."""
114 post_body = Element("set_metadata")
115 metadata = Element("metadata")
116 post_body.append(metadata)
117 for k, v in meta.items():
118 meta = Element(k)
119 meta.append(Text(v))
120 metadata.append(meta)
121 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200122 str(Document(post_body)))
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800123 aggregate = self._format_aggregate(etree.fromstring(body))
124 return resp, aggregate