blob: d9b79305ae49de019aacd510fedffb7e678457f3 [file] [log] [blame]
ivan-zhu8577cb12013-08-20 14:38:36 +08001# 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.v3 import aggregates as v3_schema
Haiwei Xu16ee2c82014-03-05 04:59:18 +090020from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
ivan-zhu8577cb12013-08-20 14:38:36 +080022from tempest import exceptions
23
Matthew Treinish684d8992014-01-30 16:27:40 +000024CONF = config.CONF
25
ivan-zhu8577cb12013-08-20 14:38:36 +080026
Haiwei Xu16ee2c82014-03-05 04:59:18 +090027class AggregatesV3ClientJSON(rest_client.RestClient):
ivan-zhu8577cb12013-08-20 14:38:36 +080028
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000029 def __init__(self, auth_provider):
30 super(AggregatesV3ClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000031 self.service = CONF.compute.catalog_v3_type
ivan-zhu8577cb12013-08-20 14:38:36 +080032
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)
ivan-zhu8577cb12013-08-20 14:38:36 +080038 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)
ivan-zhu8577cb12013-08-20 14:38:36 +080045 return resp, body['aggregate']
46
Yuiko Takadaf93d2482014-01-30 16:25:08 +000047 def create_aggregate(self, **kwargs):
ivan-zhu8577cb12013-08-20 14:38:36 +080048 """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)
ivan-zhu8577cb12013-08-20 14:38:36 +080051
52 body = json.loads(body)
Haiwei Xuc51d5702014-03-31 20:13:25 +090053 self.validate_response(v3_schema.create_aggregate, resp, body)
ivan-zhu8577cb12013-08-20 14:38:36 +080054 return resp, body['aggregate']
55
56 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)
ivan-zhu8577cb12013-08-20 14:38:36 +080064
65 body = json.loads(body)
Haiwei Xue20f9b72014-04-08 21:59:02 +090066 self.validate_response(schema.update_aggregate, resp, body)
ivan-zhu8577cb12013-08-20 14:38:36 +080067 return resp, body['aggregate']
68
69 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(v3_schema.delete_aggregate, resp, body)
73 return resp, body
ivan-zhu8577cb12013-08-20 14:38:36 +080074
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)
ivan-zhu8577cb12013-08-20 14:38:36 +080090 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +090091 self.validate_response(v3_schema.aggregate_add_remove_host, resp, body)
ivan-zhu8577cb12013-08-20 14:38:36 +080092 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)
ivan-zhu8577cb12013-08-20 14:38:36 +0800102 body = json.loads(body)
Haiwei Xucbd14572014-04-03 01:03:03 +0900103 self.validate_response(v3_schema.aggregate_add_remove_host, resp, body)
ivan-zhu8577cb12013-08-20 14:38:36 +0800104 return resp, body['aggregate']
105
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-zhu8577cb12013-08-20 14:38:36 +0800114 body = json.loads(body)
Haiwei Xuddd3cda2014-04-03 23:39:48 +0900115 self.validate_response(schema.aggregate_set_metadata, resp, body)
ivan-zhu8577cb12013-08-20 14:38:36 +0800116 return resp, body['aggregate']