blob: ddef18be5bba99813e19354c134f6ae16462b7d5 [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
Matthew Treinish684d8992014-01-30 16:27:40 +000031 def __init__(self, username, password, auth_url, tenant_name=None):
32 super(AggregatesClientXML, self).__init__(username, password,
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090033 auth_url, tenant_name)
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."""
55 resp, body = self.get("os-aggregates", self.headers)
56 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."""
61 resp, body = self.get("os-aggregates/%s" % str(aggregate_id),
62 self.headers)
63 aggregate = self._format_aggregate(etree.fromstring(body))
64 return resp, aggregate
65
66 def create_aggregate(self, name, availability_zone=None):
67 """Creates a new aggregate."""
68 post_body = Element("aggregate",
69 name=name,
70 availability_zone=availability_zone)
71 resp, body = self.post('os-aggregates',
72 str(Document(post_body)),
73 self.headers)
74 aggregate = self._format_aggregate(etree.fromstring(body))
75 return resp, aggregate
76
Zhu Zhu7b5f6292013-09-22 15:47:54 +080077 def update_aggregate(self, aggregate_id, name, availability_zone=None):
78 """Update a aggregate."""
79 put_body = Element("aggregate",
80 name=name,
81 availability_zone=availability_zone)
82 resp, body = self.put('os-aggregates/%s' % str(aggregate_id),
83 str(Document(put_body)),
84 self.headers)
85 aggregate = self._format_aggregate(etree.fromstring(body))
86 return resp, aggregate
87
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090088 def delete_aggregate(self, aggregate_id):
89 """Deletes the given aggregate."""
90 return self.delete("os-aggregates/%s" % str(aggregate_id),
91 self.headers)
92
93 def is_resource_deleted(self, id):
94 try:
95 self.get_aggregate(id)
96 except exceptions.NotFound:
97 return True
98 return False
99
100 def add_host(self, aggregate_id, host):
101 """Adds a host to the given aggregate."""
102 post_body = Element("add_host", host=host)
103 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
104 str(Document(post_body)),
105 self.headers)
106 aggregate = self._format_aggregate(etree.fromstring(body))
107 return resp, aggregate
108
109 def remove_host(self, aggregate_id, host):
110 """Removes a host from the given aggregate."""
111 post_body = Element("remove_host", host=host)
112 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
113 str(Document(post_body)),
114 self.headers)
115 aggregate = self._format_aggregate(etree.fromstring(body))
116 return resp, aggregate
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800117
118 def set_metadata(self, aggregate_id, meta):
119 """Replaces the aggregate's existing metadata with new metadata."""
120 post_body = Element("set_metadata")
121 metadata = Element("metadata")
122 post_body.append(metadata)
123 for k, v in meta.items():
124 meta = Element(k)
125 meta.append(Text(v))
126 metadata.append(meta)
127 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
128 str(Document(post_body)),
129 self.headers)
130 aggregate = self._format_aggregate(etree.fromstring(body))
131 return resp, aggregate