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 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 18 | from tempest.common import rest_client |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 19 | from tempest.common import xml_utils |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 21 | from tempest import exceptions |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 22 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 25 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 26 | class AggregatesClientXML(rest_client.RestClient): |
| 27 | TYPE = "xml" |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 28 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 29 | def __init__(self, auth_provider): |
| 30 | super(AggregatesClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 31 | self.service = CONF.compute.catalog_type |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 32 | |
| 33 | def _format_aggregate(self, g): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 34 | agg = xml_utils.xml_to_json(g) |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 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.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 52 | resp, body = self.get("os-aggregates") |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 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.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 58 | resp, body = self.get("os-aggregates/%s" % str(aggregate_id)) |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 59 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 60 | return resp, aggregate |
| 61 | |
| 62 | def create_aggregate(self, name, availability_zone=None): |
| 63 | """Creates a new aggregate.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 64 | post_body = xml_utils.Element("aggregate", |
| 65 | name=name, |
| 66 | availability_zone=availability_zone) |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 67 | resp, body = self.post('os-aggregates', |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 68 | str(xml_utils.Document(post_body))) |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 69 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 70 | return resp, aggregate |
| 71 | |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 72 | def update_aggregate(self, aggregate_id, name, availability_zone=None): |
| 73 | """Update a aggregate.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 74 | put_body = xml_utils.Element("aggregate", |
| 75 | name=name, |
| 76 | availability_zone=availability_zone) |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 77 | resp, body = self.put('os-aggregates/%s' % str(aggregate_id), |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 78 | str(xml_utils.Document(put_body))) |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 79 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 80 | return resp, aggregate |
| 81 | |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 82 | def delete_aggregate(self, aggregate_id): |
| 83 | """Deletes the given aggregate.""" |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 84 | return self.delete("os-aggregates/%s" % str(aggregate_id)) |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 85 | |
| 86 | def is_resource_deleted(self, id): |
| 87 | try: |
| 88 | self.get_aggregate(id) |
| 89 | except exceptions.NotFound: |
| 90 | return True |
| 91 | return False |
| 92 | |
| 93 | def add_host(self, aggregate_id, host): |
| 94 | """Adds a host to the given aggregate.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 95 | post_body = xml_utils.Element("add_host", host=host) |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 96 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 97 | str(xml_utils.Document(post_body))) |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 98 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 99 | return resp, aggregate |
| 100 | |
| 101 | def remove_host(self, aggregate_id, host): |
| 102 | """Removes a host from the given aggregate.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 103 | post_body = xml_utils.Element("remove_host", host=host) |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 104 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 105 | str(xml_utils.Document(post_body))) |
Mitsuhiko Yamazaki | ae8fc53 | 2013-04-22 11:17:35 +0900 | [diff] [blame] | 106 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 107 | return resp, aggregate |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 108 | |
| 109 | def set_metadata(self, aggregate_id, meta): |
| 110 | """Replaces the aggregate's existing metadata with new metadata.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 111 | post_body = xml_utils.Element("set_metadata") |
| 112 | metadata = xml_utils.Element("metadata") |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 113 | post_body.append(metadata) |
| 114 | for k, v in meta.items(): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 115 | meta = xml_utils.Element(k) |
| 116 | meta.append(xml_utils.Text(v)) |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 117 | metadata.append(meta) |
| 118 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 119 | str(xml_utils.Document(post_body))) |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 120 | aggregate = self._format_aggregate(etree.fromstring(body)) |
| 121 | return resp, aggregate |