| Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 1 | # 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 |  | 
 | 16 | import json | 
 | 17 |  | 
 | 18 | from tempest.common.rest_client import RestClient | 
| Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 19 | from tempest import config | 
| Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 20 | from tempest import exceptions | 
 | 21 |  | 
| Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 22 | CONF = config.CONF | 
 | 23 |  | 
| Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 24 |  | 
 | 25 | class AggregatesClientJSON(RestClient): | 
 | 26 |  | 
| Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame^] | 27 |     def __init__(self, auth_provider): | 
 | 28 |         super(AggregatesClientJSON, self).__init__(auth_provider) | 
| Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 29 |         self.service = CONF.compute.catalog_type | 
| Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 30 |  | 
 | 31 |     def list_aggregates(self): | 
 | 32 |         """Get aggregate list.""" | 
 | 33 |         resp, body = self.get("os-aggregates") | 
 | 34 |         body = json.loads(body) | 
 | 35 |         return resp, body['aggregates'] | 
 | 36 |  | 
 | 37 |     def get_aggregate(self, aggregate_id): | 
 | 38 |         """Get details of the given aggregate.""" | 
 | 39 |         resp, body = self.get("os-aggregates/%s" % str(aggregate_id)) | 
 | 40 |         body = json.loads(body) | 
 | 41 |         return resp, body['aggregate'] | 
 | 42 |  | 
| Yuiko Takada | f93d248 | 2014-01-30 16:25:08 +0000 | [diff] [blame] | 43 |     def create_aggregate(self, **kwargs): | 
| Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 44 |         """Creates a new aggregate.""" | 
| Yuiko Takada | f93d248 | 2014-01-30 16:25:08 +0000 | [diff] [blame] | 45 |         post_body = json.dumps({'aggregate': kwargs}) | 
| Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 46 |         resp, body = self.post('os-aggregates', post_body, self.headers) | 
 | 47 |  | 
 | 48 |         body = json.loads(body) | 
 | 49 |         return resp, body['aggregate'] | 
 | 50 |  | 
| Zhu Zhu | 7b5f629 | 2013-09-22 15:47:54 +0800 | [diff] [blame] | 51 |     def update_aggregate(self, aggregate_id, name, availability_zone=None): | 
 | 52 |         """Update a aggregate.""" | 
 | 53 |         put_body = { | 
 | 54 |             'name': name, | 
 | 55 |             'availability_zone': availability_zone | 
 | 56 |         } | 
 | 57 |         put_body = json.dumps({'aggregate': put_body}) | 
 | 58 |         resp, body = self.put('os-aggregates/%s' % str(aggregate_id), | 
 | 59 |                               put_body, self.headers) | 
 | 60 |  | 
 | 61 |         body = json.loads(body) | 
 | 62 |         return resp, body['aggregate'] | 
 | 63 |  | 
| Mitsuhiko Yamazaki | 74f0707 | 2013-04-02 11:52:31 +0900 | [diff] [blame] | 64 |     def delete_aggregate(self, aggregate_id): | 
 | 65 |         """Deletes the given aggregate.""" | 
 | 66 |         return self.delete("os-aggregates/%s" % str(aggregate_id)) | 
 | 67 |  | 
 | 68 |     def is_resource_deleted(self, id): | 
 | 69 |         try: | 
 | 70 |             self.get_aggregate(id) | 
 | 71 |         except exceptions.NotFound: | 
 | 72 |             return True | 
 | 73 |         return False | 
 | 74 |  | 
 | 75 |     def add_host(self, aggregate_id, host): | 
 | 76 |         """Adds a host to the given aggregate.""" | 
 | 77 |         post_body = { | 
 | 78 |             'host': host, | 
 | 79 |         } | 
 | 80 |         post_body = json.dumps({'add_host': post_body}) | 
 | 81 |         resp, body = self.post('os-aggregates/%s/action' % aggregate_id, | 
 | 82 |                                post_body, self.headers) | 
 | 83 |         body = json.loads(body) | 
 | 84 |         return resp, body['aggregate'] | 
 | 85 |  | 
 | 86 |     def remove_host(self, aggregate_id, host): | 
 | 87 |         """Removes a host from the given aggregate.""" | 
 | 88 |         post_body = { | 
 | 89 |             'host': host, | 
 | 90 |         } | 
 | 91 |         post_body = json.dumps({'remove_host': post_body}) | 
 | 92 |         resp, body = self.post('os-aggregates/%s/action' % aggregate_id, | 
 | 93 |                                post_body, self.headers) | 
 | 94 |         body = json.loads(body) | 
 | 95 |         return resp, body['aggregate'] | 
| ivan-zhu | 35e1f8e | 2013-10-18 15:51:16 +0800 | [diff] [blame] | 96 |  | 
 | 97 |     def set_metadata(self, aggregate_id, meta): | 
 | 98 |         """Replaces the aggregate's existing metadata with new metadata.""" | 
 | 99 |         post_body = { | 
 | 100 |             'metadata': meta, | 
 | 101 |         } | 
 | 102 |         post_body = json.dumps({'set_metadata': post_body}) | 
 | 103 |         resp, body = self.post('os-aggregates/%s/action' % aggregate_id, | 
 | 104 |                                post_body, self.headers) | 
 | 105 |         body = json.loads(body) | 
 | 106 |         return resp, body['aggregate'] |