blob: 28d4ff54ce048951b4ca110bceccdfa2aa30f3a6 [file] [log] [blame]
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +09001# 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 Treinish21905512015-07-13 10:33:35 -040016from oslo_serialization import jsonutils as json
Masayuki Igawabfa07602015-01-20 18:47:17 +090017from tempest_lib import exceptions as lib_exc
18
Rohan Kanaded610a022015-03-23 15:46:02 +053019from tempest.api_schema.response.compute.v2_1 import aggregates as schema
David Kranz0a735172015-01-16 10:51:18 -050020from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090022
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000023class AggregatesClient(service_client.ServiceClient):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090024
25 def list_aggregates(self):
26 """Get aggregate list."""
27 resp, body = self.get("os-aggregates")
28 body = json.loads(body)
Haiwei Xu88173c82014-03-20 03:15:13 +090029 self.validate_response(schema.list_aggregates, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050030 return service_client.ResponseBodyList(resp, body['aggregates'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090031
Ken'ichi Ohmichi3de6d982015-04-13 00:20:41 +000032 def show_aggregate(self, aggregate_id):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090033 """Get details of the given aggregate."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000034 resp, body = self.get("os-aggregates/%s" % aggregate_id)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090035 body = json.loads(body)
Haiwei Xu7e40d022014-03-25 22:42:13 +090036 self.validate_response(schema.get_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050037 return service_client.ResponseBody(resp, body['aggregate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090038
Yuiko Takadaf93d2482014-01-30 16:25:08 +000039 def create_aggregate(self, **kwargs):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090040 """Creates a new aggregate."""
Yuiko Takadaf93d2482014-01-30 16:25:08 +000041 post_body = json.dumps({'aggregate': kwargs})
vponomaryovf4c27f92014-02-18 10:56:42 +020042 resp, body = self.post('os-aggregates', post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090043
44 body = json.loads(body)
Rohan Kanaded610a022015-03-23 15:46:02 +053045 self.validate_response(schema.create_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050046 return service_client.ResponseBody(resp, body['aggregate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090047
Zhu Zhu7b5f6292013-09-22 15:47:54 +080048 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 Ohmichicd6e8992015-07-01 06:45:34 +000055 resp, body = self.put('os-aggregates/%s' % aggregate_id, put_body)
Zhu Zhu7b5f6292013-09-22 15:47:54 +080056
57 body = json.loads(body)
Haiwei Xue20f9b72014-04-08 21:59:02 +090058 self.validate_response(schema.update_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050059 return service_client.ResponseBody(resp, body['aggregate'])
Zhu Zhu7b5f6292013-09-22 15:47:54 +080060
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090061 def delete_aggregate(self, aggregate_id):
62 """Deletes the given aggregate."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000063 resp, body = self.delete("os-aggregates/%s" % aggregate_id)
Rohan Kanaded610a022015-03-23 15:46:02 +053064 self.validate_response(schema.delete_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050065 return service_client.ResponseBody(resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090066
67 def is_resource_deleted(self, id):
68 try:
Ken'ichi Ohmichi3de6d982015-04-13 00:20:41 +000069 self.show_aggregate(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090070 except lib_exc.NotFound:
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090071 return True
72 return False
73
Matt Riedemannd2b96512014-10-13 10:18:16 -070074 @property
75 def resource_type(self):
76 """Returns the primary type of resource this client works with."""
77 return 'aggregate'
78
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090079 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,
vponomaryovf4c27f92014-02-18 10:56:42 +020086 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090087 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +090088 self.validate_response(schema.aggregate_add_remove_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050089 return service_client.ResponseBody(resp, body['aggregate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090090
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,
vponomaryovf4c27f92014-02-18 10:56:42 +020098 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090099 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +0900100 self.validate_response(schema.aggregate_add_remove_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -0500101 return service_client.ResponseBody(resp, body['aggregate'])
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800102
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,
vponomaryovf4c27f92014-02-18 10:56:42 +0200110 post_body)
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800111 body = json.loads(body)
Haiwei Xuddd3cda2014-04-03 23:39:48 +0900112 self.validate_response(schema.aggregate_set_metadata, resp, body)
David Kranz0a735172015-01-16 10:51:18 -0500113 return service_client.ResponseBody(resp, body['aggregate'])