blob: b5f7678039d5c649186bef16545176566d38f2c2 [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."""
Matthew Treinish28f164c2014-03-04 18:55:06 +000064 post_body = xml_utils.Element("aggregate",
65 name=name,
66 availability_zone=availability_zone)
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090067 resp, body = self.post('os-aggregates',
Matthew Treinish28f164c2014-03-04 18:55:06 +000068 str(xml_utils.Document(post_body)))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090069 aggregate = self._format_aggregate(etree.fromstring(body))
70 return resp, aggregate
71
Zhu Zhu7b5f6292013-09-22 15:47:54 +080072 def update_aggregate(self, aggregate_id, name, availability_zone=None):
73 """Update a aggregate."""
Matthew Treinish28f164c2014-03-04 18:55:06 +000074 put_body = xml_utils.Element("aggregate",
75 name=name,
76 availability_zone=availability_zone)
Zhu Zhu7b5f6292013-09-22 15:47:54 +080077 resp, body = self.put('os-aggregates/%s' % str(aggregate_id),
Matthew Treinish28f164c2014-03-04 18:55:06 +000078 str(xml_utils.Document(put_body)))
Zhu Zhu7b5f6292013-09-22 15:47:54 +080079 aggregate = self._format_aggregate(etree.fromstring(body))
80 return resp, aggregate
81
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090082 def delete_aggregate(self, aggregate_id):
83 """Deletes the given aggregate."""
vponomaryovf4c27f92014-02-18 10:56:42 +020084 return self.delete("os-aggregates/%s" % str(aggregate_id))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090085
86 def is_resource_deleted(self, id):
87 try:
88 self.get_aggregate(id)
89 except exceptions.NotFound:
90 return True
91 return False
92
93 def add_host(self, aggregate_id, host):
94 """Adds a host to the given aggregate."""
Matthew Treinish28f164c2014-03-04 18:55:06 +000095 post_body = xml_utils.Element("add_host", host=host)
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090096 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
Matthew Treinish28f164c2014-03-04 18:55:06 +000097 str(xml_utils.Document(post_body)))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090098 aggregate = self._format_aggregate(etree.fromstring(body))
99 return resp, aggregate
100
101 def remove_host(self, aggregate_id, host):
102 """Removes a host from the given aggregate."""
Matthew Treinish28f164c2014-03-04 18:55:06 +0000103 post_body = xml_utils.Element("remove_host", host=host)
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900104 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
Matthew Treinish28f164c2014-03-04 18:55:06 +0000105 str(xml_utils.Document(post_body)))
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900106 aggregate = self._format_aggregate(etree.fromstring(body))
107 return resp, aggregate
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800108
109 def set_metadata(self, aggregate_id, meta):
110 """Replaces the aggregate's existing metadata with new metadata."""
Matthew Treinish28f164c2014-03-04 18:55:06 +0000111 post_body = xml_utils.Element("set_metadata")
112 metadata = xml_utils.Element("metadata")
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800113 post_body.append(metadata)
114 for k, v in meta.items():
Matthew Treinish28f164c2014-03-04 18:55:06 +0000115 meta = xml_utils.Element(k)
116 meta.append(xml_utils.Text(v))
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800117 metadata.append(meta)
118 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
Matthew Treinish28f164c2014-03-04 18:55:06 +0000119 str(xml_utils.Document(post_body)))
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800120 aggregate = self._format_aggregate(etree.fromstring(body))
121 return resp, aggregate