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 | |
Marc Koderer | 6fbd74f | 2014-08-04 09:38:19 +0200 | [diff] [blame] | 18 | from tempest.api_schema.response.compute import aggregates as schema |
| 19 | from tempest.api_schema.response.compute.v2 import aggregates as v2_schema |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +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 |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [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 | |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 26 | |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 27 | class AggregatesClientJSON(rest_client.RestClient): |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 28 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 29 | def __init__(self, auth_provider): |
| 30 | super(AggregatesClientJSON, 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 | 74f0707 | 2013-04-02 11:52:31 +0900 | [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) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [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) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [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): |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [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) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 51 | |
| 52 | body = json.loads(body) |
Haiwei Xu | c51d570 | 2014-03-31 20:13:25 +0900 | [diff] [blame] | 53 | self.validate_response(v2_schema.create_aggregate, resp, body) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 54 | return resp, body['aggregate'] |
| 55 | |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 56 | def update_aggregate(self, aggregate_id, name, availability_zone=None): |
| 57 | """Update a aggregate.""" |
| 58 | put_body = { |
| 59 | 'name': name, |
| 60 | 'availability_zone': availability_zone |
| 61 | } |
| 62 | put_body = json.dumps({'aggregate': put_body}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 63 | resp, body = self.put('os-aggregates/%s' % str(aggregate_id), put_body) |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 64 | |
| 65 | body = json.loads(body) |
Haiwei Xu | e20f9b7 | 2014-04-08 21:59:02 +0900 | [diff] [blame] | 66 | self.validate_response(schema.update_aggregate, resp, body) |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 67 | return resp, body['aggregate'] |
| 68 | |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 69 | def delete_aggregate(self, aggregate_id): |
| 70 | """Deletes the given aggregate.""" |
Ghanshyam | 0784162 | 2014-04-22 14:18:03 +0900 | [diff] [blame] | 71 | resp, body = self.delete("os-aggregates/%s" % str(aggregate_id)) |
| 72 | self.validate_response(v2_schema.delete_aggregate, resp, body) |
| 73 | return resp, body |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 74 | |
| 75 | def is_resource_deleted(self, id): |
| 76 | try: |
| 77 | self.get_aggregate(id) |
| 78 | except exceptions.NotFound: |
| 79 | return True |
| 80 | return False |
| 81 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame^] | 82 | @property |
| 83 | def resource_type(self): |
| 84 | """Returns the primary type of resource this client works with.""" |
| 85 | return 'aggregate' |
| 86 | |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 87 | def add_host(self, aggregate_id, host): |
| 88 | """Adds a host to the given aggregate.""" |
| 89 | post_body = { |
| 90 | 'host': host, |
| 91 | } |
| 92 | post_body = json.dumps({'add_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) |
Haiwei Xu | cbd1457 | 2014-04-03 01:03:03 +0900 | [diff] [blame] | 96 | self.validate_response(schema.aggregate_add_remove_host, resp, body) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 97 | return resp, body['aggregate'] |
| 98 | |
| 99 | def remove_host(self, aggregate_id, host): |
| 100 | """Removes a host from the given aggregate.""" |
| 101 | post_body = { |
| 102 | 'host': host, |
| 103 | } |
| 104 | post_body = json.dumps({'remove_host': post_body}) |
| 105 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 106 | post_body) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 107 | body = json.loads(body) |
Haiwei Xu | cbd1457 | 2014-04-03 01:03:03 +0900 | [diff] [blame] | 108 | self.validate_response(schema.aggregate_add_remove_host, resp, body) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 109 | return resp, body['aggregate'] |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 110 | |
| 111 | def set_metadata(self, aggregate_id, meta): |
| 112 | """Replaces the aggregate's existing metadata with new metadata.""" |
| 113 | post_body = { |
| 114 | 'metadata': meta, |
| 115 | } |
| 116 | post_body = json.dumps({'set_metadata': post_body}) |
| 117 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 118 | post_body) |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 119 | body = json.loads(body) |
Haiwei Xu | ddd3cda | 2014-04-03 23:39:48 +0900 | [diff] [blame] | 120 | self.validate_response(schema.aggregate_set_metadata, resp, body) |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 121 | return resp, body['aggregate'] |