blob: ba08f586dcdba3391f305f3bdedcab412d39b85a [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
18from tempest.common.rest_client import RestClientXML
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
29class AggregatesClientXML(RestClientXML):
30
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(AggregatesClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000033 self.service = CONF.compute.catalog_type
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090034
35 def _format_aggregate(self, g):
36 agg = xml_to_json(g)
37 aggregate = {}
38 for key, value in agg.items():
39 if key == 'hosts':
40 aggregate['hosts'] = []
41 for k, v in value.items():
42 aggregate['hosts'].append(v)
43 elif key == 'availability_zone':
44 aggregate[key] = None if value == 'None' else value
45 else:
46 aggregate[key] = value
47 return aggregate
48
49 def _parse_array(self, node):
50 return [self._format_aggregate(x) for x in node]
51
52 def list_aggregates(self):
53 """Get aggregate list."""
54 resp, body = self.get("os-aggregates", self.headers)
55 aggregates = self._parse_array(etree.fromstring(body))
56 return resp, aggregates
57
58 def get_aggregate(self, aggregate_id):
59 """Get details of the given aggregate."""
60 resp, body = self.get("os-aggregates/%s" % str(aggregate_id),
61 self.headers)
62 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',
71 str(Document(post_body)),
72 self.headers)
73 aggregate = self._format_aggregate(etree.fromstring(body))
74 return resp, aggregate
75
Zhu Zhu7b5f6292013-09-22 15:47:54 +080076 def update_aggregate(self, aggregate_id, name, availability_zone=None):
77 """Update a aggregate."""
78 put_body = Element("aggregate",
79 name=name,
80 availability_zone=availability_zone)
81 resp, body = self.put('os-aggregates/%s' % str(aggregate_id),
82 str(Document(put_body)),
83 self.headers)
84 aggregate = self._format_aggregate(etree.fromstring(body))
85 return resp, aggregate
86
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090087 def delete_aggregate(self, aggregate_id):
88 """Deletes the given aggregate."""
89 return self.delete("os-aggregates/%s" % str(aggregate_id),
90 self.headers)
91
92 def is_resource_deleted(self, id):
93 try:
94 self.get_aggregate(id)
95 except exceptions.NotFound:
96 return True
97 return False
98
99 def add_host(self, aggregate_id, host):
100 """Adds a host to the given aggregate."""
101 post_body = Element("add_host", host=host)
102 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
103 str(Document(post_body)),
104 self.headers)
105 aggregate = self._format_aggregate(etree.fromstring(body))
106 return resp, aggregate
107
108 def remove_host(self, aggregate_id, host):
109 """Removes a host from the given aggregate."""
110 post_body = Element("remove_host", host=host)
111 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
112 str(Document(post_body)),
113 self.headers)
114 aggregate = self._format_aggregate(etree.fromstring(body))
115 return resp, aggregate
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800116
117 def set_metadata(self, aggregate_id, meta):
118 """Replaces the aggregate's existing metadata with new metadata."""
119 post_body = Element("set_metadata")
120 metadata = Element("metadata")
121 post_body.append(metadata)
122 for k, v in meta.items():
123 meta = Element(k)
124 meta.append(Text(v))
125 metadata.append(meta)
126 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
127 str(Document(post_body)),
128 self.headers)
129 aggregate = self._format_aggregate(etree.fromstring(body))
130 return resp, aggregate