Package cm_api :: Module api_client
[hide private]
[frames] | no frames]

Source Code for Module cm_api.api_client

  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  import logging 
 18  try: 
 19    import json 
 20  except ImportError: 
 21    import simplejson as json 
 22   
 23  from cm_api.http_client import HttpClient, RestException 
 24  from cm_api.endpoints import cms, clusters, events, hosts, tools, types, users, timeseries 
 25  from cm_api.resource import Resource 
 26   
 27  __docformat__ = "epytext" 
 28   
 29  LOG = logging.getLogger(__name__) 
 30   
 31  API_AUTH_REALM = "Cloudera Manager" 
 32  API_CURRENT_VERSION = 5 
33 34 -class ApiException(RestException):
35 """ 36 Any error result from the API is converted into this exception type. 37 This handles errors from the HTTP level as well as the API level. 38 """
39 - def __init__(self, error):
40 # The parent class will set up _code and _message 41 RestException.__init__(self, error) 42 try: 43 # See if the body is json 44 json_body = json.loads(self._message) 45 self._message = json_body['message'] 46 except (ValueError, KeyError): 47 pass # Ignore json parsing error
48
49 50 -class ApiResource(Resource):
51 """ 52 Resource object that provides methods for managing the top-level API resources. 53 """ 54
55 - def __init__(self, server_host, server_port=None, 56 username="admin", password="admin", 57 use_tls=False, version=API_CURRENT_VERSION):
58 """ 59 Creates a Resource object that provides API endpoints. 60 61 @param server_host: The hostname of the Cloudera Manager server. 62 @param server_port: The port of the server. Defaults to 7180 (http) or 63 7183 (https). 64 @param username: Login name. 65 @param password: Login password. 66 @param use_tls: Whether to use tls (https). 67 @param version: API version. 68 @return Resource object referring to the root. 69 """ 70 self._version = version 71 protocol = use_tls and "https" or "http" 72 if server_port is None: 73 server_port = use_tls and 7183 or 7180 74 base_url = "%s://%s:%s/api/v%s" % \ 75 (protocol, server_host, server_port, version) 76 77 client = HttpClient(base_url, exc_class=ApiException) 78 client.set_basic_auth(username, password, API_AUTH_REALM) 79 client.set_headers( { "Content-Type" : "application/json" } ) 80 Resource.__init__(self, client)
81 82 @property
83 - def version(self):
84 """ 85 Returns the API version (integer) being used. 86 """ 87 return self._version
88 89 # CMS ops. 90
91 - def get_cloudera_manager(self):
92 """ 93 Returns a Cloudera Manager object. 94 """ 95 return cms.ClouderaManager(self)
96 97 # Cluster ops. 98
99 - def create_cluster(self, name, version):
100 """ 101 Create a new cluster. 102 103 @param name Cluster name. 104 @param version Cluster CDH version. 105 @return The created cluster. 106 """ 107 return clusters.create_cluster(self, name, version)
108
109 - def delete_cluster(self, name):
110 """ 111 Delete a cluster by name. 112 113 @param name: Cluster name 114 @return: The deleted ApiCluster object 115 """ 116 return clusters.delete_cluster(self, name)
117
118 - def get_all_clusters(self, view = None):
119 """ 120 Retrieve a list of all clusters. 121 @param view View to materialize ('full' or 'summary'). 122 @return: A list of ApiCluster objects. 123 """ 124 return clusters.get_all_clusters(self, view)
125
126 - def get_cluster(self, name):
127 """ 128 Look up a cluster by name. 129 130 @param name Cluster name. 131 @return An ApiCluster object. 132 """ 133 return clusters.get_cluster(self, name)
134 135 # Host ops. 136
137 - def create_host(self, host_id, name, ipaddr, rack_id = None):
138 """ 139 Create a host. 140 141 @param host_id The host id. 142 @param name Host name 143 @param ipaddr IP address 144 @param rack_id Rack id. Default None. 145 @return: An ApiHost object 146 """ 147 return hosts.create_host(self, host_id, name, ipaddr, rack_id)
148
149 - def delete_host(self, host_id):
150 """ 151 Delete a host by id. 152 153 @param host_id Host id 154 @return The deleted ApiHost object 155 """ 156 return hosts.delete_host(self, host_id)
157
158 - def get_all_hosts(self, view = None):
159 """ 160 Get all hosts 161 162 @param view View to materialize ('full' or 'summary'). 163 @return A list of ApiHost objects. 164 """ 165 return hosts.get_all_hosts(self, view)
166
167 - def get_host(self, host_id):
168 """ 169 Look up a host by id. 170 171 @param host_id Host id 172 @return: An ApiHost object 173 """ 174 return hosts.get_host(self, host_id)
175 176 # Users 177
178 - def get_all_users(self, view = None):
179 """ 180 Get all users. 181 182 @param view: View to materialize ('full' or 'summary'). 183 @return: A list of ApiUser objects. 184 """ 185 return users.get_all_users(self, view)
186
187 - def get_user(self, username):
188 """ 189 Look up a user by username. 190 191 @param username: Username to look up 192 @return: An ApiUser object 193 """ 194 return users.get_user(self, username)
195
196 - def create_user(self, username, password, roles):
197 """ 198 Create a user. 199 200 @param username: Username 201 @param password: Password 202 @param roles: List of roles for the user. This should be [] for a 203 regular user, or ['ROLE_ADMIN'] for an admin. 204 @return: An ApiUser object 205 """ 206 return users.create_user(self, username, password, roles)
207
208 - def delete_user(self, username):
209 """ 210 Delete user by username. 211 212 @param: username Username 213 @return: An ApiUser object 214 """ 215 return users.delete_user(self, username)
216 217 # Events 218
219 - def query_events(self, query_str = None):
220 """ 221 Query events. 222 @param query_str: Query string. 223 @return: A list of ApiEvent. 224 """ 225 return events.query_events(self, query_str)
226
227 - def get_event(self, event_id):
228 """ 229 Retrieve a particular event by ID. 230 @param event_id: The event ID. 231 @return: An ApiEvent. 232 """ 233 return events.get_event(self, event_id)
234 235 # Tools 236
237 - def echo(self, message):
238 """Have the server echo a message back.""" 239 return tools.echo(self, message)
240
241 - def echo_error(self, message):
242 """Generate an error, but we get to set the error message.""" 243 return tools.echo_error(self, message)
244 245
246 - def get_metrics(self, path, from_time, to_time, metrics, view, params=None):
247 """ 248 Generic function for querying metrics. 249 250 @param from_time: A datetime; start of the period to query (optional). 251 @param to_time: A datetime; end of the period to query (default = now). 252 @param metrics: List of metrics to query (default = all). 253 @param view: View to materialize ('full' or 'summary') 254 @param params: Other query parameters. 255 @return List of metrics and their readings. 256 """ 257 if not params: 258 params = { } 259 if from_time: 260 params['from'] = from_time.isoformat() 261 if to_time: 262 params['to'] = to_time.isoformat() 263 if metrics: 264 params['metrics'] = metrics 265 if view: 266 params['view'] = view 267 resp = self.get(path, params=params) 268 return types.ApiList.from_json_dict(types.ApiMetric, resp, self)
269
270 - def query_timeseries(self, query, from_time=None, to_time=None):
271 """ 272 Query time series. 273 @param query: Query string. 274 @param from_time: Start of the period to query (optional). 275 @param to_time: End of the period to query (default = now). 276 @return: A list of ApiTimeSeriesResponse. 277 """ 278 return timeseries.query_timeseries(self, query, from_time, to_time)
279
280 - def get_metric_schema(self):
281 """ 282 Get the schema for all of the metrics. 283 @return: A list of ApiMetricSchema. 284 """ 285 return timeseries.get_metric_schema(self)
286
287 -def get_root_resource(server_host, server_port=None, 288 username="admin", password="admin", 289 use_tls=False, version=API_CURRENT_VERSION):
290 """ 291 See ApiResource. 292 """ 293 return ApiResource(server_host, server_port, username, password, use_tls, 294 version)
295