5c5786cc58509d1534f0f21606e0b3190e1c9bc0
[osm/osmclient.git] / osmclient / sol005 / project.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 project 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
27
28 class Project(object):
29 def __init__(self, http=None, client=None):
30 self._http = http
31 self._client = client
32 self._apiName = '/admin'
33 self._apiVersion = '/v1'
34 self._apiResource = '/projects'
35 self._apiBase = '{}{}{}'.format(self._apiName,
36 self._apiVersion, self._apiResource)
37
38 def create(self, name, project):
39 """Creates a new OSM project
40 """
41 http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
42 postfields_dict=project)
43 #print('HTTP CODE: {}'.format(http_code))
44 #print('RESP: {}'.format(resp))
45 if http_code in (200, 201, 202, 204):
46 if resp:
47 resp = json.loads(resp)
48 if not resp or 'id' not in resp:
49 raise ClientException('unexpected response from server - {}'.format(
50 resp))
51 print(resp['id'])
52 else:
53 msg = ""
54 if resp:
55 try:
56 msg = json.loads(resp)
57 except ValueError:
58 msg = resp
59 raise ClientException("failed to create project {} - {}".format(name, msg))
60
61 def update(self, name, project):
62 """Updates an OSM project identified by name
63 """
64 proj = self.get(name)
65 http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,proj['_id']),
66 postfields_dict=project)
67 #print('HTTP CODE: {}'.format(http_code))
68 #print('RESP: {}'.format(resp))
69 if http_code in (200, 201, 202, 204):
70 if resp:
71 resp = json.loads(resp)
72 if not resp or 'id' not in resp:
73 raise ClientException('unexpected response from server - {}'.format(
74 resp))
75 print(resp['id'])
76 else:
77 msg = ""
78 if resp:
79 try:
80 msg = json.loads(resp)
81 except ValueError:
82 msg = resp
83 raise ClientException("failed to update project {} - {}".format(name, msg))
84
85 def delete(self, name, force=False):
86 """Deletes an OSM project identified by name
87 """
88 project = self.get(name)
89 querystring = ''
90 if force:
91 querystring = '?FORCE=True'
92 http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
93 project['_id'], querystring))
94 #print('HTTP CODE: {}'.format(http_code))
95 #print('RESP: {}'.format(resp))
96 if http_code == 202:
97 print('Deletion in progress')
98 elif http_code == 204:
99 print('Deleted')
100 elif resp and 'result' in resp:
101 print('Deleted')
102 else:
103 msg = ""
104 if resp:
105 try:
106 msg = json.loads(resp)
107 except ValueError:
108 msg = resp
109 raise ClientException("failed to delete project {} - {}".format(name, msg))
110
111 def list(self, filter=None):
112 """Returns the list of OSM projects
113 """
114 filter_string = ''
115 if filter:
116 filter_string = '?{}'.format(filter)
117 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
118 #print('RESP: {}'.format(resp))
119 if resp:
120 return resp
121 return list()
122
123 def get(self, name):
124 """Returns a specific OSM project based on name or id
125 """
126 if utils.validate_uuid4(name):
127 for proj in self.list():
128 if name == proj['_id']:
129 return proj
130 else:
131 for proj in self.list():
132 if name == proj['name']:
133 return proj
134 raise NotFound("Project {} not found".format(name))
135
136