blob: 8d7440e553bd29e83d28c4c34afad5d355636361 [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
Haiwei Xu16ee2c82014-03-05 04:59:18 +090019from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
ivan-zhu8577cb12013-08-20 14:38:36 +080021from tempest import exceptions
22
Matthew Treinish684d8992014-01-30 16:27:40 +000023CONF = config.CONF
24
ivan-zhu8577cb12013-08-20 14:38:36 +080025
Haiwei Xu16ee2c82014-03-05 04:59:18 +090026class AggregatesV3ClientJSON(rest_client.RestClient):
ivan-zhu8577cb12013-08-20 14:38:36 +080027
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000028 def __init__(self, auth_provider):
29 super(AggregatesV3ClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.compute.catalog_v3_type
ivan-zhu8577cb12013-08-20 14:38:36 +080031
32 def list_aggregates(self):
33 """Get aggregate list."""
34 resp, body = self.get("os-aggregates")
35 body = json.loads(body)
Haiwei Xu88173c82014-03-20 03:15:13 +090036 self.validate_response(schema.list_aggregates, resp, body)
ivan-zhu8577cb12013-08-20 14:38:36 +080037 return resp, body['aggregates']
38
39 def get_aggregate(self, aggregate_id):
40 """Get details of the given aggregate."""
41 resp, body = self.get("os-aggregates/%s" % str(aggregate_id))
42 body = json.loads(body)
Haiwei Xu7e40d022014-03-25 22:42:13 +090043 self.validate_response(schema.get_aggregate, resp, body)
ivan-zhu8577cb12013-08-20 14:38:36 +080044 return resp, body['aggregate']
45
Yuiko Takadaf93d2482014-01-30 16:25:08 +000046 def create_aggregate(self, **kwargs):
ivan-zhu8577cb12013-08-20 14:38:36 +080047 """Creates a new aggregate."""
Yuiko Takadaf93d2482014-01-30 16:25:08 +000048 post_body = json.dumps({'aggregate': kwargs})
vponomaryovf4c27f92014-02-18 10:56:42 +020049 resp, body = self.post('os-aggregates', post_body)
ivan-zhu8577cb12013-08-20 14:38:36 +080050
51 body = json.loads(body)
52 return resp, body['aggregate']
53
54 def update_aggregate(self, aggregate_id, name, availability_zone=None):
55 """Update a aggregate."""
56 put_body = {
57 'name': name,
58 'availability_zone': availability_zone
59 }
60 put_body = json.dumps({'aggregate': put_body})
vponomaryovf4c27f92014-02-18 10:56:42 +020061 resp, body = self.put('os-aggregates/%s' % str(aggregate_id), put_body)
ivan-zhu8577cb12013-08-20 14:38:36 +080062
63 body = json.loads(body)
64 return resp, body['aggregate']
65
66 def delete_aggregate(self, aggregate_id):
67 """Deletes the given aggregate."""
68 return self.delete("os-aggregates/%s" % str(aggregate_id))
69
70 def is_resource_deleted(self, id):
71 try:
72 self.get_aggregate(id)
73 except exceptions.NotFound:
74 return True
75 return False
76
77 def add_host(self, aggregate_id, host):
78 """Adds a host to the given aggregate."""
79 post_body = {
80 'host': host,
81 }
82 post_body = json.dumps({'add_host': post_body})
83 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020084 post_body)
ivan-zhu8577cb12013-08-20 14:38:36 +080085 body = json.loads(body)
86 return resp, body['aggregate']
87
88 def remove_host(self, aggregate_id, host):
89 """Removes a host from the given aggregate."""
90 post_body = {
91 'host': host,
92 }
93 post_body = json.dumps({'remove_host': post_body})
94 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +020095 post_body)
ivan-zhu8577cb12013-08-20 14:38:36 +080096 body = json.loads(body)
97 return resp, body['aggregate']
98
99 def set_metadata(self, aggregate_id, meta):
100 """Replaces the aggregate's existing metadata with new metadata."""
101 post_body = {
102 'metadata': meta,
103 }
104 post_body = json.dumps({'set_metadata': post_body})
105 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
vponomaryovf4c27f92014-02-18 10:56:42 +0200106 post_body)
ivan-zhu8577cb12013-08-20 14:38:36 +0800107 body = json.loads(body)
Haiwei Xuddd3cda2014-04-03 23:39:48 +0900108 self.validate_response(schema.aggregate_set_metadata, resp, body)
ivan-zhu8577cb12013-08-20 14:38:36 +0800109 return resp, body['aggregate']