Skip to content

Commit 8f0b0aa

Browse files
author
Jayesh Seshadri
committed
[scm] OPSAPS-35700 Add external accounts to python API.
Identical modeling to the final API model in OPSAPS-34817. Testing: - test scripts to test each of the APIs separately against a recent server - new unit tests Sample python script: ---- from cm_api.api_client import ApiResource, ApiException, API_CURRENT_VERSION api = ApiResource(server_host="test-1.cloudera.com", username="admin", password="admin", version=14) cats = api.get_supported_external_account_categories() print "cat: %s" %(cats) for cat in cats: types = api.get_supported_external_account_types(cat.name) for type_ in types: print "type: %s, allowed: %s" %(type_.name, type_.allowedAccountConfigs) name = "test7" acct = api.create_external_account(name=name, display_name=name, type_name="AWS_ACCESS_KEY_AUTH") print "Created: %s" % acct acct.accountConfigs['aws_access_key'] = "test1" acct.accountConfigs['aws_secret_key'] = "test2" acct_updated = api.update_external_account(acct) print "Updated: %s with configs: %s" % (acct_updated, acct_updated.accountConfigs) acct_deleted = api.delete_external_account(acct.name) print "Deleted: %s" % acct_deleted ---
1 parent 2c05d9c commit 8f0b0aa

File tree

3 files changed

+409
-1
lines changed

3 files changed

+409
-1
lines changed

python/src/cm_api/api_client.py

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import simplejson as json
2222

2323
from cm_api.http_client import HttpClient, RestException
24-
from cm_api.endpoints import batch, cms, clusters, events, hosts, tools
24+
from cm_api.endpoints import batch, cms, clusters, events, hosts, external_accounts, tools
2525
from cm_api.endpoints import types, users, timeseries
2626
from cm_api.resource import Resource
2727

@@ -309,6 +309,83 @@ def do_batch(self, elements):
309309
"""
310310
return batch.do_batch(self, elements)
311311

312+
def get_supported_external_account_categories(self):
313+
"""
314+
Lookup all supported categories.
315+
@return: An ApiExternalAcccountCategory list
316+
"""
317+
return external_accounts.get_supported_categories(self)
318+
319+
def get_supported_external_account_types(self, category_name):
320+
"""
321+
Lookup all supported types in a category.
322+
@param category_name: The category name
323+
@return: An ApiExternalAcccountType list
324+
"""
325+
return external_accounts.get_supported_types(self, category_name)
326+
327+
def create_external_account(self, name, display_name, type_name,
328+
account_configs=None):
329+
"""
330+
Create an external account
331+
@param name: Immutable external account name
332+
@param display_name: Display name
333+
@param type_name: Account type
334+
@param account_configs: Optional account configuration
335+
@return: An ApiExternalAccount object
336+
"""
337+
return external_accounts.create_external_account(
338+
self, name, display_name, type_name, account_configs)
339+
340+
341+
def get_external_account(self, name, view=None):
342+
"""
343+
Lookup an external account by name
344+
@param name: Account name
345+
@param view: View
346+
@return: An ApiExternalAccount object
347+
"""
348+
return external_accounts.get_external_account(
349+
self, name, view)
350+
351+
352+
def get_external_account_by_display_name(
353+
self, display_name, view=None):
354+
"""
355+
Lookup an external account by display name
356+
@param display_name: Account display name
357+
@param view: View
358+
@return: An ApiExternalAccount object
359+
"""
360+
return external_accounts.get_external_account_by_display_name(
361+
self, display_name, view)
362+
363+
def get_all_external_accounts(self, type_name, view=None):
364+
"""
365+
Lookup all external accounts of a particular type, by type name.
366+
@param type_name: Type name
367+
@param view: View
368+
@return: A list of ApiExternalAccount objects.
369+
"""
370+
return external_accounts.get_all_external_accounts(
371+
self, type_name, view)
372+
373+
def update_external_account(self, account):
374+
"""
375+
Update an external account
376+
@param account: Account to update, account name must be specified.
377+
@return: An ApiExternalAccount object
378+
"""
379+
return external_accounts.update_external_account(self, account)
380+
381+
def delete_external_account(self, name):
382+
"""
383+
Delete an external account by name
384+
@param name: Account name
385+
@return: An ApiExternalAccount object
386+
"""
387+
return external_accounts.delete_external_account(self, name)
388+
312389
def get_root_resource(server_host, server_port=None,
313390
username="admin", password="admin",
314391
use_tls=False, version=API_CURRENT_VERSION):
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
EXTERNAL_ACCOUNT_PATH = "/externalAccounts/%s"
27+
EXTERNAL_ACCOUNT_FETCH_PATH = "/externalAccounts/%s/%s"
28+
29+
def get_supported_categories(resource_root):
30+
"""
31+
Lookup all supported categories.
32+
@param resource_root: The root Resource object.
33+
@return: An ApiExternalAcccountCategory list
34+
"""
35+
return call(resource_root.get, EXTERNAL_ACCOUNT_PATH % ("supportedCategories",) ,
36+
ApiExternalAccountCategory, True)
37+
38+
def get_supported_types(resource_root, category_name):
39+
"""
40+
Lookup all supported types in a category.
41+
@param resource_root: The root Resource object.
42+
@param category_name: The category name
43+
@return: An ApiExternalAcccountType list
44+
"""
45+
return call(resource_root.get,
46+
EXTERNAL_ACCOUNT_FETCH_PATH % ("supportedTypes", category_name,),
47+
ApiExternalAccountType, True)
48+
49+
def create_external_account(resource_root, name, display_name, type_name,
50+
account_configs=None):
51+
"""
52+
Create an external account
53+
@param resource_root: The root Resource object.
54+
@param name: Immutable external account name
55+
@param display_name: Display name
56+
@param type_name: Account type
57+
@param account_configs: Optional account configuration (ApiList of ApiConfig objects)
58+
@return: An ApiExternalAccount object matching the newly created account
59+
"""
60+
account = ApiExternalAccount(resource_root,
61+
name=name,
62+
displayName=display_name,
63+
typeName=type_name,
64+
accountConfigs=account_configs)
65+
return call(resource_root.post,
66+
EXTERNAL_ACCOUNT_PATH % ("create",),
67+
ApiExternalAccount, False, data=account)
68+
69+
def get_external_account(resource_root, name, view=None):
70+
"""
71+
Lookup an external account by name
72+
@param resource_root: The root Resource object.
73+
@param name: Account name
74+
@param view: View
75+
@return: An ApiExternalAccount object
76+
"""
77+
return call(resource_root.get,
78+
EXTERNAL_ACCOUNT_FETCH_PATH % ("account", name,),
79+
ApiExternalAccount, False, params=view and dict(view=view) or None)
80+
81+
def get_external_account_by_display_name(resource_root,
82+
display_name, view=None):
83+
"""
84+
Lookup an external account by display name
85+
@param resource_root: The root Resource object.
86+
@param display_name: Account display name
87+
@param view: View
88+
@return: An ApiExternalAccount object
89+
"""
90+
return call(resource_root.get,
91+
EXTERNAL_ACCOUNT_FETCH_PATH % ("accountByDisplayName", display_name,),
92+
ApiExternalAccount, False, params=view and dict(view=view) or None)
93+
94+
def get_all_external_accounts(resource_root, type_name, view=None):
95+
"""
96+
Lookup all external accounts of a particular type, by type name.
97+
@param resource_root: The root Resource object.
98+
@param type_name: Type name
99+
@param view: View
100+
@return: An ApiList of ApiExternalAccount objects matching the specified type
101+
"""
102+
return call(resource_root.get,
103+
EXTERNAL_ACCOUNT_FETCH_PATH % ("type", type_name,),
104+
ApiExternalAccount, True, params=view and dict(view=view) or None)
105+
106+
def update_external_account(resource_root, account):
107+
"""
108+
Update an external account
109+
@param resource_root: The root Resource object.
110+
@param account: Account to update, account name must be specified.
111+
@return: An ApiExternalAccount object, representing the updated external account
112+
"""
113+
return call(resource_root.put,
114+
EXTERNAL_ACCOUNT_PATH % ("update",),
115+
ApiExternalAccount, False, data=account)
116+
117+
def delete_external_account(resource_root, name):
118+
"""
119+
Delete an external account by name
120+
@param resource_root: The root Resource object.
121+
@param name: Account name
122+
@return: The deleted ApiExternalAccount object
123+
"""
124+
return call(resource_root.delete,
125+
EXTERNAL_ACCOUNT_FETCH_PATH % ("delete", name,),
126+
ApiExternalAccount, False)
127+
128+
129+
class ApiExternalAccountCategory(BaseApiObject):
130+
_ATTRIBUTES = {
131+
'name' : None,
132+
'displayName' : None,
133+
'description' : None
134+
}
135+
136+
def __str__(self):
137+
return "<ApiExternalAccountCategory>: %s" % (
138+
self.name)
139+
140+
class ApiExternalAccountType(BaseApiObject):
141+
_ATTRIBUTES = {
142+
'name' : None,
143+
'displayName' : None,
144+
'type' : None,
145+
'categoryName' : None,
146+
'description' : None,
147+
'allowedAccountConfigs' : Attr(ApiConfig)
148+
}
149+
150+
def __str__(self):
151+
return "<ApiExternalAccountType>: %s (categoryName: %s)" % (
152+
self.name, self.typeName)
153+
154+
class ApiExternalAccount(BaseApiObject):
155+
_ATTRIBUTES = {
156+
'name' : None,
157+
'displayName' : None,
158+
'typeName' : None,
159+
'createdTime' : ROAttr(),
160+
'lastModifiedTime' : ROAttr(),
161+
'accountConfigs' : Attr(ApiConfig)
162+
}
163+
164+
def __init__(self, resource_root, name=None, displayName=None,
165+
typeName=None, accountConfigs=None):
166+
BaseApiObject.init(self, resource_root, locals())
167+
168+
def __str__(self):
169+
return "<ApiExternalAccount>: %s (typeName: %s)" % (
170+
self.name, self.typeName)
171+

0 commit comments

Comments
 (0)