ad8027d03d1aebb19ed3ca54d5fadc8c6904561e
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 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
44 #print('HTTP CODE: {}'.format(http_code))
45 #print('RESP: {}'.format(resp))
46 if http_code
in (200, 201, 202, 204):
48 resp
= json
.loads(resp
)
49 if not resp
or 'id' not in resp
:
50 raise ClientException('unexpected response from server - {}'.format(
57 msg
= json
.loads(resp
)
60 raise ClientException("failed to create user {} - {}".format(name
, msg
))
62 def update(self
, name
, user
):
63 """Updates an existing OSM user identified by name
65 myuser
= self
.get(name
)
66 http_code
, resp
= self
._http
.put_cmd(endpoint
='{}/{}'.format(self
._apiBase
,myuser
['_id']),
68 #print('HTTP CODE: {}'.format(http_code))
69 #print('RESP: {}'.format(resp))
70 if http_code
in (200, 201, 202, 204):
72 resp
= json
.loads(resp
)
73 if not resp
or 'id' not in resp
:
74 raise ClientException('unexpected response from server - {}'.format(
81 msg
= json
.loads(resp
)
84 raise ClientException("failed to update user {} - {}".format(name
, msg
))
86 def delete(self
, name
, force
=False):
87 """Deletes an existing OSM user identified by name
92 querystring
= '?FORCE=True'
93 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
94 user
['_id'], querystring
))
95 #print('HTTP CODE: {}'.format(http_code))
96 #print('RESP: {}'.format(resp))
98 print('Deletion in progress')
99 elif http_code
== 204:
101 elif resp
and 'result' in resp
:
107 msg
= json
.loads(resp
)
110 raise ClientException("failed to delete user {} - {}".format(name
, msg
))
112 def list(self
, filter=None):
113 """Returns the list of OSM users
117 filter_string
= '?{}'.format(filter)
118 resp
= self
._http
.get_cmd('{}{}'.format(self
._apiBase
,filter_string
))
119 #print('RESP: {}'.format(resp))
125 """Returns an OSM user based on name or id
127 if utils
.validate_uuid4(name
):
128 for user
in self
.list():
129 if name
== user
['_id']:
132 for user
in self
.list():
133 if name
== user
['username']:
135 raise NotFound("User {} not found".format(name
))