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  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   
29 -def _get_roles_path(cluster_name, service_name):
30 if cluster_name: 31 return ROLES_PATH % (cluster_name, service_name) 32 else: 33 return CM_ROLES_PATH
34
35 -def _get_role_path(cluster_name, service_name, role_name):
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 # The server returns a list of created roles (with size 1) 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
74 -def _get_role(resource_root, path):
75 dic = resource_root.get(path) 76 return ApiRole.from_json_dict(dic, resource_root)
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
116 -class ApiRole(BaseApiObject):
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):
135 BaseApiObject.init(self, resource_root, locals())
136
137 - def __str__(self):
138 return "<ApiRole>: %s (cluster: %s; service: %s)" % ( 139 self.name, self.serviceRef.clusterName, self.serviceRef.serviceName)
140
141 - def _path(self):
142 return _get_role_path(self.serviceRef.clusterName, 143 self.serviceRef.serviceName, 144 self.name)
145
146 - def _cmd(self, cmd, data=None):
147 path = self._path() + '/commands/' + cmd 148 resp = self._get_resource_root().post(path, data=data) 149 return ApiCommand.from_json_dict(resp, self._get_resource_root())
150
151 - def _get_log(self, log):
152 path = "%s/logs/%s" % (self._path(), log) 153 return self._get_resource_root().get(path)
154
155 - def get_commands(self, view=None):
156 """ 157 Retrieve a list of running commands for this role. 158 159 @param view: View to materialize ('full' or 'summary') 160 @return: A list of running commands. 161 """ 162 resp = self._get_resource_root().get( 163 self._path() + '/commands', 164 params = view and dict(view=view) or None) 165 return ApiList.from_json_dict(ApiCommand, resp, self._get_resource_root())
166
167 - def get_config(self, view = None):
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
182 - def update_config(self, config):
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
193 - def get_full_log(self):
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
201 - def get_stdout(self):
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
209 - def get_stderr(self):
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
230 - def enter_maintenance_mode(self):
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
242 - def exit_maintenance_mode(self):
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