Skip to content

Commit 0d78a8f

Browse files
committed
refactor license
1 parent e544e05 commit 0d78a8f

File tree

2 files changed

+146
-158
lines changed

2 files changed

+146
-158
lines changed

bin/cli.py

Lines changed: 2 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
from hpecp.cli.k8sworker import K8sWorkerProxy
4949
from hpecp.cli.k8scluster import K8sClusterProxy
5050
from hpecp.cli.tenant import TenantProxy
51+
from hpecp.cli.license import LicenseProxy
52+
5153
from hpecp import ContainerPlatformClient
5254

5355

@@ -57,48 +59,6 @@
5759
_log = Logger.get_logger()
5860

5961

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)
100-
101-
10262
class LockProxy(object):
10363
"""Proxy object to :py:attr:`<hpecp.client.lock>`."""
10464

@@ -160,122 +120,6 @@ def delete_all(
160120
sys.exit(1)
161121

162122

163-
class LicenseProxy(object):
164-
"""Proxy object to :py:attr:`<hpecp.client.license>`."""
165-
166-
def __dir__(self):
167-
"""Return the CLI method names."""
168-
return ["delete", "delete_all", "list", "platform_id", "register"]
169-
170-
@base.intercept_exception
171-
def platform_id(self,):
172-
"""Get the platform ID."""
173-
print(base.get_client().license.platform_id())
174-
175-
def list(
176-
self, output="yaml", license_key_only=False,
177-
):
178-
"""Retrieve the list of licenses.
179-
180-
:param output: how to display the output ['yaml'|'json']
181-
"""
182-
response = base.get_client().license.list()
183-
if license_key_only:
184-
response = [
185-
str(unicode(li["LicenseKey"])) for li in response["Licenses"]
186-
]
187-
print("\n".join(response))
188-
else:
189-
if output == "yaml":
190-
print(
191-
yaml.dump(
192-
yaml.load(
193-
json.dumps(response), Loader=yaml.FullLoader,
194-
)
195-
)
196-
)
197-
else:
198-
print(json.dumps(response))
199-
200-
@base.intercept_exception
201-
def register(
202-
self, server_filename,
203-
):
204-
"""Register a license.
205-
206-
:param server_filename: Filepath to the license on the server, e.g.
207-
'/srv/bluedata/license/LICENSE-1.txt'
208-
"""
209-
print(
210-
base.get_client().license.register(server_filename=server_filename)
211-
)
212-
213-
# TODO implement me!
214-
# def upload_with_ssh_key(
215-
# self,
216-
# server_filename,
217-
# ssh_key_file=None,
218-
# ssh_key_data=None,
219-
# license_file=None,
220-
# base64enc_license_data=None,
221-
# ):
222-
# """Not implemented yet.
223-
224-
# Workaround:
225-
# -----------
226-
# - scp your license to '/srv/bluedata/license/' on the controller
227-
# - run client.license.register(server_filename) to register
228-
# the license
229-
# """
230-
# raise Exception(
231-
# "Not implemented yet! Workaround: scp your license to"
232-
# "'/srv/bluedata/license/'"
233-
# )
234-
235-
# TODO implement me!
236-
# def upload_with_ssh_pass(
237-
# self,
238-
# server_filename,
239-
# ssh_username,
240-
# ssh_password,
241-
# license_file=None,
242-
# base64enc_license_data=None,
243-
# ):
244-
# """Not implemented yet.
245-
246-
# Workaround:
247-
# -----------
248-
# - scp your license to '/srv/bluedata/license/' on the controller
249-
# - run client.license.register(server_filename) to register
250-
# the license
251-
# """
252-
# raise Exception(
253-
# "Not implemented yet! Workaround: scp your license to"
254-
# "'/srv/bluedata/license/'"
255-
# )
256-
257-
@base.intercept_exception
258-
def delete(
259-
self, license_key,
260-
):
261-
"""Delete a license by LicenseKey.
262-
263-
:param license_key: The license key, e.g. '1234 1234 ... 1234
264-
"SOMETEXT"'
265-
"""
266-
base.get_client().license.delete(license_key=license_key)
267-
268-
@base.intercept_exception
269-
def delete_all(self,):
270-
"""Delete all licenses."""
271-
response = base.get_client().license.list()
272-
all_license_keys = [
273-
str(unicode(li["LicenseKey"])) for li in response["Licenses"]
274-
]
275-
for licence_key in all_license_keys:
276-
base.get_client().license.delete(license_key=licence_key)
277-
278-
279123
class HttpClientProxy(object):
280124
"""Proxy object to :py:attr:`<hpecp.client._request>`."""
281125

hpecp/cli/license.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# (C) Copyright [2020] Hewlett Packard Enterprise Development LP
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a
4+
# copy of this software and associated documentation files (the "Software"),
5+
# to deal in the Software without restriction, including without limitation
6+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
# and/or sell copies of the Software, and to permit persons to whom the
8+
# Software is furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included
11+
# in all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19+
# OTHER DEALINGS IN THE SOFTWARE.
20+
21+
"""HPE Container Platform CLI."""
22+
23+
from __future__ import print_function
24+
25+
import json
26+
import yaml
27+
28+
from hpecp.cli import base
29+
30+
31+
class LicenseProxy(object):
32+
"""Proxy object to :py:attr:`<hpecp.client.license>`."""
33+
34+
def __dir__(self):
35+
"""Return the CLI method names."""
36+
return ["delete", "delete_all", "list", "platform_id", "register"]
37+
38+
@base.intercept_exception
39+
def platform_id(self,):
40+
"""Get the platform ID."""
41+
print(base.get_client().license.platform_id())
42+
43+
def list(
44+
self, output="yaml", license_key_only=False,
45+
):
46+
"""Retrieve the list of licenses.
47+
48+
:param output: how to display the output ['yaml'|'json']
49+
"""
50+
response = base.get_client().license.list()
51+
if license_key_only:
52+
response = [
53+
str(unicode(li["LicenseKey"])) for li in response["Licenses"]
54+
]
55+
print("\n".join(response))
56+
else:
57+
if output == "yaml":
58+
print(
59+
yaml.dump(
60+
yaml.load(
61+
json.dumps(response), Loader=yaml.FullLoader,
62+
)
63+
)
64+
)
65+
else:
66+
print(json.dumps(response))
67+
68+
@base.intercept_exception
69+
def register(
70+
self, server_filename,
71+
):
72+
"""Register a license.
73+
74+
:param server_filename: Filepath to the license on the server, e.g.
75+
'/srv/bluedata/license/LICENSE-1.txt'
76+
"""
77+
print(
78+
base.get_client().license.register(server_filename=server_filename)
79+
)
80+
81+
# TODO implement me!
82+
# def upload_with_ssh_key(
83+
# self,
84+
# server_filename,
85+
# ssh_key_file=None,
86+
# ssh_key_data=None,
87+
# license_file=None,
88+
# base64enc_license_data=None,
89+
# ):
90+
# """Not implemented yet.
91+
92+
# Workaround:
93+
# -----------
94+
# - scp your license to '/srv/bluedata/license/' on the controller
95+
# - run client.license.register(server_filename) to register
96+
# the license
97+
# """
98+
# raise Exception(
99+
# "Not implemented yet! Workaround: scp your license to"
100+
# "'/srv/bluedata/license/'"
101+
# )
102+
103+
# TODO implement me!
104+
# def upload_with_ssh_pass(
105+
# self,
106+
# server_filename,
107+
# ssh_username,
108+
# ssh_password,
109+
# license_file=None,
110+
# base64enc_license_data=None,
111+
# ):
112+
# """Not implemented yet.
113+
114+
# Workaround:
115+
# -----------
116+
# - scp your license to '/srv/bluedata/license/' on the controller
117+
# - run client.license.register(server_filename) to register
118+
# the license
119+
# """
120+
# raise Exception(
121+
# "Not implemented yet! Workaround: scp your license to"
122+
# "'/srv/bluedata/license/'"
123+
# )
124+
125+
@base.intercept_exception
126+
def delete(
127+
self, license_key,
128+
):
129+
"""Delete a license by LicenseKey.
130+
131+
:param license_key: The license key, e.g. '1234 1234 ... 1234
132+
"SOMETEXT"'
133+
"""
134+
base.get_client().license.delete(license_key=license_key)
135+
136+
@base.intercept_exception
137+
def delete_all(self,):
138+
"""Delete all licenses."""
139+
response = base.get_client().license.list()
140+
all_license_keys = [
141+
str(unicode(li["LicenseKey"])) for li in response["Licenses"]
142+
]
143+
for licence_key in all_license_keys:
144+
base.get_client().license.delete(license_key=licence_key)

0 commit comments

Comments
 (0)