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