fix bug 647 user creation with project prompt
[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 if len(user["projects"]) == 1:
43 user["projects"] = user["projects"][0].split(",")
44 http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
45 postfields_dict=user)
46 #print('HTTP CODE: {}'.format(http_code))
47 #print('RESP: {}'.format(resp))
48 if http_code in (200, 201, 202, 204):
49 if resp:
50 resp = json.loads(resp)
51 if not resp or 'id' not in resp:
52 raise ClientException('unexpected response from server - {}'.format(
53 resp))
54 print(resp['id'])
55 else:
56 msg = ""
57 if resp:
58 try:
59 msg = json.loads(resp)
60 except ValueError:
61 msg = resp
62 raise ClientException("failed to create user {} - {}".format(name, msg))
63
64 def update(self, name, user):
65 """Updates an existing OSM user identified by name
66 """
67 myuser = self.get(name)
68 http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,myuser['_id']),
69 postfields_dict=user)
70 #print('HTTP CODE: {}'.format(http_code))
71 #print('RESP: {}'.format(resp))
72 if http_code in (200, 201, 202, 204):
73 if resp:
74 resp = json.loads(resp)
75 if not resp or 'id' not in resp:
76 raise ClientException('unexpected response from server - {}'.format(
77 resp))
78 print(resp['id'])
79 else:
80 msg = ""
81 if resp:
82 try:
83 msg = json.loads(resp)
84 except ValueError:
85 msg = resp
86 raise ClientException("failed to update user {} - {}".format(name, msg))
87
88 def delete(self, name, force=False):
89 """Deletes an existing OSM user identified by name
90 """
91 user = self.get(name)
92 querystring = ''
93 if force:
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))
99 if http_code == 202:
100 print('Deletion in progress')
101 elif http_code == 204:
102 print('Deleted')
103 elif resp and 'result' in resp:
104 print('Deleted')
105 else:
106 msg = ""
107 if resp:
108 try:
109 msg = json.loads(resp)
110 except ValueError:
111 msg = resp
112 raise ClientException("failed to delete user {} - {}".format(name, msg))
113
114 def list(self, filter=None):
115 """Returns the list of OSM users
116 """
117 filter_string = ''
118 if filter:
119 filter_string = '?{}'.format(filter)
120 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
121 #print('RESP: {}'.format(resp))
122 if resp:
123 return resp
124 return list()
125
126 def get(self, name):
127 """Returns an OSM user based on name or id
128 """
129 if utils.validate_uuid4(name):
130 for user in self.list():
131 if name == user['_id']:
132 return user
133 else:
134 for user in self.list():
135 if name == user['username']:
136 return user
137 raise NotFound("User {} not found".format(name))
138
139