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 #print('HTTP CODE: {}'.format(http_code))
48 #print('RESP: {}'.format(resp))
49 #if http_code in (200, 201, 202, 204):
51 resp
= json
.loads(resp
)
52 if not resp
or 'id' not in resp
:
53 raise ClientException('unexpected response from server - {}'.format(
60 # msg = json.loads(resp)
63 # raise ClientException("failed to create project {} - {}".format(name, msg))
65 def update(self
, project
, project_changes
):
66 """Updates an OSM project identified by name
68 self
._logger
.debug("")
69 self
._client
.get_token()
70 proj
= self
.get(project
)
71 http_code
, resp
= self
._http
.patch_cmd(endpoint
='{}/{}'.format(self
._apiBase
, proj
['_id']),
72 postfields_dict
=project_changes
)
73 # print('HTTP CODE: {}'.format(http_code))
74 # print('RESP: {}'.format(resp))
75 if http_code
in (200, 201, 202):
77 resp
= json
.loads(resp
)
78 if not resp
or 'id' not in resp
:
79 raise ClientException('unexpected response from server - {}'.format(
82 elif http_code
== 204:
88 # msg = json.loads(resp)
91 # raise ClientException("failed to update project {} - {}".format(project, msg))
93 def delete(self
, name
, force
=False):
94 """Deletes an OSM project identified by name
96 self
._logger
.debug("")
97 self
._client
.get_token()
98 project
= self
.get(name
)
101 querystring
= '?FORCE=True'
102 http_code
, resp
= self
._http
.delete_cmd('{}/{}{}'.format(self
._apiBase
,
103 project
['_id'], querystring
))
104 #print('HTTP CODE: {}'.format(http_code))
105 #print('RESP: {}'.format(resp))
107 print('Deletion in progress')
108 elif http_code
== 204:
110 elif resp
and 'result' in resp
:
116 # msg = json.loads(resp)
119 raise ClientException("failed to delete project {} - {}".format(name
, msg
))
121 def list(self
, filter=None):
122 """Returns the list of OSM projects
124 self
._logger
.debug("")
125 self
._client
.get_token()
128 filter_string
= '?{}'.format(filter)
129 _
, resp
= self
._http
.get2_cmd('{}{}'.format(self
._apiBase
,filter_string
))
130 #print('RESP: {}'.format(resp))
132 return json
.loads(resp
)
136 """Returns a specific OSM project based on name or id
138 self
._logger
.debug("")
139 self
._client
.get_token()
140 if utils
.validate_uuid4(name
):
141 for proj
in self
.list():
142 if name
== proj
['_id']:
145 for proj
in self
.list():
146 if name
== proj
['name']:
148 raise NotFound("Project {} not found".format(name
))