Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [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 xml_to_json |
| 25 | |
| 26 | |
| 27 | class AggregatesClientXML(RestClientXML): |
| 28 | |
| 29 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 30 | super(AggregatesClientXML, self).__init__(config, username, password, |
| 31 | auth_url, tenant_name) |
| 32 | self.service = self.config.compute.catalog_type |
| 33 | |
| 34 | def _format_aggregate(self, g): |
| 35 | agg = xml_to_json(g) |
| 36 | aggregate = {} |
| 37 | for key, value in agg.items(): |
| 38 | if key == 'hosts': |
| 39 | aggregate['hosts'] = [] |
| 40 | for k, v in value.items(): |
| 41 | aggregate['hosts'].append(v) |
| 42 | elif key == 'availability_zone': |
| 43 | aggregate[key] = None if value == 'None' else value |
| 44 | else: |
| 45 | aggregate[key] = value |
| 46 | return aggregate |
| 47 | |
| 48 | def _parse_array(self, node): |
| 49 | return [self._format_aggregate(x) for x in node] |
| 50 | |
| 51 | def list_aggregates(self): |
| 52 | """Get aggregate list.""" |
| 53 | resp, body = self.get("os-aggregates", self.headers) |
| 54 | aggregates = self._parse_array(etree.fromstring(body)) |
| 55 | return resp, aggregates |
| 56 | |
| 57 | def get_aggregate(self, aggregate_id): |
| 58 | """Get details of the given aggregate.""" |
| 59 | resp, body = self.get("os-aggregates/%s" % str(aggregate_id), |
| 60 | self.headers) |
| 61 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 62 | return resp, aggregate |
| 63 | |
| 64 | def create_aggregate(self, name, availability_zone=None): |
| 65 | """Creates a new aggregate.""" |
| 66 | post_body = Element("aggregate", |
| 67 | name=name, |
| 68 | availability_zone=availability_zone) |
| 69 | resp, body = self.post('os-aggregates', |
| 70 | str(Document(post_body)), |
| 71 | self.headers) |
| 72 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 73 | return resp, aggregate |
| 74 | |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame^] | 75 | def update_aggregate(self, aggregate_id, name, availability_zone=None): |
| 76 | """Update a aggregate.""" |
| 77 | put_body = Element("aggregate", |
| 78 | name=name, |
| 79 | availability_zone=availability_zone) |
| 80 | resp, body = self.put('os-aggregates/%s' % str(aggregate_id), |
| 81 | str(Document(put_body)), |
| 82 | self.headers) |
| 83 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 84 | return resp, aggregate |
| 85 | |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 86 | def delete_aggregate(self, aggregate_id): |
| 87 | """Deletes the given aggregate.""" |
| 88 | return self.delete("os-aggregates/%s" % str(aggregate_id), |
| 89 | self.headers) |
| 90 | |
| 91 | def is_resource_deleted(self, id): |
| 92 | try: |
| 93 | self.get_aggregate(id) |
| 94 | except exceptions.NotFound: |
| 95 | return True |
| 96 | return False |
| 97 | |
| 98 | def add_host(self, aggregate_id, host): |
| 99 | """Adds a host to the given aggregate.""" |
| 100 | post_body = Element("add_host", host=host) |
| 101 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
| 102 | str(Document(post_body)), |
| 103 | self.headers) |
| 104 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 105 | return resp, aggregate |
| 106 | |
| 107 | def remove_host(self, aggregate_id, host): |
| 108 | """Removes a host from the given aggregate.""" |
| 109 | post_body = Element("remove_host", host=host) |
| 110 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
| 111 | str(Document(post_body)), |
| 112 | self.headers) |
| 113 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 114 | return resp, aggregate |