1 # Copyright 2019 Whitestack, LLC
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
15 # For those usages not covered by the Apache License, Version 2.0 please
16 # contact: esousa@whitestack.com or glavado@whitestack.com
23 from osmclient
.common
import utils
24 from osmclient
.common
.exceptions
import ClientException
25 from osmclient
.common
.exceptions
import NotFound
26 from osmclient
.common
.exceptions
import OsmHttpException
33 def __init__(self
, http
=None, client
=None):
36 self
._logger
= logging
.getLogger('osmclient')
37 self
._apiName
= '/admin'
38 self
._apiVersion
= '/v1'
39 self
._apiResource
= '/roles'
40 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
41 self
._apiVersion
, self
._apiResource
)
43 def create(self
, name
, permissions
):
45 Creates a new OSM role.
47 :param name: name of the role.
48 :param permissions: permissions of the role in YAML.
49 :raises ClientException: when receives an unexpected from the server.
50 :raises ClientException: when fails creating a role.
52 self
._logger
.debug("")
53 self
._client
.get_token()
57 role_permissions
= yaml
.safe_load(permissions
)
59 if not isinstance(role_permissions
, dict):
60 raise ClientException('Role permissions should be provided in a key-value fashion')
62 for key
, value
in role_permissions
.items():
63 if not isinstance(value
, bool):
64 raise ClientException("Value of '{}' in a role permissions should be boolean".format(key
))
66 role
["permissions"] = role_permissions
68 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
70 # print('HTTP CODE: {}'.format(http_code))
71 # print('RESP: {}'.format(resp))
72 #if http_code in (200, 201, 202, 204):
74 resp
= json
.loads(resp
)
75 if not resp
or 'id' not in resp
:
76 raise OsmHttpException('Unexpected response from server - {}'.format(
83 # msg = json.loads(resp)
86 # raise ClientException("Failed to create role {} - {}".format(name, msg))
88 def update(self
, name
, new_name
, permissions
, add
=None, remove
=None):
90 Updates an OSM role identified by name.
92 NOTE: definition and add/remove are mutually exclusive.
94 :param name: name of the role
95 :param set_name: if provided, change the name.
96 :param permissions: if provided, overwrites the existing role specification. NOT IMPLEMENTED
97 :param add: if provided, adds new rules to the definition.
98 :param remove: if provided, removes rules from the definition.
99 :raises ClientException: when receives an unexpected response from the server.
100 :raises ClientException: when fails updating a role.
102 self
._logger
.debug("")
103 self
._client
.get_token()
104 if new_name
is None and permissions
is None and add
is None and remove
is None:
105 raise ClientException('At least one option should be provided')
106 elif permissions
and (add
or remove
):
107 raise ClientException('permissions and add/remove are mutually exclusive')
109 role_obj
= self
.get(name
)
110 new_role_obj
= {"permissions": {}}
112 new_role_obj
["name"] = new_name
115 role_definition
= yaml
.safe_load(permissions
)
117 if not isinstance(role_definition
, dict):
118 raise ClientException('Role permissions should be provided in a key-value fashion')
120 for key
, value
in role_definition
.items():
121 if not isinstance(value
, bool) and value
is not None:
122 raise ClientException('Value in a role permissions should be boolean or None to remove')
124 new_role_obj
["permissions"] = role_definition
127 keys_from_remove
= yaml
.safe_load(remove
)
129 if not isinstance(keys_from_remove
, list):
130 raise ClientException('Keys should be provided in a list fashion')
132 for key
in keys_from_remove
:
133 if not isinstance(key
, str):
134 raise ClientException('Individual keys should be strings')
135 new_role_obj
["permissions"][key
] = None
138 add_roles
= yaml
.safe_load(add
)
140 if not isinstance(add_roles
, dict):
141 raise ClientException('Add should be provided in a key-value fashion')
143 for key
, value
in add_roles
.items():
144 if not isinstance(value
, bool):
145 raise ClientException("Value '{}' in a role permissions should be boolean".format(key
))
147 new_role_obj
["permissions"][key
] = value
148 if not new_role_obj
["permissions"]:
149 del new_role_obj
["permissions"]
151 http_code
, resp
= self
._http
.patch_cmd(endpoint
='{}/{}'.format(self
._apiBase
, role_obj
['_id']),
152 postfields_dict
=new_role_obj
)
153 # print('HTTP CODE: {}'.format(http_code))
154 # print('RESP: {}'.format(resp))
155 if http_code
in (200, 201, 202):
157 resp
= json
.loads(resp
)
158 if not resp
or 'id' not in resp
:
159 raise OsmHttpException('Unexpected response from server - {}'.format(
162 elif http_code
== 204:
168 # msg = json.loads(resp)
171 # raise ClientException("Failed to update role {} - {}".format(name, msg))
173 def delete(self
, name
, force
=False):
175 Deletes an OSM role identified by name.
179 :raises ClientException: when fails to delete a role.
181 self
._logger
.debug("")
182 self
._client
.get_token()
183 role
= self
.get(name
)
186 querystring
= '?FORCE=True'
187 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
188 role
['_id'], querystring
))
189 # print('HTTP CODE: {}'.format(http_code))
190 # print('RESP: {}'.format(resp))
192 print('Deletion in progress')
193 elif http_code
== 204:
195 elif resp
and 'result' in resp
:
201 msg
= json
.loads(resp
)
204 raise OsmHttpException("Failed to delete role {} - {}".format(name
, msg
))
206 def list(self
, filter=None):
208 Returns the list of OSM role.
213 self
._logger
.debug("")
214 self
._client
.get_token()
217 filter_string
= '?{}'.format(filter)
218 _
, resp
= self
._http
.get2_cmd('{}{}'.format(self
._apiBase
, filter_string
))
219 # print('RESP: {}'.format(resp))
221 return json
.loads(resp
)
226 Returns a specific OSM role based on name or id.
229 :raises NotFound: when the role is not found.
230 :returns: the specified role.
232 self
._logger
.debug("")
233 self
._client
.get_token()
234 if utils
.validate_uuid4(name
):
235 for role
in self
.list():
236 if name
== role
['_id']:
239 for role
in self
.list():
240 if name
== role
['name']:
242 raise NotFound("Role {} not found".format(name
))