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