Skip to content

Commit e544e05

Browse files
committed
refactor tenant
1 parent b576809 commit e544e05

File tree

2 files changed

+212
-196
lines changed

2 files changed

+212
-196
lines changed

bin/cli.py

Lines changed: 56 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -34,209 +34,69 @@
3434

3535
from jinja2 import Environment
3636
import six
37-
import traceback
3837
import yaml
39-
import wrapt
40-
41-
from hpecp import (
42-
APIException,
43-
APIItemConflictException,
44-
ContainerPlatformClient,
45-
ContainerPlatformClientException,
46-
)
38+
4739
from hpecp.logger import Logger
48-
from hpecp.exceptions import (
49-
APIForbiddenException,
50-
APIItemNotFoundException,
51-
APIUnknownException,
52-
)
5340
from textwrap import dedent
5441
import inspect
5542
import collections
56-
from hpecp.tenant import Tenant
5743
from hpecp.user import User
5844
from hpecp.role import Role
5945
from hpecp.cli import base
6046
from hpecp.cli.catalog import CatalogProxy
6147
from hpecp.cli.gateway import GatewayProxy
6248
from hpecp.cli.k8sworker import K8sWorkerProxy
6349
from hpecp.cli.k8scluster import K8sClusterProxy
50+
from hpecp.cli.tenant import TenantProxy
51+
from hpecp import ContainerPlatformClient
52+
6453

6554
if sys.version_info[0] >= 3:
6655
unicode = str
6756

6857
_log = Logger.get_logger()
6958

7059

71-
@wrapt.decorator
72-
def intercept_exception(wrapped, instance, args, kwargs):
73-
"""Handle Exceptions.""" # noqa: D202
74-
75-
def _unknown_exception_handler(ex):
76-
"""Handle unknown exceptions."""
77-
if _log.level == 10: # "DEBUG"
78-
print(
79-
"Unknown error.", file=sys.stderr,
80-
)
81-
else:
82-
print(
83-
"Unknown error. To debug run with env var LOG_LEVEL=DEBUG",
84-
file=sys.stderr,
85-
)
86-
tb = traceback.format_exc()
87-
_log.debug(tb)
88-
_log.debug(ex)
89-
sys.exit(1)
90-
91-
try:
92-
return wrapped(*args, **kwargs)
93-
except SystemExit as se:
94-
sys.exit(se.code)
95-
except AssertionError as ae:
96-
print(ae, file=sys.stderr)
97-
sys.exit(1)
98-
except APIUnknownException as ue:
99-
_unknown_exception_handler(ue)
100-
except (
101-
APIException,
102-
APIItemNotFoundException,
103-
APIItemConflictException,
104-
APIForbiddenException,
105-
ContainerPlatformClientException,
106-
) as e:
107-
print(e.message, file=sys.stderr)
108-
sys.exit(1)
109-
except Exception as ex:
110-
_unknown_exception_handler(ex)
111-
112-
113-
class TenantProxy(base.BaseProxy):
114-
"""Proxy object to :py:attr:`<hpecp.client.tenant>`."""
115-
116-
def __dir__(self):
117-
"""Return the CLI method names."""
118-
return [
119-
"add_external_user_group",
120-
"assign_user_to_role",
121-
"create",
122-
"delete",
123-
"delete_external_user_group",
124-
"examples",
125-
"get",
126-
"get_external_user_groups",
127-
"k8skubeconfig",
128-
"list",
129-
# "status", # TODO: implement me!
130-
"users",
131-
"wait_for_status",
132-
]
133-
134-
def __init__(self):
135-
"""Create instance of proxy class with the client module name."""
136-
super(TenantProxy, self).new_instance("tenant", Tenant)
137-
138-
@intercept_exception
139-
def create(
140-
self,
141-
name=None,
142-
description=None,
143-
tenant_type=None,
144-
k8s_cluster_id=None,
145-
):
146-
"""Create a tenant.
147-
148-
Parameters
149-
----------
150-
name : [type], optional
151-
[description], by default None
152-
description : [type], optional
153-
[description], by default None
154-
tenant_type : [type], optional
155-
[description], by default None
156-
k8s_cluster_id : [type], optional
157-
[description], by default None
158-
"""
159-
tenant_id = base.get_client().tenant.create(
160-
name=name,
161-
description=description,
162-
tenant_type=tenant_type,
163-
k8s_cluster=k8s_cluster_id,
164-
)
165-
print(tenant_id)
166-
167-
def examples(self):
168-
"""Show usage_examples of the list method."""
169-
print(
170-
dedent(
171-
"""\
172-
173-
hpecp tenant list --query "[?tenant_type == 'k8s']" --output json-pp
174-
175-
""" # noqa: E501
176-
)
177-
)
178-
179-
@intercept_exception
180-
def k8skubeconfig(self):
181-
"""Retrieve the tenant kubeconfig.
182-
183-
This requires the ContainerPlatformClient to be created with
184-
a 'tenant' parameter.
185-
186-
Returns
187-
-------
188-
str
189-
Tenant KubeConfig
190-
"""
191-
conf = base.get_client().tenant.k8skubeconfig()
192-
print(conf)
193-
194-
@intercept_exception
195-
def users(self, id, output="table", columns="ALL", query={}):
196-
"""Retrieve users assigned to tenant.
197-
198-
Parameters
199-
----------
200-
id : str
201-
The tenant ID.
202-
"""
203-
list_instance = base.get_client().tenant.users(id=id)
204-
self.print_list(
205-
list_instance=list_instance,
206-
output=output,
207-
columns=columns,
208-
query=query,
209-
)
210-
211-
@intercept_exception
212-
def assign_user_to_role(self, tenant_id, user_id, role_id):
213-
"""Assign user to role in tenant."""
214-
base.get_client().tenant.assign_user_to_role(
215-
tenant_id=tenant_id, user_id=user_id, role_id=role_id
216-
)
217-
218-
@intercept_exception
219-
def get_external_user_groups(self, tenant_id):
220-
"""Retrieve External User Groups."""
221-
print(
222-
base.get_client().tenant.get_external_user_groups(
223-
tenant_id=tenant_id
224-
)
225-
)
226-
227-
@intercept_exception
228-
def add_external_user_group(self, tenant_id, group, role_id):
229-
"""Add External User Group."""
230-
base.get_client().tenant.add_external_user_group(
231-
tenant_id=tenant_id, group=group, role_id=role_id
232-
)
233-
234-
@intercept_exception
235-
def delete_external_user_group(self, tenant_id, group):
236-
"""Delete External User Group."""
237-
base.get_client().tenant.delete_external_user_group(
238-
tenant_id=tenant_id, group=group
239-
)
60+
# @wrapt.decorator
61+
# def intercept_exception(wrapped, instance, args, kwargs):
62+
# """Handle Exceptions.""" # noqa: D202
63+
64+
# def _unknown_exception_handler(ex):
65+
# """Handle unknown exceptions."""
66+
# if _log.level == 10: # "DEBUG"
67+
# print(
68+
# "Unknown error.", file=sys.stderr,
69+
# )
70+
# else:
71+
# print(
72+
# "Unknown error. To debug run with env var LOG_LEVEL=DEBUG",
73+
# file=sys.stderr,
74+
# )
75+
# tb = traceback.format_exc()
76+
# _log.debug(tb)
77+
# _log.debug(ex)
78+
# sys.exit(1)
79+
80+
# try:
81+
# return wrapped(*args, **kwargs)
82+
# except SystemExit as se:
83+
# sys.exit(se.code)
84+
# except AssertionError as ae:
85+
# print(ae, file=sys.stderr)
86+
# sys.exit(1)
87+
# except APIUnknownException as ue:
88+
# _unknown_exception_handler(ue)
89+
# except (
90+
# APIException,
91+
# APIItemNotFoundException,
92+
# APIItemConflictException,
93+
# APIForbiddenException,
94+
# ContainerPlatformClientException,
95+
# ) as e:
96+
# print(e.message, file=sys.stderr)
97+
# sys.exit(1)
98+
# except Exception as ex:
99+
# _unknown_exception_handler(ex)
240100

241101

242102
class LockProxy(object):
@@ -275,21 +135,21 @@ def list(
275135
else:
276136
print(json.dumps(response))
277137

278-
@intercept_exception
138+
@base.intercept_exception
279139
def create(
280140
self, reason,
281141
):
282142
"""Create a lock."""
283143
print(base.get_client().lock.create(reason), file=sys.stdout)
284144

285-
@intercept_exception
145+
@base.intercept_exception
286146
def delete(
287147
self, id,
288148
):
289149
"""Delete a user lock."""
290150
base.get_client().lock.delete(id)
291151

292-
@intercept_exception
152+
@base.intercept_exception
293153
def delete_all(
294154
self, timeout_secs=300,
295155
):
@@ -307,7 +167,7 @@ def __dir__(self):
307167
"""Return the CLI method names."""
308168
return ["delete", "delete_all", "list", "platform_id", "register"]
309169

310-
@intercept_exception
170+
@base.intercept_exception
311171
def platform_id(self,):
312172
"""Get the platform ID."""
313173
print(base.get_client().license.platform_id())
@@ -337,7 +197,7 @@ def list(
337197
else:
338198
print(json.dumps(response))
339199

340-
@intercept_exception
200+
@base.intercept_exception
341201
def register(
342202
self, server_filename,
343203
):
@@ -394,7 +254,7 @@ def register(
394254
# "'/srv/bluedata/license/'"
395255
# )
396256

397-
@intercept_exception
257+
@base.intercept_exception
398258
def delete(
399259
self, license_key,
400260
):
@@ -405,7 +265,7 @@ def delete(
405265
"""
406266
base.get_client().license.delete(license_key=license_key)
407267

408-
@intercept_exception
268+
@base.intercept_exception
409269
def delete_all(self,):
410270
"""Delete all licenses."""
411271
response = base.get_client().license.list()
@@ -423,7 +283,7 @@ def __dir__(self):
423283
"""Return the CLI method names."""
424284
return ["delete", "get", "post", "put"]
425285

426-
@intercept_exception
286+
@base.intercept_exception
427287
def get(
428288
self, url,
429289
):
@@ -438,7 +298,7 @@ def get(
438298
)
439299
print(response.text, file=sys.stdout)
440300

441-
@intercept_exception
301+
@base.intercept_exception
442302
def delete(
443303
self, url,
444304
):
@@ -452,7 +312,7 @@ def delete(
452312
url, http_method="delete", description="CLI HTTP DELETE",
453313
)
454314

455-
@intercept_exception
315+
@base.intercept_exception
456316
def post(
457317
self, url, json_file="",
458318
):
@@ -487,7 +347,7 @@ def post(
487347
)
488348
print(response.text, file=sys.stdout)
489349

490-
@intercept_exception
350+
@base.intercept_exception
491351
def put(
492352
self, url, json_file="",
493353
):
@@ -517,7 +377,7 @@ def __init__(self):
517377
"""Create instance of proxy class with the client module name."""
518378
super(UserProxy, self).new_instance("user", User)
519379

520-
@intercept_exception
380+
@base.intercept_exception
521381
def create(
522382
self, name, password, description, is_external=False,
523383
):

0 commit comments

Comments
 (0)