blob: 57f5e4e79af67f9397ad7d5c731b181789fa9d6e [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
zhufl4af2c822018-08-06 14:38:53 +080018from tempest.lib.api_schema.response.compute.v2_1 \
19 import aggregates as schema
20from tempest.lib.api_schema.response.compute.v2_41 \
21 import aggregates as schemav241
Matthew Treinish9e26ca82016-02-23 11:43:20 -050022from tempest.lib.common import rest_client
23from tempest.lib import exceptions as lib_exc
Ghanshyamee9af302016-02-25 06:12:43 +090024from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050025
26
Ghanshyamee9af302016-02-25 06:12:43 +090027class AggregatesClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050028
zhufl4af2c822018-08-06 14:38:53 +080029 schema_versions_info = [
30 {'min': None, 'max': '2.40', 'schema': schema},
31 {'min': '2.41', 'max': None, 'schema': schemav241}]
32
Matthew Treinish9e26ca82016-02-23 11:43:20 -050033 def list_aggregates(self):
34 """Get aggregate list."""
35 resp, body = self.get("os-aggregates")
36 body = json.loads(body)
zhufl4af2c822018-08-06 14:38:53 +080037 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050038 self.validate_response(schema.list_aggregates, resp, body)
39 return rest_client.ResponseBody(resp, body)
40
41 def show_aggregate(self, aggregate_id):
42 """Get details of the given aggregate."""
43 resp, body = self.get("os-aggregates/%s" % aggregate_id)
44 body = json.loads(body)
zhufl4af2c822018-08-06 14:38:53 +080045 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050046 self.validate_response(schema.get_aggregate, resp, body)
47 return rest_client.ResponseBody(resp, body)
48
49 def create_aggregate(self, **kwargs):
50 """Create a new aggregate.
51
Dong Mad12c2332016-10-19 01:36:27 -070052 For a full list of available parameters, please refer to the official
53 API reference:
zhufl8d4134b2017-03-08 15:04:20 +080054 https://developer.openstack.org/api-ref/compute/#create-aggregate
Matthew Treinish9e26ca82016-02-23 11:43:20 -050055 """
56 post_body = json.dumps({'aggregate': kwargs})
57 resp, body = self.post('os-aggregates', post_body)
58
59 body = json.loads(body)
zhufl4af2c822018-08-06 14:38:53 +080060 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050061 self.validate_response(schema.create_aggregate, resp, body)
62 return rest_client.ResponseBody(resp, body)
63
64 def update_aggregate(self, aggregate_id, **kwargs):
65 """Update an aggregate.
66
Dong Mad12c2332016-10-19 01:36:27 -070067 For a full list of available parameters, please refer to the official
68 API reference:
zhufl8d4134b2017-03-08 15:04:20 +080069 https://developer.openstack.org/api-ref/compute/#update-aggregate
Matthew Treinish9e26ca82016-02-23 11:43:20 -050070 """
71 put_body = json.dumps({'aggregate': kwargs})
72 resp, body = self.put('os-aggregates/%s' % aggregate_id, put_body)
73
74 body = json.loads(body)
zhufl4af2c822018-08-06 14:38:53 +080075 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050076 self.validate_response(schema.update_aggregate, resp, body)
77 return rest_client.ResponseBody(resp, body)
78
79 def delete_aggregate(self, aggregate_id):
80 """Delete the given aggregate."""
81 resp, body = self.delete("os-aggregates/%s" % aggregate_id)
zhufl4af2c822018-08-06 14:38:53 +080082 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050083 self.validate_response(schema.delete_aggregate, resp, body)
84 return rest_client.ResponseBody(resp, body)
85
86 def is_resource_deleted(self, id):
87 try:
88 self.show_aggregate(id)
89 except lib_exc.NotFound:
90 return True
91 return False
92
93 @property
94 def resource_type(self):
95 """Return the primary type of resource this client works with."""
96 return 'aggregate'
97
98 def add_host(self, aggregate_id, **kwargs):
99 """Add a host to the given aggregate.
100
Dong Mad12c2332016-10-19 01:36:27 -0700101 For a full list of available parameters, please refer to the official
102 API reference:
zhufl8d4134b2017-03-08 15:04:20 +0800103 https://developer.openstack.org/api-ref/compute/#add-host
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500104 """
105 post_body = json.dumps({'add_host': kwargs})
106 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
107 post_body)
108 body = json.loads(body)
zhufl4af2c822018-08-06 14:38:53 +0800109 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500110 self.validate_response(schema.aggregate_add_remove_host, resp, body)
111 return rest_client.ResponseBody(resp, body)
112
113 def remove_host(self, aggregate_id, **kwargs):
114 """Remove a host from the given aggregate.
115
Dong Mad12c2332016-10-19 01:36:27 -0700116 For a full list of available parameters, please refer to the official
117 API reference:
zhufl8d4134b2017-03-08 15:04:20 +0800118 https://developer.openstack.org/api-ref/compute/#remove-host
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500119 """
120 post_body = json.dumps({'remove_host': kwargs})
121 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
122 post_body)
123 body = json.loads(body)
zhufl4af2c822018-08-06 14:38:53 +0800124 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500125 self.validate_response(schema.aggregate_add_remove_host, resp, body)
126 return rest_client.ResponseBody(resp, body)
127
128 def set_metadata(self, aggregate_id, **kwargs):
caojinlaned348852016-06-21 11:59:55 +0800129 """Replace the aggregate's existing metadata with new metadata.
130
Dong Mad12c2332016-10-19 01:36:27 -0700131 For a full list of available parameters, please refer to the official
132 API reference:
zhufl8d4134b2017-03-08 15:04:20 +0800133 https://developer.openstack.org/api-ref/compute/#create-or-update-aggregate-metadata
caojinlaned348852016-06-21 11:59:55 +0800134 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500135 post_body = json.dumps({'set_metadata': kwargs})
136 resp, body = self.post('os-aggregates/%s/action' % aggregate_id,
137 post_body)
138 body = json.loads(body)
zhufl4af2c822018-08-06 14:38:53 +0800139 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -0500140 self.validate_response(schema.aggregate_set_metadata, resp, body)
141 return rest_client.ResponseBody(resp, body)