blob: d9e73de77a83d1754d4abaa5676607405e568afa [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
18from tempest.common.rest_client import RestClient
19from tempest import exceptions
20
21
22class AggregatesClientJSON(RestClient):
23
24 def __init__(self, config, username, password, auth_url, tenant_name=None):
25 super(AggregatesClientJSON, self).__init__(config, username, password,
26 auth_url, tenant_name)
27 self.service = self.config.compute.catalog_type
28
29 def list_aggregates(self):
30 """Get aggregate list."""
31 resp, body = self.get("os-aggregates")
32 body = json.loads(body)
33 return resp, body['aggregates']
34
35 def get_aggregate(self, aggregate_id):
36 """Get details of the given aggregate."""
37 resp, body = self.get("os-aggregates/%s" % str(aggregate_id))
38 body = json.loads(body)
39 return resp, body['aggregate']
40
41 def create_aggregate(self, name, availability_zone=None):
42 """Creates a new aggregate."""
43 post_body = {
44 'name': name,
45 'availability_zone': availability_zone,
46 }
47 post_body = json.dumps({'aggregate': post_body})
48 resp, body = self.post('os-aggregates', post_body, self.headers)
49
50 body = json.loads(body)
51 return resp, body['aggregate']
52
Zhu Zhu7b5f6292013-09-22 15:47:54 +080053 def update_aggregate(self, aggregate_id, name, availability_zone=None):
54 """Update a aggregate."""
55 put_body = {
56 'name': name,
57 'availability_zone': availability_zone
58 }
59 put_body = json.dumps({'aggregate': put_body})
60 resp, body = self.put('os-aggregates/%s' % str(aggregate_id),
61 put_body, self.headers)
62
63 body = json.loads(body)
64 return resp, body['aggregate']
65
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090066 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,
84 post_body, self.headers)
85 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,
95 post_body, self.headers)
96 body = json.loads(body)
97 return resp, body['aggregate']
ivan-zhu35e1f8e2013-10-18 15:51:16 +080098
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,
106 post_body, self.headers)
107 body = json.loads(body)
108 return resp, body['aggregate']