support all cluster update option
[escalator.git] / client / escalatorclient / v1 / update.py
1 # Copyright 2012 OpenStack Foundation
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 copy
17 import six
18
19 from escalatorclient.common import utils
20 from escalatorclient.openstack.common.apiclient import base
21
22 CREATE_PARAMS = (
23     'cluster_id', 'version_id', 'version_patch_id', 'hosts',
24     'update_object', 'update_script')
25
26 OS_REQ_ID_HDR = 'x-openstack-request-id'
27
28
29 class Update(base.Resource):
30     def __repr__(self):
31         return "<Update %s>" % self._info
32
33     def update(self, **fields):
34         self.manager.update(self, **fields)
35
36     def data(self, **kwargs):
37         return self.manager.data(self, **kwargs)
38
39
40 class UpdateManager(base.ManagerWithFind):
41     resource_class = Update
42
43     def _Update_meta_to_headers(self, fields):
44         headers = {}
45         fields_copy = copy.deepcopy(fields)
46
47         # NOTE(flaper87): Convert to str, headers
48         # that are not instance of basestring. All
49         # headers will be encoded later, before the
50         # request is sent.
51
52         for key, value in six.iteritems(fields_copy):
53             headers['%s' % key] = utils.to_str(value)
54         return headers
55
56     @staticmethod
57     def _format_update_meta_for_user(meta):
58         for key in ['size', 'min_ram', 'min_disk']:
59             if key in meta:
60                 try:
61                     meta[key] = int(meta[key]) if meta[key] else 0
62                 except ValueError:
63                     pass
64         return meta
65
66     def list(self, **kwargs):
67         pass
68
69     def query_progress(self, **kwargs):
70         fields = {}
71         for field in kwargs:
72             if field in CREATE_PARAMS:
73                 fields[field] = kwargs[field]
74             else:
75                 msg = 'update() got an unexpected argument \'%s\''
76                 raise TypeError(msg % field)
77
78             if "cluster" in fields:
79                 url = '/v1/update/%s' % fields['cluster_id']
80
81         resp, body = self.client.get(url)
82         return Update(self, self._format_update_meta_for_user(body))
83
84     def update(self, **kwargs):
85         """Update a cluster
86
87         TODO(bcwaldon): document accepted params
88         """
89
90         fields = {}
91         for field in kwargs:
92             if field in CREATE_PARAMS:
93                 fields[field] = kwargs[field]
94             elif field == 'return_req_id':
95                 continue
96             else:
97                 msg = 'update() got an unexpected argument \'%s\''
98                 raise TypeError(msg % field)
99
100         if "cluster_id" in fields:
101             url = '/v1/update/%s' % fields['cluster_id']
102
103             hdrs = self._Update_meta_to_headers(fields)
104             resp, body = self.client.post(url, headers=None, data=hdrs)
105             return Update(self, self._format_update_meta_for_user(body))