blob: d619aa792250d1c83d58b43fc20b355df084c14a [file] [log] [blame]
ivan-zhu8577cb12013-08-20 14:38:36 +08001# 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
19from tempest import exceptions
20from tempest.services.compute.xml.common import Document
21from tempest.services.compute.xml.common import Element
22from tempest.services.compute.xml.common import Text
23from tempest.services.compute.xml.common import xml_to_json
24
25
ivan-zhu00fe64f2013-08-20 19:35:51 +080026class AggregatesV3ClientXML(RestClientXML):
ivan-zhu8577cb12013-08-20 14:38:36 +080027
28 def __init__(self, config, username, password, auth_url, tenant_name=None):
ivan-zhu00fe64f2013-08-20 19:35:51 +080029 super(AggregatesV3ClientXML, self).__init__(config, username, password,
30 auth_url, tenant_name)
31 self.service = self.config.compute.catalog_v3_type
ivan-zhu8577cb12013-08-20 14:38:36 +080032
33 def _format_aggregate(self, g):
34 agg = xml_to_json(g)
35 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."""
52 resp, body = self.get("os-aggregates", self.headers)
53 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."""
58 resp, body = self.get("os-aggregates/%s" % str(aggregate_id),
59 self.headers)
60 aggregate = self._format_aggregate(etree.fromstring(body))
61 return resp, aggregate
62
63 def create_aggregate(self, name, availability_zone=None):
64 """Creates a new aggregate."""
65 post_body = Element("aggregate",
66 name=name,
67 availability_zone=availability_zone)
68 resp, body = self.post('os-aggregates',
69 str(Document(post_body)),
70 self.headers)
71 aggregate = self._format_aggregate(etree.fromstring(body))
72 return resp, aggregate
73
74 def update_aggregate(self, aggregate_id, name, availability_zone=None):
75 """Update a aggregate."""
76 put_body = Element("aggregate",
77 name=name,
78 availability_zone=availability_zone)
79 resp, body = self.put('os-aggregates/%s' % str(aggregate_id),
80 str(Document(put_body)),
81 self.headers)
82 aggregate = self._format_aggregate(etree.fromstring(body))
83 return resp, aggregate
84
85 def delete_aggregate(self, aggregate_id):
86 """Deletes the given aggregate."""
87 return self.delete("os-aggregates/%s" % str(aggregate_id),
88 self.headers)
89
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."""
99 post_body = Element("add_host", host=host)
100 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
101 str(Document(post_body)),
102 self.headers)
103 aggregate = self._format_aggregate(etree.fromstring(body))
104 return resp, aggregate
105
106 def remove_host(self, aggregate_id, host):
107 """Removes a host from the given aggregate."""
108 post_body = Element("remove_host", host=host)
109 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
110 str(Document(post_body)),
111 self.headers)
112 aggregate = self._format_aggregate(etree.fromstring(body))
113 return resp, aggregate
114
115 def set_metadata(self, aggregate_id, meta):
116 """Replaces the aggregate's existing metadata with new metadata."""
117 post_body = Element("set_metadata")
118 metadata = Element("metadata")
119 post_body.append(metadata)
120 for k, v in meta.items():
121 meta = Element(k)
122 meta.append(Text(v))
123 metadata.append(meta)
124 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
125 str(Document(post_body)),
126 self.headers)
127 aggregate = self._format_aggregate(etree.fromstring(body))
128 return resp, aggregate