39d24538f6622381f11c422c14fe997221c514eb
[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 NotFound
24 from osmclient.common.exceptions import OsmHttpException
25 import json
26 import logging
27
28
29 class Project(object):
30 def __init__(self, http=None, client=None):
31 self._http = http
32 self._client = client
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)
39
40 def create(self, name, project):
41 """Creates a new OSM project
42 """
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):
50 if resp:
51 resp = json.loads(resp)
52 if not resp or 'id' not in resp:
53 raise OsmHttpException('unexpected response from server - {}'.format(
54 resp))
55 print(resp['id'])
56 #else:
57 # msg = ""
58 # if resp:
59 # try:
60 # msg = json.loads(resp)
61 # except ValueError:
62 # msg = resp
63 # raise ClientException("failed to create project {} - {}".format(name, msg))
64
65 def update(self, project, project_changes):
66 """Updates an OSM project identified by name
67 """
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):
76 if resp:
77 resp = json.loads(resp)
78 if not resp or 'id' not in resp:
79 raise OsmHttpException('unexpected response from server - {}'.format(
80 resp))
81 print(resp['id'])
82 elif http_code == 204:
83 print("Updated")
84 #else:
85 # msg = ""
86 # if resp:
87 # try:
88 # msg = json.loads(resp)
89 # except ValueError:
90 # msg = resp
91 # raise ClientException("failed to update project {} - {}".format(project, msg))
92
93 def delete(self, name, force=False):
94 """Deletes an OSM project identified by name
95 """
96 self._logger.debug("")
97 self._client.get_token()
98 project = self.get(name)
99 querystring = ''
100 if force:
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))
106 if http_code == 202:
107 print('Deletion in progress')
108 elif http_code == 204:
109 print('Deleted')
110 elif resp and 'result' in resp:
111 print('Deleted')
112 else:
113 msg = ""
114 if resp:
115 try:
116 msg = json.loads(resp)
117 except ValueError:
118 msg = resp
119 raise OsmHttpException("failed to delete project {} - {}".format(name, msg))
120
121 def list(self, filter=None):
122 """Returns the list of OSM projects
123 """
124 self._logger.debug("")
125 self._client.get_token()
126 filter_string = ''
127 if filter:
128 filter_string = '?{}'.format(filter)
129 _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string))
130 #print('RESP: {}'.format(resp))
131 if resp:
132 return json.loads(resp)
133 return list()
134
135 def get(self, name):
136 """Returns a specific OSM project based on name or id
137 """
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']:
143 return proj
144 else:
145 for proj in self.list():
146 if name == proj['name']:
147 return proj
148 raise NotFound("Project {} not found".format(name))
149