blob: 168126c513696d019dc266009e45e5a795d19bf7 [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# 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
16from oslo_serialization import jsonutils as json
17
18from tempest.lib.api_schema.response.compute.v2_1 import aggregates as schema
19from tempest.lib.common import rest_client
20from tempest.lib import exceptions as lib_exc
Ghanshyamee9af302016-02-25 06:12:43 +090021from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050022
23
Ghanshyamee9af302016-02-25 06:12:43 +090024class AggregatesClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050025
26 def list_aggregates(self):
27 """Get aggregate list."""
28 resp, body = self.get("os-aggregates")
29 body = json.loads(body)
30 self.validate_response(schema.list_aggregates, resp, body)
31 return rest_client.ResponseBody(resp, body)
32
33 def show_aggregate(self, aggregate_id):
34 """Get details of the given aggregate."""
35 resp, body = self.get("os-aggregates/%s" % aggregate_id)
36 body = json.loads(body)
37 self.validate_response(schema.get_aggregate, resp, body)
38 return rest_client.ResponseBody(resp, body)
39
40 def create_aggregate(self, **kwargs):
41 """Create a new aggregate.
42
43 Available params: see http://developer.openstack.org/
44 api-ref-compute-v2.1.html#createaggregate
45 """
46 post_body = json.dumps({'aggregate': kwargs})
47 resp, body = self.post('os-aggregates', post_body)
48
49 body = json.loads(body)
50 self.validate_response(schema.create_aggregate, resp, body)
51 return rest_client.ResponseBody(resp, body)
52
53 def update_aggregate(self, aggregate_id, **kwargs):
54 """Update an aggregate.
55
56 Available params: see http://developer.openstack.org/
57 api-ref-compute-v2.1.html#updateaggregate
58 """
59 put_body = json.dumps({'aggregate': kwargs})
60 resp, body = self.put('os-aggregates/%s' % aggregate_id, put_body)
61
62 body = json.loads(body)
63 self.validate_response(schema.update_aggregate, resp, body)
64 return rest_client.ResponseBody(resp, body)
65
66 def delete_aggregate(self, aggregate_id):
67 """Delete the given aggregate."""
68 resp, body = self.delete("os-aggregates/%s" % aggregate_id)
69 self.validate_response(schema.delete_aggregate, resp, body)
70 return rest_client.ResponseBody(resp, body)
71
72 def is_resource_deleted(self, id):
73 try:
74 self.show_aggregate(id)
75 except lib_exc.NotFound:
76 return True
77 return False
78
79 @property
80 def resource_type(self):
81 """Return the primary type of resource this client works with."""
82 return 'aggregate'
83
84 def add_host(self, aggregate_id, **kwargs):
85 """Add a host to the given aggregate.
86
87 Available params: see http://developer.openstack.org/
88 api-ref-compute-v2.1.html#addhost
89 """
90 post_body = json.dumps({'add_host': kwargs})
91 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
92 post_body)
93 body = json.loads(body)
94 self.validate_response(schema.aggregate_add_remove_host, resp, body)
95 return rest_client.ResponseBody(resp, body)
96
97 def remove_host(self, aggregate_id, **kwargs):
98 """Remove a host from the given aggregate.
99
100 Available params: see http://developer.openstack.org/
101 api-ref-compute-v2.1.html#removehost
102 """
103 post_body = json.dumps({'remove_host': kwargs})
104 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
105 post_body)
106 body = json.loads(body)
107 self.validate_response(schema.aggregate_add_remove_host, resp, body)
108 return rest_client.ResponseBody(resp, body)
109
110 def set_metadata(self, aggregate_id, **kwargs):
111 """Replace the aggregate's existing metadata with new metadata."""
112 post_body = json.dumps({'set_metadata': kwargs})
113 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
114 post_body)
115 body = json.loads(body)
116 self.validate_response(schema.aggregate_set_metadata, resp, body)
117 return rest_client.ResponseBody(resp, body)