|
| 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