2 # Copyright 2018 Telefonica Investigacion y Desarrollo S.A.U.
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
22 from osmclient
.common
import utils
23 from osmclient
.common
.exceptions
import ClientException
24 from osmclient
.common
.exceptions
import NotFound
30 def __init__(self
, http
=None, client
=None):
33 self
._apiName
= '/admin'
34 self
._apiVersion
= '/v1'
35 self
._apiResource
= '/users'
36 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
37 self
._apiVersion
, self
._apiResource
)
39 def create(self
, name
, user
):
40 """Creates a new OSM user
42 if 'projects' in user
and user
['projects'] is not None:
43 user
['projects'] = yaml
.safe_load(user
['projects'])
44 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
46 #print('HTTP CODE: {}'.format(http_code))
47 #print('RESP: {}'.format(resp))
48 if http_code
in (200, 201, 202, 204):
50 resp
= json
.loads(resp
)
51 if not resp
or 'id' not in resp
:
52 raise ClientException('unexpected response from server - {}'.format(
59 msg
= json
.loads(resp
)
62 raise ClientException("failed to create user {} - {}".format(name
, msg
))
64 def update(self
, name
, user
):
65 """Updates an existing OSM user identified by name
67 myuser
= self
.get(name
)
68 http_code
, resp
= self
._http
.put_cmd(endpoint
='{}/{}'.format(self
._apiBase
,myuser
['_id']),
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 ClientException('unexpected response from server - {}'.format(
83 msg
= json
.loads(resp
)
86 raise ClientException("failed to update user {} - {}".format(name
, msg
))
88 def delete(self
, name
, force
=False):
89 """Deletes an existing OSM user identified by name
94 querystring
= '?FORCE=True'
95 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
96 user
['_id'], querystring
))
97 #print('HTTP CODE: {}'.format(http_code))
98 #print('RESP: {}'.format(resp))
100 print('Deletion in progress')
101 elif http_code
== 204:
103 elif resp
and 'result' in resp
:
109 msg
= json
.loads(resp
)
112 raise ClientException("failed to delete user {} - {}".format(name
, msg
))
114 def list(self
, filter=None):
115 """Returns the list of OSM users
119 filter_string
= '?{}'.format(filter)
120 resp
= self
._http
.get_cmd('{}{}'.format(self
._apiBase
,filter_string
))
121 #print('RESP: {}'.format(resp))
127 """Returns an OSM user based on name or id
129 if utils
.validate_uuid4(name
):
130 for user
in self
.list():
131 if name
== user
['_id']:
134 for user
in self
.list():
135 if name
== user
['name']:
137 raise NotFound("User {} not found".format(name
))