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