all-projects and public general options
[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 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 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):
51 if resp:
52 resp = json.loads(resp)
53 if not resp or 'id' not in resp:
54 raise ClientException('unexpected response from server - {}'.format(
55 resp))
56 print(resp['id'])
57 #else:
58 # msg = ""
59 # if resp:
60 # try:
61 # msg = json.loads(resp)
62 # except ValueError:
63 # msg = resp
64 # raise ClientException("failed to create project {} - {}".format(name, msg))
65
66 def update(self, project, project_changes):
67 """Updates an OSM project identified by name
68 """
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):
78 if resp:
79 resp = json.loads(resp)
80 if not resp or 'id' not in resp:
81 raise ClientException('unexpected response from server - {}'.format(
82 resp))
83 print(resp['id'])
84 elif http_code == 204:
85 print("Updated")
86 #else:
87 # msg = ""
88 # if resp:
89 # try:
90 # msg = json.loads(resp)
91 # except ValueError:
92 # msg = resp
93 # raise ClientException("failed to update project {} - {}".format(project, msg))
94
95 def delete(self, name, force=False):
96 """Deletes an OSM project identified by name
97 """
98 self._logger.debug("")
99 self._client.get_token()
100 project = self.get(name)
101 querystring = ''
102 if force:
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))
109 if http_code == 202:
110 print('Deletion in progress')
111 elif http_code == 204:
112 print('Deleted')
113 elif resp and 'result' in resp:
114 print('Deleted')
115 else:
116 msg = resp or ""
117 # if resp:
118 # try:
119 # msg = json.loads(resp)
120 # except ValueError:
121 # msg = resp
122 raise ClientException("failed to delete project {} - {}".format(name, msg))
123
124 def list(self, filter=None):
125 """Returns the list of OSM projects
126 """
127 self._logger.debug("")
128 self._client.get_token()
129 filter_string = ''
130 if filter:
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))
135 if resp:
136 return json.loads(resp)
137 return list()
138
139 def get(self, name):
140 """Returns a specific OSM project based on name or id
141 """
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']:
147 return proj
148 else:
149 for proj in self.list():
150 if name == proj['name']:
151 return proj
152 raise NotFound("Project {} not found".format(name))
153