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 | |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 16 | from oslo_serialization import jsonutils as json |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 17 | from tempest_lib import exceptions as lib_exc |
| 18 | |
Rohan Kanade | d610a02 | 2015-03-23 15:46:02 +0530 | [diff] [blame] | 19 | from tempest.api_schema.response.compute.v2_1 import aggregates as schema |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 20 | from tempest.common import service_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 21 | |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 22 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 23 | class AggregatesClient(service_client.ServiceClient): |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 24 | |
| 25 | def list_aggregates(self): |
| 26 | """Get aggregate list.""" |
| 27 | resp, body = self.get("os-aggregates") |
| 28 | body = json.loads(body) |
Haiwei Xu | 88173c8 | 2014-03-20 03:15:13 +0900 | [diff] [blame] | 29 | self.validate_response(schema.list_aggregates, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 30 | return service_client.ResponseBodyList(resp, body['aggregates']) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 31 | |
Ken'ichi Ohmichi | 3de6d98 | 2015-04-13 00:20:41 +0000 | [diff] [blame] | 32 | def show_aggregate(self, aggregate_id): |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 33 | """Get details of the given aggregate.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 34 | resp, body = self.get("os-aggregates/%s" % aggregate_id) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 35 | body = json.loads(body) |
Haiwei Xu | 7e40d02 | 2014-03-25 22:42:13 +0900 | [diff] [blame] | 36 | self.validate_response(schema.get_aggregate, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 37 | return service_client.ResponseBody(resp, body['aggregate']) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 38 | |
Yuiko Takada | f93d248 | 2014-01-30 16:25:08 +0000 | [diff] [blame] | 39 | def create_aggregate(self, **kwargs): |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 40 | """Creates a new aggregate.""" |
Yuiko Takada | f93d248 | 2014-01-30 16:25:08 +0000 | [diff] [blame] | 41 | post_body = json.dumps({'aggregate': kwargs}) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 42 | resp, body = self.post('os-aggregates', post_body) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 43 | |
| 44 | body = json.loads(body) |
Rohan Kanade | d610a02 | 2015-03-23 15:46:02 +0530 | [diff] [blame] | 45 | self.validate_response(schema.create_aggregate, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 46 | return service_client.ResponseBody(resp, body['aggregate']) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 47 | |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 48 | def update_aggregate(self, aggregate_id, name, availability_zone=None): |
| 49 | """Update a aggregate.""" |
| 50 | put_body = { |
| 51 | 'name': name, |
| 52 | 'availability_zone': availability_zone |
| 53 | } |
| 54 | put_body = json.dumps({'aggregate': put_body}) |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 55 | resp, body = self.put('os-aggregates/%s' % aggregate_id, put_body) |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 56 | |
| 57 | body = json.loads(body) |
Haiwei Xu | e20f9b7 | 2014-04-08 21:59:02 +0900 | [diff] [blame] | 58 | self.validate_response(schema.update_aggregate, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 59 | return service_client.ResponseBody(resp, body['aggregate']) |
Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 60 | |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 61 | def delete_aggregate(self, aggregate_id): |
| 62 | """Deletes the given aggregate.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 63 | resp, body = self.delete("os-aggregates/%s" % aggregate_id) |
Rohan Kanade | d610a02 | 2015-03-23 15:46:02 +0530 | [diff] [blame] | 64 | self.validate_response(schema.delete_aggregate, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 65 | return service_client.ResponseBody(resp, body) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 66 | |
| 67 | def is_resource_deleted(self, id): |
| 68 | try: |
Ken'ichi Ohmichi | 3de6d98 | 2015-04-13 00:20:41 +0000 | [diff] [blame] | 69 | self.show_aggregate(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 70 | except lib_exc.NotFound: |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 71 | return True |
| 72 | return False |
| 73 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 74 | @property |
| 75 | def resource_type(self): |
| 76 | """Returns the primary type of resource this client works with.""" |
| 77 | return 'aggregate' |
| 78 | |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 79 | def add_host(self, aggregate_id, host): |
| 80 | """Adds a host to the given aggregate.""" |
| 81 | post_body = { |
| 82 | 'host': host, |
| 83 | } |
| 84 | post_body = json.dumps({'add_host': post_body}) |
| 85 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 86 | post_body) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 87 | body = json.loads(body) |
Haiwei Xu | cbd1457 | 2014-04-03 01:03:03 +0900 | [diff] [blame] | 88 | self.validate_response(schema.aggregate_add_remove_host, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 89 | return service_client.ResponseBody(resp, body['aggregate']) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 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) |
Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 99 | body = json.loads(body) |
Haiwei Xu | cbd1457 | 2014-04-03 01:03:03 +0900 | [diff] [blame] | 100 | self.validate_response(schema.aggregate_add_remove_host, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 101 | return service_client.ResponseBody(resp, body['aggregate']) |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 102 | |
| 103 | def set_metadata(self, aggregate_id, meta): |
| 104 | """Replaces the aggregate's existing metadata with new metadata.""" |
| 105 | post_body = { |
| 106 | 'metadata': meta, |
| 107 | } |
| 108 | post_body = json.dumps({'set_metadata': post_body}) |
| 109 | resp, body = self.post('os-aggregates/%s/action' % aggregate_id, |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 110 | post_body) |
ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 111 | body = json.loads(body) |
Haiwei Xu | ddd3cda | 2014-04-03 23:39:48 +0900 | [diff] [blame] | 112 | self.validate_response(schema.aggregate_set_metadata, resp, body) |
David Kranz | 0a73517 | 2015-01-16 10:51:18 -0500 | [diff] [blame] | 113 | return service_client.ResponseBody(resp, body['aggregate']) |