Skip to content

Commit f4e5ac6

Browse files
committed
role.get() implemented in role module;
client.role() implemented in client module; RoleProxy() implemented in cli module; Fixes #10
1 parent a8b7455 commit f4e5ac6

File tree

3 files changed

+203
-2
lines changed

3 files changed

+203
-2
lines changed

bin/cli.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,27 @@ def list(
906906
else:
907907
print(get_client().user.list().json)
908908

909+
class RoleProxy(object):
910+
def get(
911+
self, role_id, output="yaml",
912+
):
913+
"""Retrieve a Role by Id
914+
915+
:param role_id: the id of the role with format: '/api/v1/role/[0-9]+'
916+
:param output: how to display the output ['yaml'|'json']
917+
"""
918+
response = get_client().role.get(role_id)
919+
if output == "yaml":
920+
print(
921+
yaml.dump(
922+
yaml.load(
923+
json.dumps(response.json), Loader=yaml.FullLoader,
924+
)
925+
)
926+
)
927+
else:
928+
print(response.json)
929+
909930

910931
class AutoComplete:
911932
"""Example Usage:
@@ -1083,9 +1104,11 @@ def __init__(self,):
10831104
self.license = LicenseProxy()
10841105
self.httpclient = HttpClientProxy()
10851106
self.user = UserProxy()
1107+
self.role = RoleProxy()
10861108
self.autocomplete = AutoComplete()
10871109
self.configure_cli = configure_cli
10881110

10891111

1112+
10901113
if __name__ == "__main__":
10911114
fire.Fire(CLI)

hpecp/client.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from .logger import Logger
4848
from .tenant import TenantController
4949
from .catalog import CatalogController
50+
from .role import RoleController
5051

5152
try:
5253
basestring
@@ -349,6 +350,7 @@ def __init__(
349350
self._lock = LockController(self)
350351
self._user = UserController(self)
351352
self._catalog = CatalogController(self)
353+
self._role = RoleController(self)
352354

353355
def create_session(self):
354356
"""Create a session with the HPE CP controller defined in the object :py:class:`ContainerPlatformClient`.
@@ -716,15 +718,15 @@ def user(self):
716718
"""
717719
This attribute is a reference to an object of type `.user.UserController`.
718720
719-
See the class :py:class:`.lock.UserController` for the methods available.
721+
See the class :py:class:`.user.UserController` for the methods available.
720722
721723
Example::
722724
723725
client = ContainerPlatformClient(...)
724726
client.create_session()
725727
client.user.get()
726728
727-
This example calls the method :py:meth:`get() <.user.UserController.list>` in :py:class:`.user.UserController`.
729+
This example calls the method :py:meth:`get() <.user.UserController.get>` in :py:class:`.user.UserController`.
728730
"""
729731

730732
return self._user
@@ -746,3 +748,21 @@ def catalog(self):
746748
"""
747749

748750
return self._catalog
751+
752+
@property
753+
def role(self):
754+
"""
755+
This attribute is a reference to an object of type `.role.RoleController`.
756+
757+
See the class :py:class:`.role.RoleController` for the methods available.
758+
759+
Example::
760+
761+
client = ContainerPlatformClient(...)
762+
client.create_session()
763+
client.role.get()
764+
765+
This example calls the method :py:meth:`get() <.role.RoleController.get>` in :py:class:`.role.RoleController`.
766+
"""
767+
768+
return self._role

hpecp/role.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
from __future__ import absolute_import
22+
from .logger import Logger
23+
from .exceptions import (
24+
ContainerPlatformClientException,
25+
APIException,
26+
APIItemNotFoundException,
27+
APIItemConflictException,
28+
)
29+
30+
import json
31+
from operator import attrgetter
32+
33+
# from tabulate import tabulate
34+
# from enum import Enum
35+
# import polling
36+
import re
37+
import six
38+
import sys
39+
40+
try:
41+
basestring
42+
except NameError:
43+
basestring = str
44+
45+
46+
class RoleController:
47+
"""This is the main class that users will interact with to work with roles.
48+
49+
An instance of this class is available in the client.ContainerPlatformClient with the attribute name
50+
:py:attr:`role <.client.ContainerPlatformClient.role>`. The methods of this class can be
51+
invoked using `client.role.method()`. See the example below:
52+
53+
Example::
54+
55+
client = ContainerPlatformClient(...).create_session()
56+
client.role.get()
57+
58+
"""
59+
def __init__(self, client):
60+
self.client = client
61+
62+
def get(self, role_id):
63+
"""Retrieve a Role by ID.
64+
65+
Args:
66+
role_id: str
67+
The role ID - format: '/api/v1/role/[0-9]+'
68+
69+
Returns:
70+
Role: object representing Role
71+
72+
Raises:
73+
APIException
74+
"""
75+
assert isinstance(
76+
role_id, str
77+
), "'role_id' must be provided and must be a string"
78+
assert re.match(
79+
r"\/api\/v1\/role\/[0-9]+", role_id
80+
), "'role_id' must have format '/api/v1/role/[0-9]+'"
81+
82+
response = self.client._request(
83+
url=role_id, http_method="get", description="role/get"
84+
)
85+
if response.json()["purpose"] != "proxy":
86+
raise APIItemNotFoundException(
87+
message="role not found with id: " + role_id,
88+
request_method="get",
89+
request_url=role_id,
90+
)
91+
92+
return Role(response.json())
93+
94+
95+
class Role:
96+
"""Create an instance of Role from json data returned from the HPE Container Platform API.
97+
98+
Users of this library are not expected to create an instance of this class.
99+
100+
Parameters:
101+
json : str
102+
The json returned by the API representing a Gateway.
103+
104+
Returns:
105+
Gateway:
106+
An instance of Gateway
107+
"""
108+
109+
all_fields = [
110+
"id",
111+
"name",
112+
"description",
113+
]
114+
"""All of the fields of Role objects as returned by the HPE Container Platform API"""
115+
116+
default_display_fields = [
117+
"id",
118+
"name",
119+
"description",
120+
]
121+
"""These fields are displayed by default, e.g. in tabulate()"""
122+
123+
def __init__(self, json):
124+
self.json = json
125+
self.display_columns = Role.default_display_fields
126+
127+
def __repr__(self):
128+
return "<Role id:{} description:{}>".format(self.id, self.description)
129+
130+
def __str__(self):
131+
return "K8sCluster(id={}, description={})".format(
132+
self.id, self.description
133+
)
134+
135+
def __dir__(self):
136+
return self.display_columns
137+
138+
def __getitem__(self, item):
139+
return getattr(self, self.display_columns[item])
140+
141+
def __len__(self):
142+
return len(dir(self))
143+
144+
@property
145+
def id(self):
146+
"""@Field: from json['_links']['self']['href'] - id format: '/api/v1/role/[0-9]+'"""
147+
return self.json["_links"]["self"]["href"]
148+
149+
@property
150+
def name(self):
151+
"""@Field: from json['label']['name']"""
152+
return self.json["label"]["name"]
153+
154+
@property
155+
def description(self):
156+
"""@Field: from json['label']['description']"""
157+
return self.json["label"]["description"]
158+

0 commit comments

Comments
 (0)