blob: 09927d3435be76fe3f79e9047c3aece26cc27545 [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
Marc Koderer6fbd74f2014-08-04 09:38:19 +020018from tempest.api_schema.response.compute import aggregates as schema
19from tempest.api_schema.response.compute.v2 import aggregates as v2_schema
Haiwei Xuab924622014-03-05 04:33:51 +090020from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090022from tempest import exceptions
23
Matthew Treinish684d8992014-01-30 16:27:40 +000024CONF = config.CONF
25
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090026
Haiwei Xuab924622014-03-05 04:33:51 +090027class AggregatesClientJSON(rest_client.RestClient):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090028
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000029 def __init__(self, auth_provider):
30 super(AggregatesClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000031 self.service = CONF.compute.catalog_type
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090032
33 def list_aggregates(self):
34 """Get aggregate list."""
35 resp, body = self.get("os-aggregates")
36 body = json.loads(body)
Haiwei Xu88173c82014-03-20 03:15:13 +090037 self.validate_response(schema.list_aggregates, resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090038 return resp, body['aggregates']
39
40 def get_aggregate(self, aggregate_id):
41 """Get details of the given aggregate."""
42 resp, body = self.get("os-aggregates/%s" % str(aggregate_id))
43 body = json.loads(body)
Haiwei Xu7e40d022014-03-25 22:42:13 +090044 self.validate_response(schema.get_aggregate, resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090045 return resp, body['aggregate']
46
Yuiko Takadaf93d2482014-01-30 16:25:08 +000047 def create_aggregate(self, **kwargs):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090048 """Creates a new aggregate."""
Yuiko Takadaf93d2482014-01-30 16:25:08 +000049 post_body = json.dumps({'aggregate': kwargs})
vponomaryovf4c27f92014-02-18 10:56:42 +020050 resp, body = self.post('os-aggregates', post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090051
52 body = json.loads(body)
Haiwei Xuc51d5702014-03-31 20:13:25 +090053 self.validate_response(v2_schema.create_aggregate, resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090054 return resp, body['aggregate']
55
Zhu Zhu7b5f6292013-09-22 15:47:54 +080056 def update_aggregate(self, aggregate_id, name, availability_zone=None):
57 """Update a aggregate."""
58 put_body = {
59 'name': name,
60 'availability_zone': availability_zone
61 }
62 put_body = json.dumps({'aggregate': put_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020063 resp, body = self.put('os-aggregates/%s' % str(aggregate_id), put_body)
Zhu Zhu7b5f6292013-09-22 15:47:54 +080064
65 body = json.loads(body)
Haiwei Xue20f9b72014-04-08 21:59:02 +090066 self.validate_response(schema.update_aggregate, resp, body)
Zhu Zhu7b5f6292013-09-22 15:47:54 +080067 return resp, body['aggregate']
68
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090069 def delete_aggregate(self, aggregate_id):
70 """Deletes the given aggregate."""
Ghanshyam07841622014-04-22 14:18:03 +090071 resp, body = self.delete("os-aggregates/%s" % str(aggregate_id))
72 self.validate_response(v2_schema.delete_aggregate, resp, body)
73 return resp, body
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090074
75 def is_resource_deleted(self, id):
76 try:
77 self.get_aggregate(id)
78 except exceptions.NotFound:
79 return True
80 return False
81
Matt Riedemannd2b96512014-10-13 10:18:16 -070082 @property
83 def resource_type(self):
84 """Returns the primary type of resource this client works with."""
85 return 'aggregate'
86
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090087 def add_host(self, aggregate_id, host):
88 """Adds a host to the given aggregate."""
89 post_body = {
90 'host': host,
91 }
92 post_body = json.dumps({'add_host': post_body})
93 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020094 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090095 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +090096 self.validate_response(schema.aggregate_add_remove_host, resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090097 return resp, body['aggregate']
98
99 def remove_host(self, aggregate_id, host):
100 """Removes a host from the given aggregate."""
101 post_body = {
102 'host': host,
103 }
104 post_body = json.dumps({'remove_host': post_body})
105 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200106 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900107 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +0900108 self.validate_response(schema.aggregate_add_remove_host, resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900109 return resp, body['aggregate']
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800110
111 def set_metadata(self, aggregate_id, meta):
112 """Replaces the aggregate's existing metadata with new metadata."""
113 post_body = {
114 'metadata': meta,
115 }
116 post_body = json.dumps({'set_metadata': post_body})
117 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200118 post_body)
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800119 body = json.loads(body)
Haiwei Xuddd3cda2014-04-03 23:39:48 +0900120 self.validate_response(schema.aggregate_set_metadata, resp, body)
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800121 return resp, body['aggregate']