ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2013 NEC Corporation. |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | from lxml import etree |
| 19 | |
| 20 | from tempest.common.rest_client import RestClientXML |
| 21 | from tempest import exceptions |
| 22 | from tempest.services.compute.xml.common import Document |
| 23 | from tempest.services.compute.xml.common import Element |
| 24 | from tempest.services.compute.xml.common import Text |
| 25 | from tempest.services.compute.xml.common import xml_to_json |
| 26 | |
| 27 | |
ivan-zhu | 00fe64f | 2013-08-20 19:35:51 +0800 | [diff] [blame] | 28 | class AggregatesV3ClientXML(RestClientXML): |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 29 | |
| 30 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
ivan-zhu | 00fe64f | 2013-08-20 19:35:51 +0800 | [diff] [blame] | 31 | super(AggregatesV3ClientXML, self).__init__(config, username, password, |
| 32 | auth_url, tenant_name) |
| 33 | self.service = self.config.compute.catalog_v3_type |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 34 | |
| 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 | |
| 76 | 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 | |
| 87 | 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 |
| 116 | |
| 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 |