blob: bc037c975942215f2f81e646f70d1c4b6259797a [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
18from tempest.common.rest_client import RestClient
Matthew Treinish684d8992014-01-30 16:27:40 +000019from tempest import config
ivan-zhu8577cb12013-08-20 14:38:36 +080020from tempest import exceptions
21
Matthew Treinish684d8992014-01-30 16:27:40 +000022CONF = config.CONF
23
ivan-zhu8577cb12013-08-20 14:38:36 +080024
ivan-zhu00fe64f2013-08-20 19:35:51 +080025class AggregatesV3ClientJSON(RestClient):
ivan-zhu8577cb12013-08-20 14:38:36 +080026
Matthew Treinish684d8992014-01-30 16:27:40 +000027 def __init__(self, username, password, auth_url, tenant_name=None):
28 super(AggregatesV3ClientJSON, self).__init__(username,
ivan-zhu00fe64f2013-08-20 19:35:51 +080029 password, auth_url,
30 tenant_name)
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)
37 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)
43 return resp, body['aggregate']
44
45 def create_aggregate(self, name, availability_zone=None):
46 """Creates a new aggregate."""
47 post_body = {
48 'name': name,
49 'availability_zone': availability_zone,
50 }
51 post_body = json.dumps({'aggregate': post_body})
52 resp, body = self.post('os-aggregates', post_body, self.headers)
53
54 body = json.loads(body)
55 return resp, body['aggregate']
56
57 def update_aggregate(self, aggregate_id, name, availability_zone=None):
58 """Update a aggregate."""
59 put_body = {
60 'name': name,
61 'availability_zone': availability_zone
62 }
63 put_body = json.dumps({'aggregate': put_body})
64 resp, body = self.put('os-aggregates/%s' % str(aggregate_id),
65 put_body, self.headers)
66
67 body = json.loads(body)
68 return resp, body['aggregate']
69
70 def delete_aggregate(self, aggregate_id):
71 """Deletes the given aggregate."""
72 return self.delete("os-aggregates/%s" % str(aggregate_id))
73
74 def is_resource_deleted(self, id):
75 try:
76 self.get_aggregate(id)
77 except exceptions.NotFound:
78 return True
79 return False
80
81 def add_host(self, aggregate_id, host):
82 """Adds a host to the given aggregate."""
83 post_body = {
84 'host': host,
85 }
86 post_body = json.dumps({'add_host': post_body})
87 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
88 post_body, self.headers)
89 body = json.loads(body)
90 return resp, body['aggregate']
91
92 def remove_host(self, aggregate_id, host):
93 """Removes a host from the given aggregate."""
94 post_body = {
95 'host': host,
96 }
97 post_body = json.dumps({'remove_host': post_body})
98 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
99 post_body, self.headers)
100 body = json.loads(body)
101 return resp, body['aggregate']
102
103 def set_metadata(self, aggregate_id, meta):
104 """Replaces the aggregate's existing metadata with new metadata."""
105 post_body = {
106 'metadata': meta,
107 }
108 post_body = json.dumps({'set_metadata': post_body})
109 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
110 post_body, self.headers)
111 body = json.loads(body)
112 return resp, body['aggregate']