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 } 126
127 - def __init__(self, resource_root, name=None, type=None, hostRef=None):
128 BaseApiObject.init(self, resource_root, locals())
129
130 - def __str__(self):
131 return "<ApiRole>: %s (cluster: %s; service: %s)" % ( 132 self.name, self.serviceRef.clusterName, self.serviceRef.serviceName)
133
134 - def _path(self):
135 return _get_role_path(self.serviceRef.clusterName, 136 self.serviceRef.serviceName, 137 self.name)
138
139 - def _get_log(self, log):
140 path = "%s/logs/%s" % (self._path(), log) 141 return self._get_resource_root().get(path)
142
143 - def get_commands(self, view=None):
144 """ 145 Retrieve a list of running commands for this role. 146 147 @param view: View to materialize ('full' or 'summary') 148 @return: A list of running commands. 149 """ 150 return self._get("commands", ApiCommand, True, 151 params = view and dict(view=view) or None)
152
153 - def get_config(self, view = None):
154 """ 155 Retrieve the role's configuration. 156 157 The 'summary' view contains strings as the dictionary values. The full 158 view contains ApiConfig instances as the values. 159 160 @param view: View to materialize ('full' or 'summary') 161 @return: Dictionary with configuration data. 162 """ 163 return self._get_config("config", view)
164
165 - def update_config(self, config):
166 """ 167 Update the role's configuration. 168 169 @param config: Dictionary with configuration to update. 170 @return: Dictionary with updated configuration. 171 """ 172 return self._update_config("config", config)
173
174 - def get_full_log(self):
175 """ 176 Retrieve the contents of the role's log file. 177 178 @return: Contents of log file. 179 """ 180 return self._get_log('full')
181
182 - def get_stdout(self):
183 """ 184 Retrieve the contents of the role's standard output. 185 186 @return: Contents of stdout. 187 """ 188 return self._get_log('stdout')
189
190 - def get_stderr(self):
191 """ 192 Retrieve the contents of the role's standard error. 193 194 @return: Contents of stderr. 195 """ 196 return self._get_log('stderr')
197
198 - def get_metrics(self, from_time=None, to_time=None, metrics=None, view=None):
199 """ 200 This endpoint is not supported as of v6. Use the timeseries API 201 instead. To get all metrics for a role with the timeseries API use 202 the query: 203 204 'select * where roleName = $ROLE_NAME'. 205 206 To get specific metrics for a role use a comma-separated list of 207 the metric names as follows: 208 209 'select $METRIC_NAME1, $METRIC_NAME2 where roleName = $ROLE_NAME'. 210 211 For more information see http://tiny.cloudera.com/tsquery_doc 212 @param from_time: A datetime; start of the period to query (optional). 213 @param to_time: A datetime; end of the period to query (default = now). 214 @param metrics: List of metrics to query (default = all). 215 @param view: View to materialize ('full' or 'summary') 216 @return: List of metrics and their readings. 217 """ 218 return self._get_resource_root().get_metrics(self._path() + '/metrics', 219 from_time, to_time, metrics, view)
220
221 - def enter_maintenance_mode(self):
222 """ 223 Put the role in maintenance mode. 224 225 @return: Reference to the completed command. 226 @since: API v2 227 """ 228 cmd = self._cmd('enterMaintenanceMode') 229 if cmd.success: 230 self._update(_get_role(self._get_resource_root(), self._path())) 231 return cmd
232
233 - def exit_maintenance_mode(self):
234 """ 235 Take the role out of maintenance mode. 236 237 @return: Reference to the completed command. 238 @since: API v2 239 """ 240 cmd = self._cmd('exitMaintenanceMode') 241 if cmd.success: 242 self._update(_get_role(self._get_resource_root(), self._path())) 243 return cmd
244
245 - def list_commands_by_name(self):
246 """ 247 Lists all the commands that can be executed by name 248 on the provided role. 249 250 @return: A list of command metadata objects 251 @since: API v6 252 """ 253 return self._get("commandsByName", ApiCommandMetadata, True, api_version=6)
254