blob: 71d6f6333f46d5770e4460825e16f616becea935 [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
Haiwei Xu88173c82014-03-20 03:15:13 +090018from tempest.api_schema.compute import aggregates as schema
Ghanshyam07841622014-04-22 14:18:03 +090019from tempest.api_schema.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
82 def add_host(self, aggregate_id, host):
83 """Adds a host to the given aggregate."""
84 post_body = {
85 'host': host,
86 }
87 post_body = json.dumps({'add_host': post_body})
88 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020089 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090090 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +090091 self.validate_response(schema.aggregate_add_remove_host, resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090092 return resp, body['aggregate']
93
94 def remove_host(self, aggregate_id, host):
95 """Removes a host from the given aggregate."""
96 post_body = {
97 'host': host,
98 }
99 post_body = json.dumps({'remove_host': post_body})
100 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200101 post_body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900102 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +0900103 self.validate_response(schema.aggregate_add_remove_host, resp, body)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900104 return resp, body['aggregate']
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800105
106 def set_metadata(self, aggregate_id, meta):
107 """Replaces the aggregate's existing metadata with new metadata."""
108 post_body = {
109 'metadata': meta,
110 }
111 post_body = json.dumps({'set_metadata': post_body})
112 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200113 post_body)
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800114 body = json.loads(body)
Haiwei Xuddd3cda2014-04-03 23:39:48 +0900115 self.validate_response(schema.aggregate_set_metadata, resp, body)
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800116 return resp, body['aggregate']