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