Skip to content

Commit e2b1076

Browse files
authored
Merge pull request #43 from hpe-container-platform-community/hurrynair/implement-client-role-10
reformatted role.py
2 parents 25ca49f + 01ad6ed commit e2b1076

File tree

3 files changed

+202
-2
lines changed

3 files changed

+202
-2
lines changed

bin/cli.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,28 @@ def list(
924924
print(get_client().user.list().json)
925925

926926

927+
class RoleProxy(object):
928+
def get(
929+
self, role_id, output="yaml",
930+
):
931+
"""Retrieve a Role by Id
932+
933+
:param role_id: the id of the role with format: '/api/v1/role/[0-9]+'
934+
:param output: how to display the output ['yaml'|'json']
935+
"""
936+
response = get_client().role.get(role_id)
937+
if output == "yaml":
938+
print(
939+
yaml.dump(
940+
yaml.load(
941+
json.dumps(response.json), Loader=yaml.FullLoader,
942+
)
943+
)
944+
)
945+
else:
946+
print(response.json)
947+
948+
927949
class AutoComplete:
928950
"""Example Usage:
929951
@@ -1100,6 +1122,7 @@ def __init__(self,):
11001122
self.license = LicenseProxy()
11011123
self.httpclient = HttpClientProxy()
11021124
self.user = UserProxy()
1125+
self.role = RoleProxy()
11031126
self.autocomplete = AutoComplete()
11041127
self.configure_cli = configure_cli
11051128

hpecp/client.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from .lock import LockController
4747
from .logger import Logger
4848
from .tenant import TenantController
49+
from .role import RoleController
4950
from .user import UserController
5051

5152
try:
@@ -353,6 +354,7 @@ def __init__(
353354
self._lock = LockController(self)
354355
self._user = UserController(self)
355356
self._catalog = CatalogController(self)
357+
self._role = RoleController(self)
356358

357359
def create_session(self):
358360
"""Create a session with the HPE CP controller defined in the object
@@ -740,7 +742,6 @@ def some_method(self):
740742
@property
741743
def user(self):
742744
"""This attribute is a reference to an object of type
743-
744745
`.user.UserController`. See the class :py:class:`.lock.UserController`
745746
for the methods available.
746747
@@ -751,7 +752,7 @@ def user(self):
751752
client.user.get()
752753
753754
This example calls the method
754-
:py:meth:`get() <.user.UserController.list>` in
755+
:py:meth:`get() <.user.UserController.get>` in
755756
:py:class:`.user.UserController`.
756757
"""
757758

@@ -776,3 +777,21 @@ def catalog(self):
776777
"""
777778

778779
return self._catalog
780+
781+
@property
782+
def role(self):
783+
"""
784+
This attribute is a reference to an object of type `.role.RoleController`.
785+
786+
See the class :py:class:`.role.RoleController` for the methods available.
787+
788+
Example::
789+
790+
client = ContainerPlatformClient(...)
791+
client.create_session()
792+
client.role.get()
793+
794+
This example calls the method :py:meth:`get() <.role.RoleController.get>` in :py:class:`.role.RoleController`.
795+
"""
796+
797+
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+
60+
def __init__(self, client):
61+
self.client = client
62+
63+
def get(self, role_id):
64+
"""Retrieve a Role by ID.
65+
66+
Args:
67+
role_id: str
68+
The role ID - format: '/api/v1/role/[0-9]+'
69+
70+
Returns:
71+
Role: object representing Role
72+
73+
Raises:
74+
APIException
75+
"""
76+
assert isinstance(
77+
role_id, str
78+
), "'role_id' must be provided and must be a string"
79+
assert re.match(
80+
r"\/api\/v1\/role\/[0-9]+", role_id
81+
), "'role_id' must have format '/api/v1/role/[0-9]+'"
82+
83+
response = self.client._request(
84+
url=role_id, http_method="get", description="role/get"
85+
)
86+
if response.json()["purpose"] != "proxy":
87+
raise APIItemNotFoundException(
88+
message="role not found with id: " + role_id,
89+
request_method="get",
90+
request_url=role_id,
91+
)
92+
93+
return Role(response.json())
94+
95+
96+
class Role:
97+
"""Create an instance of Role from json data returned from the HPE Container Platform API.
98+
99+
Users of this library are not expected to create an instance of this class.
100+
101+
Parameters:
102+
json : str
103+
The json returned by the API representing a Gateway.
104+
105+
Returns:
106+
Gateway:
107+
An instance of Gateway
108+
"""
109+
110+
all_fields = [
111+
"id",
112+
"name",
113+
"description",
114+
]
115+
"""All of the fields of Role objects as returned by the HPE Container Platform API"""
116+
117+
default_display_fields = [
118+
"id",
119+
"name",
120+
"description",
121+
]
122+
"""These fields are displayed by default, e.g. in tabulate()"""
123+
124+
def __init__(self, json):
125+
self.json = json
126+
self.display_columns = Role.default_display_fields
127+
128+
def __repr__(self):
129+
return "<Role id:{} description:{}>".format(self.id, self.description)
130+
131+
def __str__(self):
132+
return "K8sCluster(id={}, description={})".format(
133+
self.id, self.description
134+
)
135+
136+
def __dir__(self):
137+
return self.display_columns
138+
139+
def __getitem__(self, item):
140+
return getattr(self, self.display_columns[item])
141+
142+
def __len__(self):
143+
return len(dir(self))
144+
145+
@property
146+
def id(self):
147+
"""@Field: from json['_links']['self']['href'] - id format: '/api/v1/role/[0-9]+'"""
148+
return self.json["_links"]["self"]["href"]
149+
150+
@property
151+
def name(self):
152+
"""@Field: from json['label']['name']"""
153+
return self.json["label"]["name"]
154+
155+
@property
156+
def description(self):
157+
"""@Field: from json['label']['description']"""
158+
return self.json["label"]["description"]

0 commit comments

Comments
 (0)