ad8027d03d1aebb19ed3ca54d5fadc8c6904561e
[osm/osmclient.git] / osmclient / sol005 / user.py
1 #
2 # Copyright 2018 Telefonica Investigacion y Desarrollo S.A.U.
3 #
4 # All Rights Reserved.
5 #
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
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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
16 # under the License.
17
18 """
19 OSM user mgmt API
20 """
21
22 from osmclient.common import utils
23 from osmclient.common.exceptions import ClientException
24 from osmclient.common.exceptions import NotFound
25 import json
26 # import yaml
27
28
29 class User(object):
30 def __init__(self, http=None, client=None):
31 self._http = http
32 self._client = client
33 self._apiName = '/admin'
34 self._apiVersion = '/v1'
35 self._apiResource = '/users'
36 self._apiBase = '{}{}{}'.format(self._apiName,
37 self._apiVersion, self._apiResource)
38
39 def create(self, name, user):
40 """Creates a new OSM user
41 """
42 http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
43 postfields_dict=user)
44 #print('HTTP CODE: {}'.format(http_code))
45 #print('RESP: {}'.format(resp))
46 if http_code in (200, 201, 202, 204):
47 if resp:
48 resp = json.loads(resp)
49 if not resp or 'id' not in resp:
50 raise ClientException('unexpected response from server - {}'.format(
51 resp))
52 print(resp['id'])
53 else:
54 msg = ""
55 if resp:
56 try:
57 msg = json.loads(resp)
58 except ValueError:
59 msg = resp
60 raise ClientException("failed to create user {} - {}".format(name, msg))
61
62 def update(self, name, user):
63 """Updates an existing OSM user identified by name
64 """
65 myuser = self.get(name)
66 http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,myuser['_id']),
67 postfields_dict=user)
68 #print('HTTP CODE: {}'.format(http_code))
69 #print('RESP: {}'.format(resp))
70 if http_code in (200, 201, 202, 204):
71 if resp:
72 resp = json.loads(resp)
73 if not resp or 'id' not in resp:
74 raise ClientException('unexpected response from server - {}'.format(
75 resp))
76 print(resp['id'])
77 else:
78 msg = ""
79 if resp:
80 try:
81 msg = json.loads(resp)
82 except ValueError:
83 msg = resp
84 raise ClientException("failed to update user {} - {}".format(name, msg))
85
86 def delete(self, name, force=False):
87 """Deletes an existing OSM user identified by name
88 """
89 user = self.get(name)
90 querystring = ''
91 if force:
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))
97 if http_code == 202:
98 print('Deletion in progress')
99 elif http_code == 204:
100 print('Deleted')
101 elif resp and 'result' in resp:
102 print('Deleted')
103 else:
104 msg = ""
105 if resp:
106 try:
107 msg = json.loads(resp)
108 except ValueError:
109 msg = resp
110 raise ClientException("failed to delete user {} - {}".format(name, msg))
111
112 def list(self, filter=None):
113 """Returns the list of OSM users
114 """
115 filter_string = ''
116 if filter:
117 filter_string = '?{}'.format(filter)
118 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
119 #print('RESP: {}'.format(resp))
120 if resp:
121 return resp
122 return list()
123
124 def get(self, name):
125 """Returns an OSM user based on name or id
126 """
127 if utils.validate_uuid4(name):
128 for user in self.list():
129 if name == user['_id']:
130 return user
131 else:
132 for user in self.list():
133 if name == user['username']:
134 return user
135 raise NotFound("User {} not found".format(name))
136
137