blob: 9c2d4aa9ebb999823146731e27aa665cc19028b4 [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 Treinish28f164c2014-03-04 18:55:06 +000019from tempest.common import xml_utils
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090021from tempest import exceptions
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090022
Matthew Treinish684d8992014-01-30 16:27:40 +000023CONF = config.CONF
24
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090025
vponomaryov960eeb42014-02-22 18:25:25 +020026class AggregatesClientXML(rest_client.RestClient):
27 TYPE = "xml"
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090028
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000029 def __init__(self, auth_provider):
30 super(AggregatesClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000031 self.service = CONF.compute.catalog_type
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090032
33 def _format_aggregate(self, g):
Matthew Treinish28f164c2014-03-04 18:55:06 +000034 agg = xml_utils.xml_to_json(g)
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090035 aggregate = {}
36 for key, value in agg.items():
37 if key == 'hosts':
38 aggregate['hosts'] = []
39 for k, v in value.items():
40 aggregate['hosts'].append(v)
41 elif key == 'availability_zone':
42 aggregate[key] = None if value == 'None' else value
43 else:
44 aggregate[key] = value
45 return aggregate
46
47 def _parse_array(self, node):
48 return [self._format_aggregate(x) for x in node]
49
50 def list_aggregates(self):
51 """Get aggregate list."""
vponomaryovf4c27f92014-02-18 10:56:42 +020052 resp, body = self.get("os-aggregates")
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090053 aggregates = self._parse_array(etree.fromstring(body))
54 return resp, aggregates
55
56 def get_aggregate(self, aggregate_id):
57 """Get details of the given aggregate."""
vponomaryovf4c27f92014-02-18 10:56:42 +020058 resp, body = self.get("os-aggregates/%s" % str(aggregate_id))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090059 aggregate = self._format_aggregate(etree.fromstring(body))
60 return resp, aggregate
61
62 def create_aggregate(self, name, availability_zone=None):
63 """Creates a new aggregate."""
Ken'ichi Ohmichic974d032014-04-15 13:52:04 +090064 if availability_zone is not None:
65 post_body = xml_utils.Element("aggregate", name=name,
66 availability_zone=availability_zone)
67 else:
68 post_body = xml_utils.Element("aggregate", name=name)
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090069 resp, body = self.post('os-aggregates',
Matthew Treinish28f164c2014-03-04 18:55:06 +000070 str(xml_utils.Document(post_body)))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090071 aggregate = self._format_aggregate(etree.fromstring(body))
72 return resp, aggregate
73
Zhu Zhu7b5f6292013-09-22 15:47:54 +080074 def update_aggregate(self, aggregate_id, name, availability_zone=None):
75 """Update a aggregate."""
Ken'ichi Ohmichic974d032014-04-15 13:52:04 +090076 if availability_zone is not None:
77 put_body = xml_utils.Element("aggregate", name=name,
78 availability_zone=availability_zone)
79 else:
80 put_body = xml_utils.Element("aggregate", name=name)
Zhu Zhu7b5f6292013-09-22 15:47:54 +080081 resp, body = self.put('os-aggregates/%s' % str(aggregate_id),
Matthew Treinish28f164c2014-03-04 18:55:06 +000082 str(xml_utils.Document(put_body)))
Zhu Zhu7b5f6292013-09-22 15:47:54 +080083 aggregate = self._format_aggregate(etree.fromstring(body))
84 return resp, aggregate
85
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090086 def delete_aggregate(self, aggregate_id):
87 """Deletes the given aggregate."""
vponomaryovf4c27f92014-02-18 10:56:42 +020088 return self.delete("os-aggregates/%s" % str(aggregate_id))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090089
90 def is_resource_deleted(self, id):
91 try:
92 self.get_aggregate(id)
93 except exceptions.NotFound:
94 return True
95 return False
96
97 def add_host(self, aggregate_id, host):
98 """Adds a host to the given aggregate."""
Matthew Treinish28f164c2014-03-04 18:55:06 +000099 post_body = xml_utils.Element("add_host", host=host)
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900100 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
Matthew Treinish28f164c2014-03-04 18:55:06 +0000101 str(xml_utils.Document(post_body)))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900102 aggregate = self._format_aggregate(etree.fromstring(body))
103 return resp, aggregate
104
105 def remove_host(self, aggregate_id, host):
106 """Removes a host from the given aggregate."""
Matthew Treinish28f164c2014-03-04 18:55:06 +0000107 post_body = xml_utils.Element("remove_host", host=host)
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900108 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
Matthew Treinish28f164c2014-03-04 18:55:06 +0000109 str(xml_utils.Document(post_body)))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900110 aggregate = self._format_aggregate(etree.fromstring(body))
111 return resp, aggregate
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800112
113 def set_metadata(self, aggregate_id, meta):
114 """Replaces the aggregate's existing metadata with new metadata."""
Matthew Treinish28f164c2014-03-04 18:55:06 +0000115 post_body = xml_utils.Element("set_metadata")
116 metadata = xml_utils.Element("metadata")
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800117 post_body.append(metadata)
118 for k, v in meta.items():
Matthew Treinish28f164c2014-03-04 18:55:06 +0000119 meta = xml_utils.Element(k)
120 meta.append(xml_utils.Text(v))
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800121 metadata.append(meta)
122 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
Matthew Treinish28f164c2014-03-04 18:55:06 +0000123 str(xml_utils.Document(post_body)))
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800124 aggregate = self._format_aggregate(etree.fromstring(body))
125 return resp, aggregate