Skip to content

Commit e1b8957

Browse files
committed
refactor k8sworker
1 parent 233f1ef commit e1b8957

File tree

5 files changed

+64
-76
lines changed

5 files changed

+64
-76
lines changed

bin/cli.py

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@
7272

7373
_log = Logger.get_logger()
7474

75-
PROFILE = os.getenv("PROFILE", default="default")
76-
77-
_log.debug(
78-
"PROFILE envirionment variable exists with value '{}'".format(PROFILE)
79-
)
80-
81-
82-
def get_config_file():
83-
"""Retrieve the CLI config file."""
84-
return base.get_config_file()
85-
8675

8776
@wrapt.decorator
8877
def intercept_exception(wrapped, instance, args, kwargs):
@@ -126,17 +115,6 @@ def _unknown_exception_handler(ex):
126115
_unknown_exception_handler(ex)
127116

128117

129-
@intercept_exception
130-
def get_client(start_session=True):
131-
"""Retrieve a reference to an authenticated client object."""
132-
client = ContainerPlatformClient.create_from_config_file(
133-
config_file=get_config_file(), profile=PROFILE,
134-
)
135-
if start_session:
136-
client.create_session()
137-
return client
138-
139-
140118
class K8sClusterProxy(base.BaseProxy):
141119
"""Proxy object to :py:attr:`<hpecp.client.k8s_cluster>`."""
142120

@@ -200,7 +178,7 @@ def create(
200178
]
201179

202180
print(
203-
get_client().k8s_cluster.create(
181+
base.get_client().k8s_cluster.create(
204182
name=name,
205183
description=description,
206184
k8s_version=k8s_version,
@@ -220,7 +198,7 @@ def admin_kube_config(self, id):
220198
:param id: the cluster ID
221199
"""
222200
print(
223-
get_client()
201+
base.get_client()
224202
.k8s_cluster.get(id)
225203
.admin_kube_config.replace("\\n", "\n",)
226204
)
@@ -232,7 +210,9 @@ def dashboard_url(
232210
233211
:param id: the cluster ID
234212
"""
235-
url = get_client().k8s_cluster.get(id=id).dashboard_endpoint_access
213+
url = (
214+
base.get_client().k8s_cluster.get(id=id).dashboard_endpoint_access
215+
)
236216
print(url)
237217

238218
def dashboard_token(
@@ -242,7 +222,7 @@ def dashboard_token(
242222
243223
:param id: the cluster ID
244224
"""
245-
token = get_client().k8s_cluster.get(id=id).dashboard_token
225+
token = base.get_client().k8s_cluster.get(id=id).dashboard_token
246226
if six.PY2:
247227
print(base64.b64decode(token.encode()))
248228
else:
@@ -251,7 +231,7 @@ def dashboard_token(
251231
@intercept_exception
252232
def k8smanifest(self):
253233
"""Retrieve the k8smanifest."""
254-
response = get_client().k8s_cluster.k8smanifest()
234+
response = base.get_client().k8s_cluster.k8smanifest()
255235
print(
256236
yaml.dump(yaml.load(json.dumps(response), Loader=yaml.FullLoader,))
257237
)
@@ -261,7 +241,7 @@ def get_installed_addons(self, id):
261241
262242
:param id: get installed addons for a specific cluster
263243
"""
264-
print(get_client().k8s_cluster.get(id=id).addons)
244+
print(base.get_client().k8s_cluster.get(id=id).addons)
265245

266246
def get_available_addons(self, id=None, k8s_version=None):
267247
"""Retrieve the available addons for a cluster.
@@ -284,10 +264,10 @@ def get_available_addons(self, id=None, k8s_version=None):
284264
sys.exit(1)
285265

286266
if id:
287-
print(get_client().k8s_cluster.get_available_addons(id=id))
267+
print(base.get_client().k8s_cluster.get_available_addons(id=id))
288268
else:
289269
print(
290-
get_client().k8s_cluster.get_available_addons(
270+
base.get_client().k8s_cluster.get_available_addons(
291271
k8s_version=k8s_version
292272
)
293273
)
@@ -310,7 +290,7 @@ def add_addons(self, id, addons, wait_for_ready_sec=0):
310290
)
311291
sys.exit(1)
312292

313-
get_client().k8s_cluster.add_addons(id=id, addons=addons)
293+
base.get_client().k8s_cluster.add_addons(id=id, addons=addons)
314294

315295
if wait_for_ready_sec > 0:
316296
self.wait_for_status(
@@ -371,7 +351,7 @@ def k8s_supported_versions(
371351
patch_filter = int(patch_filter)
372352

373353
vers = []
374-
for v in get_client().k8s_cluster.k8s_supported_versions():
354+
for v in base.get_client().k8s_cluster.k8s_supported_versions():
375355
(major, minor, patch) = v.split(".")
376356
major = int(major)
377357
minor = int(minor)
@@ -437,7 +417,7 @@ def create(
437417
k8s_cluster_id : [type], optional
438418
[description], by default None
439419
"""
440-
tenant_id = get_client().tenant.create(
420+
tenant_id = base.get_client().tenant.create(
441421
name=name,
442422
description=description,
443423
tenant_type=tenant_type,
@@ -469,7 +449,7 @@ def k8skubeconfig(self):
469449
str
470450
Tenant KubeConfig
471451
"""
472-
conf = get_client().tenant.k8skubeconfig()
452+
conf = base.get_client().tenant.k8skubeconfig()
473453
print(conf)
474454

475455
@intercept_exception
@@ -481,7 +461,7 @@ def users(self, id, output="table", columns="ALL", query={}):
481461
id : str
482462
The tenant ID.
483463
"""
484-
list_instance = get_client().tenant.users(id=id)
464+
list_instance = base.get_client().tenant.users(id=id)
485465
self.print_list(
486466
list_instance=list_instance,
487467
output=output,
@@ -492,28 +472,30 @@ def users(self, id, output="table", columns="ALL", query={}):
492472
@intercept_exception
493473
def assign_user_to_role(self, tenant_id, user_id, role_id):
494474
"""Assign user to role in tenant."""
495-
get_client().tenant.assign_user_to_role(
475+
base.get_client().tenant.assign_user_to_role(
496476
tenant_id=tenant_id, user_id=user_id, role_id=role_id
497477
)
498478

499479
@intercept_exception
500480
def get_external_user_groups(self, tenant_id):
501481
"""Retrieve External User Groups."""
502482
print(
503-
get_client().tenant.get_external_user_groups(tenant_id=tenant_id)
483+
base.get_client().tenant.get_external_user_groups(
484+
tenant_id=tenant_id
485+
)
504486
)
505487

506488
@intercept_exception
507489
def add_external_user_group(self, tenant_id, group, role_id):
508490
"""Add External User Group."""
509-
get_client().tenant.add_external_user_group(
491+
base.get_client().tenant.add_external_user_group(
510492
tenant_id=tenant_id, group=group, role_id=role_id
511493
)
512494

513495
@intercept_exception
514496
def delete_external_user_group(self, tenant_id, group):
515497
"""Delete External User Group."""
516-
get_client().tenant.delete_external_user_group(
498+
base.get_client().tenant.delete_external_user_group(
517499
tenant_id=tenant_id, group=group
518500
)
519501

@@ -543,7 +525,7 @@ def list(
543525
)
544526
sys.exit(1)
545527

546-
response = get_client().lock.get()
528+
response = base.get_client().lock.get()
547529

548530
if output == "yaml":
549531
print(
@@ -559,21 +541,21 @@ def create(
559541
self, reason,
560542
):
561543
"""Create a lock."""
562-
print(get_client().lock.create(reason), file=sys.stdout)
544+
print(base.get_client().lock.create(reason), file=sys.stdout)
563545

564546
@intercept_exception
565547
def delete(
566548
self, id,
567549
):
568550
"""Delete a user lock."""
569-
get_client().lock.delete(id)
551+
base.get_client().lock.delete(id)
570552

571553
@intercept_exception
572554
def delete_all(
573555
self, timeout_secs=300,
574556
):
575557
"""Delete all locks."""
576-
success = get_client().lock.delete_all(timeout_secs=timeout_secs)
558+
success = base.get_client().lock.delete_all(timeout_secs=timeout_secs)
577559
if not success:
578560
print("Could not delete locks.", file=sys.stderr)
579561
sys.exit(1)
@@ -589,7 +571,7 @@ def __dir__(self):
589571
@intercept_exception
590572
def platform_id(self,):
591573
"""Get the platform ID."""
592-
print(get_client().license.platform_id())
574+
print(base.get_client().license.platform_id())
593575

594576
def list(
595577
self, output="yaml", license_key_only=False,
@@ -598,7 +580,7 @@ def list(
598580
599581
:param output: how to display the output ['yaml'|'json']
600582
"""
601-
response = get_client().license.list()
583+
response = base.get_client().license.list()
602584
if license_key_only:
603585
response = [
604586
str(unicode(li["LicenseKey"])) for li in response["Licenses"]
@@ -625,7 +607,9 @@ def register(
625607
:param server_filename: Filepath to the license on the server, e.g.
626608
'/srv/bluedata/license/LICENSE-1.txt'
627609
"""
628-
print(get_client().license.register(server_filename=server_filename))
610+
print(
611+
base.get_client().license.register(server_filename=server_filename)
612+
)
629613

630614
# TODO implement me!
631615
# def upload_with_ssh_key(
@@ -680,17 +664,17 @@ def delete(
680664
:param license_key: The license key, e.g. '1234 1234 ... 1234
681665
"SOMETEXT"'
682666
"""
683-
get_client().license.delete(license_key=license_key)
667+
base.get_client().license.delete(license_key=license_key)
684668

685669
@intercept_exception
686670
def delete_all(self,):
687671
"""Delete all licenses."""
688-
response = get_client().license.list()
672+
response = base.get_client().license.list()
689673
all_license_keys = [
690674
str(unicode(li["LicenseKey"])) for li in response["Licenses"]
691675
]
692676
for licence_key in all_license_keys:
693-
get_client().license.delete(license_key=licence_key)
677+
base.get_client().license.delete(license_key=licence_key)
694678

695679

696680
class HttpClientProxy(object):
@@ -710,7 +694,7 @@ def get(
710694
--------
711695
$ hpecp httpclient get /api/v1/workers
712696
"""
713-
response = get_client()._request(
697+
response = base.get_client()._request(
714698
url, http_method="get", description="CLI HTTP GET",
715699
)
716700
print(response.text, file=sys.stdout)
@@ -725,7 +709,7 @@ def delete(
725709
--------
726710
$ hpecp httpclient delete /api/v1/workers/1
727711
"""
728-
get_client()._request(
712+
base.get_client()._request(
729713
url, http_method="delete", description="CLI HTTP DELETE",
730714
)
731715

@@ -759,7 +743,7 @@ def post(
759743
with open(json_file, "r",) as f:
760744
data = json.load(f)
761745

762-
response = get_client()._request(
746+
response = base.get_client()._request(
763747
url, http_method="post", data=data, description="CLI HTTP POST",
764748
)
765749
print(response.text, file=sys.stdout)
@@ -777,7 +761,7 @@ def put(
777761
with open(json_file, "r",) as f:
778762
data = json.load(f)
779763

780-
response = get_client()._request(
764+
response = base.get_client()._request(
781765
url, http_method="put", data=data, description="CLI HTTP PUT",
782766
)
783767
print(response.text, file=sys.stdout)
@@ -805,7 +789,7 @@ def create(
805789
:param description: the user descripton
806790
807791
"""
808-
user_id = get_client().user.create(
792+
user_id = base.get_client().user.create(
809793
name=name,
810794
password=password,
811795
description=description,

hpecp/cli/base.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@
5050

5151
_log = Logger.get_logger()
5252

53-
PROFILE = os.getenv("PROFILE", default="default")
53+
54+
def get_profile():
55+
"""Retrieve the profile - if supplied."""
56+
profile = os.getenv("PROFILE", default="default")
57+
_log.debug(
58+
"PROFILE envirionment variable exists with value '{}'".format(profile)
59+
)
60+
return profile
5461

5562

5663
def get_config_file():
@@ -118,7 +125,7 @@ def _unknown_exception_handler(ex):
118125
def get_client(start_session=True):
119126
"""Retrieve a reference to an authenticated client object."""
120127
client = ContainerPlatformClient.create_from_config_file(
121-
config_file=get_config_file(), profile=PROFILE,
128+
config_file=get_config_file(), profile=get_profile(),
122129
)
123130
if start_session:
124131
client.create_session()

tests/library/base_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,14 @@ def setUp(self):
158158
self.cli = cli
159159

160160
self.saved_base_get_config_file = base.get_config_file
161-
self.saved_cli_get_config_file = self.cli.get_config_file
161+
self.saved_base_get_client = base.get_client
162+
# self.saved_cli_get_config_file = self.cli.get_config_file
162163

163164
def get_config_file():
164165
return self.tmpFile.name
165166

166167
# override method to return config file path
167-
self.cli.get_config_file = get_config_file
168+
# self.cli.get_config_file = get_config_file
168169
base.get_config_file = get_config_file
169170

170171
def tearDown(self):
@@ -175,4 +176,5 @@ def tearDown(self):
175176
sys.stderr = self.saved_stderr
176177

177178
base.get_config_file = self.saved_base_get_config_file
178-
self.cli.get_config_file = self.saved_cli_get_config_file
179+
base.get_client = self.saved_base_get_client
180+
# self.cli.get_config_file = self.saved_cli_get_config_file

0 commit comments

Comments
 (0)