1f487ded0aa5edf54c1119813cec8fbe19e6b0bb
[osm/osmclient.git] / osmclient / sol005 / repo.py
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13 #
14
15 """
16 OSM Repo API handling
17 """
18
19 from osmclient.common import utils
20 from osmclient.common.exceptions import OsmHttpException
21 from osmclient.common.exceptions import ClientException
22 from osmclient.common.exceptions import NotFound
23 import json
24
25 class Repo(object):
26 def __init__(self, http=None, client=None):
27 self._http = http
28 self._client = client
29 self._apiName = '/admin'
30 self._apiVersion = '/v1'
31 self._apiResource = '/k8srepos'
32 self._apiBase = '{}{}{}'.format(self._apiName,
33 self._apiVersion, self._apiResource)
34
35 def create(self, name, repo):
36 self._client.get_token()
37 http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
38 postfields_dict=repo)
39 #print 'HTTP CODE: {}'.format(http_code)
40 #print 'RESP: {}'.format(resp)
41 #if http_code in (200, 201, 202, 204):
42 if resp:
43 resp = json.loads(resp)
44 if not resp or 'id' not in resp:
45 raise OsmHttpException('unexpected response from server - {}'.format(
46 resp))
47 print(resp['id'])
48 #else:
49 # msg = ""
50 # if resp:
51 # try:
52 # msg = json.loads(resp)
53 # except ValueError:
54 # msg = resp
55 # raise ClientException("failed to add repo {} - {}".format(name, msg))
56
57 def update(self, name, repo):
58 self._client.get_token()
59 repo_dict = self.get(name)
60 http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,repo_dict['_id']),
61 postfields_dict=repo)
62 # print 'HTTP CODE: {}'.format(http_code)
63 # print 'RESP: {}'.format(resp)
64 #if http_code in (200, 201, 202, 204):
65 # pass
66 #else:
67 # msg = ""
68 # if resp:
69 # try:
70 # msg = json.loads(resp)
71 # except ValueError:
72 # msg = resp
73 # raise ClientException("failed to update repo {} - {}".format(name, msg))
74
75 def get_id(self, name):
76 """Returns a repo id from a repo name
77 """
78 self._client.get_token()
79 for repo in self.list():
80 if name == repo['name']:
81 return repo['_id']
82 raise NotFound("Repo {} not found".format(name))
83
84 def delete(self, name, force=False):
85 self._client.get_token()
86 repo_id = name
87 if not utils.validate_uuid4(name):
88 repo_id = self.get_id(name)
89 querystring = ''
90 if force:
91 querystring = '?FORCE=True'
92 http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
93 repo_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 else:
101 msg = ""
102 if resp:
103 try:
104 msg = json.loads(resp)
105 except ValueError:
106 msg = resp
107 raise ClientException("failed to delete repo {} - {}".format(name, msg))
108
109 def list(self, filter=None):
110 """Returns a list of repos
111 """
112 self._client.get_token()
113 filter_string = ''
114 if filter:
115 filter_string = '?{}'.format(filter)
116 _, resp = self._http.get2_cmd('{}{}'.format(self._apiBase,filter_string))
117 if resp:
118 return json.loads(resp)
119 return list()
120
121 def get(self, name):
122 """Returns a repo based on name or id
123 """
124 self._client.get_token()
125 repo_id = name
126 if not utils.validate_uuid4(name):
127 repo_id = self.get_id(name)
128 _, resp = self._http.get2_cmd('{}/{}'.format(self._apiBase,repo_id))
129 # if not resp or '_id' not in resp:
130 # raise ClientException('failed to get repo info: '.format(resp))
131 # else:
132 if resp:
133 return json.loads(resp)
134 raise NotFound("Repo {} not found".format(name))
135