1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 try:
18 import json
19 except ImportError:
20 import simplejson as json
21
22 from cm_api.endpoints.types import *
23
24 __docformat__ = "epytext"
25
26 ROLES_PATH = "/clusters/%s/services/%s/roles"
27 CM_ROLES_PATH = "/cm/service/roles"
28
34
36 path = _get_roles_path(cluster_name, service_name)
37 return "%s/%s" % (path, role_name)
38
39 -def create_role(resource_root,
40 service_name,
41 role_type,
42 role_name,
43 host_id,
44 cluster_name="default"):
45 """
46 Create a role
47 @param resource_root: The root Resource object.
48 @param service_name: Service name
49 @param role_type: Role type
50 @param role_name: Role name
51 @param cluster_name: Cluster name
52 @return: An ApiRole object
53 """
54 apirole = ApiRole(resource_root, role_name, role_type,
55 ApiHostRef(resource_root, host_id))
56 apirole_list = ApiList([apirole])
57 body = json.dumps(apirole_list.to_json_dict())
58 resp = resource_root.post(_get_roles_path(cluster_name, service_name),
59 data=body)
60
61 return ApiList.from_json_dict(ApiRole, resp, resource_root)[0]
62
63 -def get_role(resource_root, service_name, name, cluster_name="default"):
64 """
65 Lookup a role by name
66 @param resource_root: The root Resource object.
67 @param service_name: Service name
68 @param name: Role name
69 @param cluster_name: Cluster name
70 @return: An ApiRole object
71 """
72 return _get_role(resource_root, _get_role_path(cluster_name, service_name, name))
73
77
78 -def get_all_roles(resource_root, service_name, cluster_name="default", view=None):
79 """
80 Get all roles
81 @param resource_root: The root Resource object.
82 @param service_name: Service name
83 @param cluster_name: Cluster name
84 @return: A list of ApiRole objects.
85 """
86 dic = resource_root.get(_get_roles_path(cluster_name, service_name),
87 params=view and dict(view=view) or None)
88 return ApiList.from_json_dict(ApiRole, dic, resource_root)
89
90 -def get_roles_by_type(resource_root, service_name, role_type,
91 cluster_name="default", view=None):
92 """
93 Get all roles of a certain type in a service
94 @param resource_root: The root Resource object.
95 @param service_name: Service name
96 @param role_type: Role type
97 @param cluster_name: Cluster name
98 @return: A list of ApiRole objects.
99 """
100 roles = get_all_roles(resource_root, service_name, cluster_name, view)
101 return [ r for r in roles if r.type == role_type ]
102
103 -def delete_role(resource_root, service_name, name, cluster_name="default"):
104 """
105 Delete a role by name
106 @param resource_root: The root Resource object.
107 @param service_name: Service name
108 @param name: Role name
109 @param cluster_name: Cluster name
110 @return: The deleted ApiRole object
111 """
112 resp = resource_root.delete(_get_role_path(cluster_name, service_name, name))
113 return ApiRole.from_json_dict(resp, resource_root)
114
115
117 _ATTRIBUTES = {
118 'name' : None,
119 'type' : None,
120 'hostRef' : Attr(ApiHostRef),
121 'roleState' : ROAttr(),
122 'healthSummary' : ROAttr(),
123 'healthChecks' : ROAttr(),
124 'serviceRef' : ROAttr(ApiServiceRef),
125 'configStale' : ROAttr(),
126 'haStatus' : ROAttr(),
127 'roleUrl' : ROAttr(),
128 'commissionState' : ROAttr(),
129 'maintenanceMode' : ROAttr(),
130 'maintenanceOwners' : ROAttr(),
131 'roleConfigGroupRef' : ROAttr(ApiRoleConfigGroupRef),
132 }
133
134 - def __init__(self, resource_root, name=None, type=None, hostRef=None):
136
138 return "<ApiRole>: %s (cluster: %s; service: %s)" % (
139 self.name, self.serviceRef.clusterName, self.serviceRef.serviceName)
140
142 return _get_role_path(self.serviceRef.clusterName,
143 self.serviceRef.serviceName,
144 self.name)
145
146 - def _cmd(self, cmd, data=None):
150
154
166
168 """
169 Retrieve the role's configuration.
170
171 The 'summary' view contains strings as the dictionary values. The full
172 view contains ApiConfig instances as the values.
173
174 @param view: View to materialize ('full' or 'summary')
175 @return Dictionary with configuration data.
176 """
177 path = self._path() + '/config'
178 resp = self._get_resource_root().get(path,
179 params = view and dict(view=view) or None)
180 return json_to_config(resp, view == 'full')
181
183 """
184 Update the role's configuration.
185
186 @param config Dictionary with configuration to update.
187 @return Dictionary with updated configuration.
188 """
189 path = self._path() + '/config'
190 resp = self._get_resource_root().put(path, data = config_to_json(config))
191 return json_to_config(resp)
192
194 """
195 Retrieve the contents of the role's log file.
196
197 @return: Contents of log file.
198 """
199 return self._get_log('full')
200
202 """
203 Retrieve the contents of the role's standard output.
204
205 @return: Contents of stdout.
206 """
207 return self._get_log('stdout')
208
210 """
211 Retrieve the contents of the role's standard error.
212
213 @return: Contents of stderr.
214 """
215 return self._get_log('stderr')
216
217 - def get_metrics(self, from_time=None, to_time=None, metrics=None, view=None):
218 """
219 Retrieve metric readings for the role.
220
221 @param from_time: A datetime; start of the period to query (optional).
222 @param to_time: A datetime; end of the period to query (default = now).
223 @param metrics: List of metrics to query (default = all).
224 @param view: View to materialize ('full' or 'summary')
225 @return List of metrics and their readings.
226 """
227 return self._get_resource_root().get_metrics(self._path() + '/metrics',
228 from_time, to_time, metrics, view)
229
231 """
232 Put the role in maintenance mode.
233
234 @return: Reference to the completed command.
235 @since: API v2
236 """
237 cmd = self._cmd('enterMaintenanceMode')
238 if cmd.success:
239 self._update(_get_role(self._get_resource_root(), self._path()))
240 return cmd
241
243 """
244 Take the role out of maintenance mode.
245
246 @return: Reference to the completed command.
247 @since: API v2
248 """
249 cmd = self._cmd('exitMaintenanceMode')
250 if cmd.success:
251 self._update(_get_role(self._get_resource_root(), self._path()))
252 return cmd
253