Skip to content

Commit 9a311de

Browse files
committed
refactor lock, role, user, httpclient
1 parent 520da92 commit 9a311de

File tree

5 files changed

+342
-239
lines changed

5 files changed

+342
-239
lines changed

bin/cli.py

+13-239
Original file line numberDiff line numberDiff line change
@@ -24,257 +24,30 @@
2424

2525
from __future__ import print_function
2626

27+
import collections
2728
import configparser
28-
import json
29+
import inspect
2930
import os
3031
import sys
3132
from collections import OrderedDict
33+
from textwrap import dedent
3234

3335
import fire
34-
35-
from jinja2 import Environment
3636
import six
37-
import yaml
37+
from jinja2 import Environment
3838

39-
from hpecp.logger import Logger
40-
from textwrap import dedent
41-
import inspect
42-
import collections
43-
from hpecp.user import User
44-
from hpecp.role import Role
45-
from hpecp.cli import base
39+
from hpecp import ContainerPlatformClient
4640
from hpecp.cli.catalog import CatalogProxy
4741
from hpecp.cli.gateway import GatewayProxy
48-
from hpecp.cli.k8sworker import K8sWorkerProxy
42+
from hpecp.cli.httpclient import HttpClientProxy
4943
from hpecp.cli.k8scluster import K8sClusterProxy
50-
from hpecp.cli.tenant import TenantProxy
44+
from hpecp.cli.k8sworker import K8sWorkerProxy
5145
from hpecp.cli.license import LicenseProxy
52-
53-
from hpecp import ContainerPlatformClient
54-
55-
56-
if sys.version_info[0] >= 3:
57-
unicode = str
58-
59-
_log = Logger.get_logger()
60-
61-
62-
class LockProxy(object):
63-
"""Proxy object to :py:attr:`<hpecp.client.lock>`."""
64-
65-
def __dir__(self):
66-
"""Return the CLI method names."""
67-
return [
68-
"create",
69-
"delete",
70-
"delete_all",
71-
"list",
72-
]
73-
74-
def list(
75-
self, output="yaml",
76-
):
77-
"""Get the system and user locks.
78-
79-
:param output: how to display the output ['yaml'|'json']
80-
"""
81-
if output not in ["yaml", "json"]:
82-
print(
83-
"'output' parameter must be 'yaml' or 'json'", file=sys.stderr
84-
)
85-
sys.exit(1)
86-
87-
response = base.get_client().lock.get()
88-
89-
if output == "yaml":
90-
print(
91-
yaml.dump(
92-
yaml.load(json.dumps(response), Loader=yaml.FullLoader,)
93-
)
94-
)
95-
else:
96-
print(json.dumps(response))
97-
98-
@base.intercept_exception
99-
def create(
100-
self, reason,
101-
):
102-
"""Create a lock."""
103-
print(base.get_client().lock.create(reason), file=sys.stdout)
104-
105-
@base.intercept_exception
106-
def delete(
107-
self, id,
108-
):
109-
"""Delete a user lock."""
110-
base.get_client().lock.delete(id)
111-
112-
@base.intercept_exception
113-
def delete_all(
114-
self, timeout_secs=300,
115-
):
116-
"""Delete all locks."""
117-
success = base.get_client().lock.delete_all(timeout_secs=timeout_secs)
118-
if not success:
119-
print("Could not delete locks.", file=sys.stderr)
120-
sys.exit(1)
121-
122-
123-
class HttpClientProxy(object):
124-
"""Proxy object to :py:attr:`<hpecp.client._request>`."""
125-
126-
def __dir__(self):
127-
"""Return the CLI method names."""
128-
return ["delete", "get", "post", "put"]
129-
130-
@base.intercept_exception
131-
def get(
132-
self, url,
133-
):
134-
"""Make HTTP GET request.
135-
136-
Examples
137-
--------
138-
$ hpecp httpclient get /api/v1/workers
139-
"""
140-
response = base.get_client()._request(
141-
url, http_method="get", description="CLI HTTP GET",
142-
)
143-
print(response.text, file=sys.stdout)
144-
145-
@base.intercept_exception
146-
def delete(
147-
self, url,
148-
):
149-
"""Make HTTP DELETE request.
150-
151-
Examples
152-
--------
153-
$ hpecp httpclient delete /api/v1/workers/1
154-
"""
155-
base.get_client()._request(
156-
url, http_method="delete", description="CLI HTTP DELETE",
157-
)
158-
159-
@base.intercept_exception
160-
def post(
161-
self, url, json_file="",
162-
):
163-
"""Make HTTP POST request.
164-
165-
Examples
166-
--------
167-
$ cat > my.json <<-EOF
168-
{
169-
"external_identity_server": {
170-
"bind_pwd":"5ambaPwd@",
171-
"user_attribute":"sAMAccountName",
172-
"bind_type":"search_bind",
173-
"bind_dn":"cn=Administrator,CN=Users,DC=samdom,DC=example,DC=com",
174-
"host":"10.1.0.77",
175-
"security_protocol":"ldaps",
176-
"base_dn":"CN=Users,DC=samdom,DC=example,DC=com",
177-
"verify_peer": false,
178-
"type":"Active Directory",
179-
"port":636
180-
}
181-
}
182-
EOF
183-
184-
hpecp httpclient post /api/v2/config/auth --json-file my.json
185-
"""
186-
with open(json_file, "r",) as f:
187-
data = json.load(f)
188-
189-
response = base.get_client()._request(
190-
url, http_method="post", data=data, description="CLI HTTP POST",
191-
)
192-
print(response.text, file=sys.stdout)
193-
194-
@base.intercept_exception
195-
def put(
196-
self, url, json_file="",
197-
):
198-
"""Make HTTP PUT request.
199-
200-
Examples
201-
--------
202-
$ hpecp httpclient put /api/v2/config/auth --json-file my.json
203-
""" # noqa: W293
204-
with open(json_file, "r",) as f:
205-
data = json.load(f)
206-
207-
response = base.get_client()._request(
208-
url, http_method="put", data=data, description="CLI HTTP PUT",
209-
)
210-
print(response.text, file=sys.stdout)
211-
212-
213-
class UserProxy(base.BaseProxy):
214-
"""Proxy object to :py:attr:`<hpecp.client.user>`."""
215-
216-
def __dir__(self):
217-
"""Return the CLI method names."""
218-
return ["create", "get", "delete", "examples", "list"]
219-
220-
def __init__(self):
221-
"""Create instance of proxy class with the client module name."""
222-
super(UserProxy, self).new_instance("user", User)
223-
224-
@base.intercept_exception
225-
def create(
226-
self, name, password, description, is_external=False,
227-
):
228-
"""Create a User.
229-
230-
:param name: the user name
231-
:param password: the password
232-
:param description: the user descripton
233-
234-
"""
235-
user_id = base.get_client().user.create(
236-
name=name,
237-
password=password,
238-
description=description,
239-
is_external=is_external,
240-
)
241-
print(user_id)
242-
243-
def examples(self):
244-
"""Show usage_examples of the list method."""
245-
print(
246-
dedent(
247-
"""\
248-
249-
hpecp user list --query '[?is_external]' --output json-pp
250-
""" # noqa: E501
251-
)
252-
)
253-
254-
255-
class RoleProxy(base.BaseProxy):
256-
"""Proxy object to :py:attr:`<hpecp.client.role>`."""
257-
258-
def __dir__(self):
259-
"""Return the CLI method names."""
260-
return ["delete", "examples", "get", "list"]
261-
262-
def __init__(self):
263-
"""Create instance of proxy class with the client module name."""
264-
super(RoleProxy, self).new_instance("role", Role)
265-
266-
def examples(self):
267-
"""Show examples for working with roles."""
268-
print(
269-
dedent(
270-
"""\
271-
272-
# Retrieve the role ID for 'Admin'
273-
$ hpecp role list --query "[?label.name == 'Admin'][_links.self.href] | [0][0]" --output json | tr -d '"'
274-
/api/v1/role/2
275-
""" # noqa: E501
276-
)
277-
)
46+
from hpecp.cli.lock import LockProxy
47+
from hpecp.cli.role import RoleProxy
48+
from hpecp.cli.tenant import TenantProxy
49+
from hpecp.cli.user import UserProxy
50+
from hpecp.logger import Logger
27851

27952

28053
def configure_cli():
@@ -424,6 +197,7 @@ def _get_metadata(self):
424197
modules[module_name] = function_parameters
425198
columns[module_name] = all_fields
426199

200+
# _log = Logger.get_logger()
427201
# _log.debug(modules)
428202
# _log.debug(columns)
429203

hpecp/cli/httpclient.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 sys
27+
28+
from hpecp.cli import base
29+
30+
31+
class HttpClientProxy(object):
32+
"""Proxy object to :py:attr:`<hpecp.client._request>`."""
33+
34+
def __dir__(self):
35+
"""Return the CLI method names."""
36+
return ["delete", "get", "post", "put"]
37+
38+
@base.intercept_exception
39+
def get(
40+
self, url,
41+
):
42+
"""Make HTTP GET request.
43+
44+
Examples
45+
--------
46+
$ hpecp httpclient get /api/v1/workers
47+
"""
48+
response = base.get_client()._request(
49+
url, http_method="get", description="CLI HTTP GET",
50+
)
51+
print(response.text, file=sys.stdout)
52+
53+
@base.intercept_exception
54+
def delete(
55+
self, url,
56+
):
57+
"""Make HTTP DELETE request.
58+
59+
Examples
60+
--------
61+
$ hpecp httpclient delete /api/v1/workers/1
62+
"""
63+
base.get_client()._request(
64+
url, http_method="delete", description="CLI HTTP DELETE",
65+
)
66+
67+
@base.intercept_exception
68+
def post(
69+
self, url, json_file="",
70+
):
71+
"""Make HTTP POST request.
72+
73+
Examples
74+
--------
75+
$ cat > my.json <<-EOF
76+
{
77+
"external_identity_server": {
78+
"bind_pwd":"5ambaPwd@",
79+
"user_attribute":"sAMAccountName",
80+
"bind_type":"search_bind",
81+
"bind_dn":"cn=Administrator,CN=Users,DC=samdom,DC=example,DC=com",
82+
"host":"10.1.0.77",
83+
"security_protocol":"ldaps",
84+
"base_dn":"CN=Users,DC=samdom,DC=example,DC=com",
85+
"verify_peer": false,
86+
"type":"Active Directory",
87+
"port":636
88+
}
89+
}
90+
EOF
91+
92+
hpecp httpclient post /api/v2/config/auth --json-file my.json
93+
"""
94+
with open(json_file, "r",) as f:
95+
data = json.load(f)
96+
97+
response = base.get_client()._request(
98+
url, http_method="post", data=data, description="CLI HTTP POST",
99+
)
100+
print(response.text, file=sys.stdout)
101+
102+
@base.intercept_exception
103+
def put(
104+
self, url, json_file="",
105+
):
106+
"""Make HTTP PUT request.
107+
108+
Examples
109+
--------
110+
$ hpecp httpclient put /api/v2/config/auth --json-file my.json
111+
""" # noqa: W293
112+
with open(json_file, "r",) as f:
113+
data = json.load(f)
114+
115+
response = base.get_client()._request(
116+
url, http_method="put", data=data, description="CLI HTTP PUT",
117+
)
118+
print(response.text, file=sys.stdout)

0 commit comments

Comments
 (0)