1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 """
40
41 RestException.__init__(self, error)
42 try:
43
44 json_body = json.loads(self._message)
45 self._message = json_body['message']
46 except (ValueError, KeyError):
47 pass
48
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
84 """
85 Returns the API version (integer) being used.
86 """
87 return self._version
88
89
90
96
97
98
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
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
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
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
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
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
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
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
177
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
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
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
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
218
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
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
236
237 - def echo(self, message):
238 """Have the server echo a message back."""
239 return tools.echo(self, message)
240
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
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
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
290 """
291 See ApiResource.
292 """
293 return ApiResource(server_host, server_port, username, password, use_tls,
294 version)
295