Package cm_api :: Package endpoints :: Module roles
[hide private]
[frames] | no frames]

Source Code for Module cm_api.endpoints.roles

  1  # Licensed to Cloudera, Inc. under one 
  2  # or more contributor license agreements.  See the NOTICE file 
  3  # distributed with this work for additional information 
  4  # regarding copyright ownership.  Cloudera, Inc. licenses this file 
  5  # to you under the Apache License, Version 2.0 (the 
  6  # "License"); you may not use this file except in compliance 
  7  # with the License.  You may obtain a copy of the License at 
  8  # 
  9  #     http://www.apache.org/licenses/LICENSE-2.0 
 10  # 
 11  # Unless required by applicable law or agreed to in writing, software 
 12  # distributed under the License is distributed on an "AS IS" BASIS, 
 13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 14  # See the License for the specific language governing permissions and 
 15  # limitations under the License. 
 16   
 17  from cm_api.endpoints.types import * 
 18   
 19  __docformat__ = "epytext" 
 20   
 21  ROLES_PATH = "/clusters/%s/services/%s/roles" 
 22  CM_ROLES_PATH = "/cm/service/roles" 
 23   
24 -def _get_roles_path(cluster_name, service_name):
25 if cluster_name: 26 return ROLES_PATH % (cluster_name, service_name) 27 else: 28 return CM_ROLES_PATH
29
30 -def _get_role_path(cluster_name, service_name, role_name):
31 path = _get_roles_path(cluster_name, service_name) 32 return "%s/%s" % (path, role_name)
33
34 -def create_role(resource_root, 35 service_name, 36 role_type, 37 role_name, 38 host_id, 39 cluster_name="default"):
40 """ 41 Create a role 42 @param resource_root: The root Resource object. 43 @param service_name: Service name 44 @param role_type: Role type 45 @param role_name: Role name 46 @param cluster_name: Cluster name 47 @return: An ApiRole object 48 """ 49 apirole = ApiRole(resource_root, role_name, role_type, 50 ApiHostRef(resource_root, host_id)) 51 return call(resource_root.post, 52 _get_roles_path(cluster_name, service_name), 53 ApiRole, True, data=[apirole])[0]
54
55 -def get_role(resource_root, service_name, name, cluster_name="default"):
56 """ 57 Lookup a role by name 58 @param resource_root: The root Resource object. 59 @param service_name: Service name 60 @param name: Role name 61 @param cluster_name: Cluster name 62 @return: An ApiRole object 63 """ 64 return _get_role(resource_root, _get_role_path(cluster_name, service_name, name))
65
66 -def _get_role(resource_root, path):
67 return call(resource_root.get, path, ApiRole)
68
69 -def get_all_roles(resource_root, service_name, cluster_name="default", view=None):
70 """ 71 Get all roles 72 @param resource_root: The root Resource object. 73 @param service_name: Service name 74 @param cluster_name: Cluster name 75 @return: A list of ApiRole objects. 76 """ 77 return call(resource_root.get, 78 _get_roles_path(cluster_name, service_name), 79 ApiRole, True, params=view and dict(view=view) or None)
80
81 -def get_roles_by_type(resource_root, service_name, role_type, 82 cluster_name="default", view=None):
83 """ 84 Get all roles of a certain type in a service 85 @param resource_root: The root Resource object. 86 @param service_name: Service name 87 @param role_type: Role type 88 @param cluster_name: Cluster name 89 @return: A list of ApiRole objects. 90 """ 91 roles = get_all_roles(resource_root, service_name, cluster_name, view) 92 return [ r for r in roles if r.type == role_type ]
93
94 -def delete_role(resource_root, service_name, name, cluster_name="default"):
95 """ 96 Delete a role by name 97 @param resource_root: The root Resource object. 98 @param service_name: Service name 99 @param name: Role name 100 @param cluster_name: Cluster name 101 @return: The deleted ApiRole object 102 """ 103 return call(resource_root.delete, 104 _get_role_path(cluster_name, service_name, name), ApiRole)
105 106
107 -class ApiRole(BaseApiResource):
108 _ATTRIBUTES = { 109 'name' : None, 110 'type' : None, 111 'hostRef' : Attr(ApiHostRef), 112 'roleState' : ROAttr(), 113 'healthSummary' : ROAttr(), 114 'healthChecks' : ROAttr(), 115 'serviceRef' : ROAttr(ApiServiceRef), 116 'configStale' : ROAttr(), 117 'configStalenessStatus' : ROAttr(), 118 'haStatus' : ROAttr(), 119 'roleUrl' : ROAttr(), 120 'commissionState' : ROAttr(), 121 'maintenanceMode' : ROAttr(), 122 'maintenanceOwners' : ROAttr(), 123 'roleConfigGroupRef' : ROAttr(ApiRoleConfigGroupRef), 124 'zooKeeperServerMode' : ROAttr(), 125 'entityStatus' : ROAttr(), 126 } 127
128 - def __init__(self, resource_root, name=None, type=None, hostRef=None):
129 BaseApiObject.init(self, resource_root, locals())
130
131 - def __str__(self):
132 return "<ApiRole>: %s (cluster: %s; service: %s)" % ( 133 self.name, self.serviceRef.clusterName, self.serviceRef.serviceName)
134
135 - def _path(self):
136 return _get_role_path(self.serviceRef.clusterName, 137 self.serviceRef.serviceName, 138 self.name)
139
140 - def _get_log(self, log):
141 path = "%s/logs/%s" % (self._path(), log) 142 return self._get_resource_root().get(path)
143
144 - def get_commands(self, view=None):
145 """ 146 Retrieve a list of running commands for this role. 147 148 @param view: View to materialize ('full' or 'summary') 149 @return: A list of running commands. 150 """ 151 return self._get("commands", ApiCommand, True, 152 params = view and dict(view=view) or None)
153
154 - def get_config(self, view = None):
155 """ 156 Retrieve the role's configuration. 157 158 The 'summary' view contains strings as the dictionary values. The full 159 view contains ApiConfig instances as the values. 160 161 @param view: View to materialize ('full' or 'summary') 162 @return: Dictionary with configuration data. 163 """ 164 return self._get_config("config", view)
165
166 - def update_config(self, config):
167 """ 168 Update the role's configuration. 169 170 @param config: Dictionary with configuration to update. 171 @return: Dictionary with updated configuration. 172 """ 173 return self._update_config("config", config)
174
175 - def get_full_log(self):
176 """ 177 Retrieve the contents of the role's log file. 178 179 @return: Contents of log file. 180 """ 181 return self._get_log('full')
182
183 - def get_stdout(self):
184 """ 185 Retrieve the contents of the role's standard output. 186 187 @return: Contents of stdout. 188 """ 189 return self._get_log('stdout')
190
191 - def get_stderr(self):
192 """ 193 Retrieve the contents of the role's standard error. 194 195 @return: Contents of stderr. 196 """ 197 return self._get_log('stderr')
198
199 - def get_stacks_log(self):
200 """ 201 Retrieve the contents of the role's stacks log file. 202 203 @return: Contents of stacks log file. 204 @since: API v8 205 """ 206 return self._get_log('stacks')
207
208 - def get_stacks_logs_bundle(self):
209 """ 210 Retrieve a zip file of the role's stacks log files. 211 212 @return: A zipfile of stacks log files. 213 @since: API v8 214 """ 215 return self._get_log('stacksBundle')
216
217 - def get_metrics(self, from_time=None, to_time=None, metrics=None, view=None):
218 """ 219 This endpoint is not supported as of v6. Use the timeseries API 220 instead. To get all metrics for a role with the timeseries API use 221 the query: 222 223 'select * where roleName = $ROLE_NAME'. 224 225 To get specific metrics for a role use a comma-separated list of 226 the metric names as follows: 227 228 'select $METRIC_NAME1, $METRIC_NAME2 where roleName = $ROLE_NAME'. 229 230 For more information see http://tiny.cloudera.com/tsquery_doc 231 @param from_time: A datetime; start of the period to query (optional). 232 @param to_time: A datetime; end of the period to query (default = now). 233 @param metrics: List of metrics to query (default = all). 234 @param view: View to materialize ('full' or 'summary') 235 @return: List of metrics and their readings. 236 """ 237 return self._get_resource_root().get_metrics(self._path() + '/metrics', 238 from_time, to_time, metrics, view)
239
240 - def enter_maintenance_mode(self):
241 """ 242 Put the role in maintenance mode. 243 244 @return: Reference to the completed command. 245 @since: API v2 246 """ 247 cmd = self._cmd('enterMaintenanceMode') 248 if cmd.success: 249 self._update(_get_role(self._get_resource_root(), self._path())) 250 return cmd
251
252 - def exit_maintenance_mode(self):
253 """ 254 Take the role out of maintenance mode. 255 256 @return: Reference to the completed command. 257 @since: API v2 258 """ 259 cmd = self._cmd('exitMaintenanceMode') 260 if cmd.success: 261 self._update(_get_role(self._get_resource_root(), self._path())) 262 return cmd
263
264 - def list_commands_by_name(self):
265 """ 266 Lists all the commands that can be executed by name 267 on the provided role. 268 269 @return: A list of command metadata objects 270 @since: API v6 271 """ 272 return self._get("commandsByName", ApiCommandMetadata, True, api_version=6)
273