ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [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 | import json |
| 17 | |
Haiwei Xu | 88173c8 | 2014-03-20 03:15:13 +0900 | [diff] [blame] | 18 | from tempest.api_schema.compute import aggregates as schema |
Ghanshyam | 0784162 | 2014-04-22 14:18:03 +0900 | [diff] [blame^] | 19 | from tempest.api_schema.compute.v3 import aggregates as v3_schema |
Haiwei Xu | 16ee2c8 | 2014-03-05 04:59:18 +0900 | [diff] [blame] | 20 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 21 | from tempest import config |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 22 | from tempest import exceptions |
| 23 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 24 | CONF = config.CONF |
| 25 | |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 26 | |
Haiwei Xu | 16ee2c8 | 2014-03-05 04:59:18 +0900 | [diff] [blame] | 27 | class AggregatesV3ClientJSON(rest_client.RestClient): |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 28 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 29 | def __init__(self, auth_provider): |
| 30 | super(AggregatesV3ClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 31 | self.service = CONF.compute.catalog_v3_type |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 32 | |
| 33 | def list_aggregates(self): |
| 34 | """Get aggregate list.""" |
| 35 | resp, body = self.get("os-aggregates") |
| 36 | body = json.loads(body) |
Haiwei Xu | 88173c8 | 2014-03-20 03:15:13 +0900 | [diff] [blame] | 37 | self.validate_response(schema.list_aggregates, resp, body) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 38 | return resp, body['aggregates'] |
| 39 | |
| 40 | def get_aggregate(self, aggregate_id): |
| 41 | """Get details of the given aggregate.""" |
| 42 | resp, body = self.get("os-aggregates/%s" % str(aggregate_id)) |
| 43 | body = json.loads(body) |
Haiwei Xu | 7e40d02 | 2014-03-25 22:42:13 +0900 | [diff] [blame] | 44 | self.validate_response(schema.get_aggregate, resp, body) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 45 | return resp, body['aggregate'] |
| 46 | |
Yuiko Takada | f93d248 | 2014-01-30 16:25:08 +0000 | [diff] [blame] | 47 | def create_aggregate(self, **kwargs): |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 48 | """Creates a new aggregate.""" |
Yuiko Takada | f93d248 | 2014-01-30 16:25:08 +0000 | [diff] [blame] | 49 | post_body = json.dumps({'aggregate': kwargs}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 50 | resp, body = self.post('os-aggregates', post_body) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 51 | |
| 52 | body = json.loads(body) |
| 53 | return resp, body['aggregate'] |
| 54 | |
| 55 | def update_aggregate(self, aggregate_id, name, availability_zone=None): |
| 56 | """Update a aggregate.""" |
| 57 | put_body = { |
| 58 | 'name': name, |
| 59 | 'availability_zone': availability_zone |
| 60 | } |
| 61 | put_body = json.dumps({'aggregate': put_body}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 62 | resp, body = self.put('os-aggregates/%s' % str(aggregate_id), put_body) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 63 | |
| 64 | body = json.loads(body) |
| 65 | return resp, body['aggregate'] |
| 66 | |
| 67 | def delete_aggregate(self, aggregate_id): |
| 68 | """Deletes the given aggregate.""" |
Ghanshyam | 0784162 | 2014-04-22 14:18:03 +0900 | [diff] [blame^] | 69 | resp, body = self.delete("os-aggregates/%s" % str(aggregate_id)) |
| 70 | self.validate_response(v3_schema.delete_aggregate, resp, body) |
| 71 | return resp, body |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 72 | |
| 73 | def is_resource_deleted(self, id): |
| 74 | try: |
| 75 | self.get_aggregate(id) |
| 76 | except exceptions.NotFound: |
| 77 | return True |
| 78 | return False |
| 79 | |
| 80 | def add_host(self, aggregate_id, host): |
| 81 | """Adds a host to the given aggregate.""" |
| 82 | post_body = { |
| 83 | 'host': host, |
| 84 | } |
| 85 | post_body = json.dumps({'add_host': post_body}) |
| 86 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 87 | post_body) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 88 | body = json.loads(body) |
| 89 | return resp, body['aggregate'] |
| 90 | |
| 91 | def remove_host(self, aggregate_id, host): |
| 92 | """Removes a host from the given aggregate.""" |
| 93 | post_body = { |
| 94 | 'host': host, |
| 95 | } |
| 96 | post_body = json.dumps({'remove_host': post_body}) |
| 97 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 98 | post_body) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 99 | body = json.loads(body) |
| 100 | return resp, body['aggregate'] |
| 101 | |
| 102 | def set_metadata(self, aggregate_id, meta): |
| 103 | """Replaces the aggregate's existing metadata with new metadata.""" |
| 104 | post_body = { |
| 105 | 'metadata': meta, |
| 106 | } |
| 107 | post_body = json.dumps({'set_metadata': post_body}) |
| 108 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 109 | post_body) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 110 | body = json.loads(body) |
Haiwei Xu | ddd3cda | 2014-04-03 23:39:48 +0900 | [diff] [blame] | 111 | self.validate_response(schema.aggregate_set_metadata, resp, body) |
ivan-zhu | 8577cb1 | 2013-08-20 14:38:36 +0800 | [diff] [blame] | 112 | return resp, body['aggregate'] |