blob: 10955fd544d9b70b1f81125c7ae40e9dbed6ede1 [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
Marc Koderer6fbd74f2014-08-04 09:38:19 +020020from tempest.api_schema.response.compute import aggregates as schema
21from tempest.api_schema.response.compute.v2 import aggregates as v2_schema
David Kranz0a735172015-01-16 10:51:18 -050022from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000023
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090024
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000025class AggregatesClientJSON(service_client.ServiceClient):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090026
27 def list_aggregates(self):
28 """Get aggregate list."""
29 resp, body = self.get("os-aggregates")
30 body = json.loads(body)
Haiwei Xu88173c82014-03-20 03:15:13 +090031 self.validate_response(schema.list_aggregates, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050032 return service_client.ResponseBodyList(resp, body['aggregates'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090033
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 Xu7e40d022014-03-25 22:42:13 +090038 self.validate_response(schema.get_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050039 return service_client.ResponseBody(resp, body['aggregate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090040
Yuiko Takadaf93d2482014-01-30 16:25:08 +000041 def create_aggregate(self, **kwargs):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090042 """Creates a new aggregate."""
Yuiko Takadaf93d2482014-01-30 16:25:08 +000043 post_body = json.dumps({'aggregate': kwargs})
vponomaryovf4c27f92014-02-18 10:56:42 +020044 resp, body = self.post('os-aggregates', post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090045
46 body = json.loads(body)
Haiwei Xuc51d5702014-03-31 20:13:25 +090047 self.validate_response(v2_schema.create_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050048 return service_client.ResponseBody(resp, body['aggregate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090049
Zhu Zhu7b5f6292013-09-22 15:47:54 +080050 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})
vponomaryovf4c27f92014-02-18 10:56:42 +020057 resp, body = self.put('os-aggregates/%s' % str(aggregate_id), put_body)
Zhu Zhu7b5f6292013-09-22 15:47:54 +080058
59 body = json.loads(body)
Haiwei Xue20f9b72014-04-08 21:59:02 +090060 self.validate_response(schema.update_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050061 return service_client.ResponseBody(resp, body['aggregate'])
Zhu Zhu7b5f6292013-09-22 15:47:54 +080062
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090063 def delete_aggregate(self, aggregate_id):
64 """Deletes the given aggregate."""
Ghanshyam07841622014-04-22 14:18:03 +090065 resp, body = self.delete("os-aggregates/%s" % str(aggregate_id))
66 self.validate_response(v2_schema.delete_aggregate, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050067 return service_client.ResponseBody(resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090068
69 def is_resource_deleted(self, id):
70 try:
71 self.get_aggregate(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090072 except lib_exc.NotFound:
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090073 return True
74 return False
75
Matt Riedemannd2b96512014-10-13 10:18:16 -070076 @property
77 def resource_type(self):
78 """Returns the primary type of resource this client works with."""
79 return 'aggregate'
80
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090081 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,
vponomaryovf4c27f92014-02-18 10:56:42 +020088 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090089 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +090090 self.validate_response(schema.aggregate_add_remove_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -050091 return service_client.ResponseBody(resp, body['aggregate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090092
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,
vponomaryovf4c27f92014-02-18 10:56:42 +0200100 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900101 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +0900102 self.validate_response(schema.aggregate_add_remove_host, resp, body)
David Kranz0a735172015-01-16 10:51:18 -0500103 return service_client.ResponseBody(resp, body['aggregate'])
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800104
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,
vponomaryovf4c27f92014-02-18 10:56:42 +0200112 post_body)
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800113 body = json.loads(body)
Haiwei Xuddd3cda2014-04-03 23:39:48 +0900114 self.validate_response(schema.aggregate_set_metadata, resp, body)
David Kranz0a735172015-01-16 10:51:18 -0500115 return service_client.ResponseBody(resp, body['aggregate'])