blob: 36a347bbb70ce861c9d919553070302606bad286 [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
16import json
17
Masayuki Igawabfa07602015-01-20 18:47:17 +090018from tempest_lib import exceptions as lib_exc
19
Rohan Kanaded610a022015-03-23 15:46:02 +053020from tempest.api_schema.response.compute.v2_1 import aggregates as schema
David Kranz0a735172015-01-16 10:51:18 -050021from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000022
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090023
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000024class AggregatesClientJSON(service_client.ServiceClient):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090025
26 def list_aggregates(self):
27 """Get aggregate list."""
28 resp, body = self.get("os-aggregates")
29 body = json.loads(body)
Haiwei Xu88173c82014-03-20 03:15:13 +090030 self.validate_response(schema.list_aggregates, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050031 return service_client.ResponseBodyList(resp, body['aggregates'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090032
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 Xu7e40d022014-03-25 22:42:13 +090037 self.validate_response(schema.get_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050038 return service_client.ResponseBody(resp, body['aggregate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090039
Yuiko Takadaf93d2482014-01-30 16:25:08 +000040 def create_aggregate(self, **kwargs):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090041 """Creates a new aggregate."""
Yuiko Takadaf93d2482014-01-30 16:25:08 +000042 post_body = json.dumps({'aggregate': kwargs})
vponomaryovf4c27f92014-02-18 10:56:42 +020043 resp, body = self.post('os-aggregates', post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090044
45 body = json.loads(body)
Rohan Kanaded610a022015-03-23 15:46:02 +053046 self.validate_response(schema.create_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050047 return service_client.ResponseBody(resp, body['aggregate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090048
Zhu Zhu7b5f6292013-09-22 15:47:54 +080049 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})
vponomaryovf4c27f92014-02-18 10:56:42 +020056 resp, body = self.put('os-aggregates/%s' % str(aggregate_id), put_body)
Zhu Zhu7b5f6292013-09-22 15:47:54 +080057
58 body = json.loads(body)
Haiwei Xue20f9b72014-04-08 21:59:02 +090059 self.validate_response(schema.update_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050060 return service_client.ResponseBody(resp, body['aggregate'])
Zhu Zhu7b5f6292013-09-22 15:47:54 +080061
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090062 def delete_aggregate(self, aggregate_id):
63 """Deletes the given aggregate."""
Ghanshyam07841622014-04-22 14:18:03 +090064 resp, body = self.delete("os-aggregates/%s" % str(aggregate_id))
Rohan Kanaded610a022015-03-23 15:46:02 +053065 self.validate_response(schema.delete_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050066 return service_client.ResponseBody(resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090067
68 def is_resource_deleted(self, id):
69 try:
70 self.get_aggregate(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090071 except lib_exc.NotFound:
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090072 return True
73 return False
74
Matt Riedemannd2b96512014-10-13 10:18:16 -070075 @property
76 def resource_type(self):
77 """Returns the primary type of resource this client works with."""
78 return 'aggregate'
79
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090080 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,
vponomaryovf4c27f92014-02-18 10:56:42 +020087 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090088 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +090089 self.validate_response(schema.aggregate_add_remove_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050090 return service_client.ResponseBody(resp, body['aggregate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090091
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,
vponomaryovf4c27f92014-02-18 10:56:42 +020099 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900100 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +0900101 self.validate_response(schema.aggregate_add_remove_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -0500102 return service_client.ResponseBody(resp, body['aggregate'])
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800103
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,
vponomaryovf4c27f92014-02-18 10:56:42 +0200111 post_body)
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800112 body = json.loads(body)
Haiwei Xuddd3cda2014-04-03 23:39:48 +0900113 self.validate_response(schema.aggregate_set_metadata, resp, body)
David Kranz0a735172015-01-16 10:51:18 -0500114 return service_client.ResponseBody(resp, body['aggregate'])