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