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
29 class Project(object):
30 def __init__(self
, http
=None, client
=None):
33 self
._logger
= logging
.getLogger('osmclient')
34 self
._apiName
= '/admin'
35 self
._apiVersion
= '/v1'
36 self
._apiResource
= '/projects'
37 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
38 self
._apiVersion
, self
._apiResource
)
40 def create(self
, name
, project
):
41 """Creates a new OSM project
43 self
._logger
.debug("")
44 self
._client
.get_token()
45 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
46 postfields_dict
=project
,
47 skip_query_admin
=True)
48 #print('HTTP CODE: {}'.format(http_code))
49 #print('RESP: {}'.format(resp))
50 #if http_code in (200, 201, 202, 204):
52 resp
= json
.loads(resp
)
53 if not resp
or 'id' not in resp
:
54 raise ClientException('unexpected response from server - {}'.format(
61 # msg = json.loads(resp)
64 # raise ClientException("failed to create project {} - {}".format(name, msg))
66 def update(self
, project
, project_changes
):
67 """Updates an OSM project identified by name
69 self
._logger
.debug("")
70 self
._client
.get_token()
71 proj
= self
.get(project
)
72 http_code
, resp
= self
._http
.patch_cmd(endpoint
='{}/{}'.format(self
._apiBase
, proj
['_id']),
73 postfields_dict
=project_changes
,
74 skip_query_admin
=True)
75 # print('HTTP CODE: {}'.format(http_code))
76 # print('RESP: {}'.format(resp))
77 if http_code
in (200, 201, 202):
79 resp
= json
.loads(resp
)
80 if not resp
or 'id' not in resp
:
81 raise ClientException('unexpected response from server - {}'.format(
84 elif http_code
== 204:
90 # msg = json.loads(resp)
93 # raise ClientException("failed to update project {} - {}".format(project, msg))
95 def delete(self
, name
, force
=False):
96 """Deletes an OSM project identified by name
98 self
._logger
.debug("")
99 self
._client
.get_token()
100 project
= self
.get(name
)
103 querystring
= '?FORCE=True'
104 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
105 project
['_id'], querystring
),
106 skip_query_admin
=True)
107 #print('HTTP CODE: {}'.format(http_code))
108 #print('RESP: {}'.format(resp))
110 print('Deletion in progress')
111 elif http_code
== 204:
113 elif resp
and 'result' in resp
:
119 # msg = json.loads(resp)
122 raise ClientException("failed to delete project {} - {}".format(name
, msg
))
124 def list(self
, filter=None):
125 """Returns the list of OSM projects
127 self
._logger
.debug("")
128 self
._client
.get_token()
131 filter_string
= '?{}'.format(filter)
132 _
, resp
= self
._http
.get2_cmd('{}{}'.format(self
._apiBase
,filter_string
),
133 skip_query_admin
=True)
134 #print('RESP: {}'.format(resp))
136 return json
.loads(resp
)
140 """Returns a specific OSM project based on name or id
142 self
._logger
.debug("")
143 self
._client
.get_token()
144 if utils
.validate_uuid4(name
):
145 for proj
in self
.list():
146 if name
== proj
['_id']:
149 for proj
in self
.list():
150 if name
== proj
['name']:
152 raise NotFound("Project {} not found".format(name
))