Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 1 | # 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 | |
| 16 | from lxml import etree |
| 17 | |
| 18 | from tempest.common.rest_client import RestClientXML |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 19 | from tempest import config |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 20 | from tempest import exceptions |
| 21 | from tempest.services.compute.xml.common import Document |
| 22 | from tempest.services.compute.xml.common import Element |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 23 | from tempest.services.compute.xml.common import Text |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 24 | from tempest.services.compute.xml.common import xml_to_json |
| 25 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 26 | CONF = config.CONF |
| 27 | |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 28 | |
| 29 | class AggregatesClientXML(RestClientXML): |
| 30 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 31 | def __init__(self, username, password, auth_url, tenant_name=None): |
| 32 | super(AggregatesClientXML, self).__init__(username, password, |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 33 | auth_url, tenant_name) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 34 | self.service = CONF.compute.catalog_type |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 35 | |
| 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 Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 77 | 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 Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 88 | 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-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 117 | |
| 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 |