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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
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
26 def __init__(self
, http
=None, client
=None):
29 self
._apiName
= '/admin'
30 self
._apiVersion
= '/v1'
31 self
._apiResource
= '/k8srepos'
32 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
33 self
._apiVersion
, self
._apiResource
)
35 def create(self
, name
, repo
):
36 self
._client
.get_token()
37 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
39 #print 'HTTP CODE: {}'.format(http_code)
40 #print 'RESP: {}'.format(resp)
41 #if http_code in (200, 201, 202, 204):
43 resp
= json
.loads(resp
)
44 if not resp
or 'id' not in resp
:
45 raise OsmHttpException('unexpected response from server - {}'.format(
52 # msg = json.loads(resp)
55 # raise ClientException("failed to add repo {} - {}".format(name, msg))
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']),
62 # print 'HTTP CODE: {}'.format(http_code)
63 # print 'RESP: {}'.format(resp)
64 #if http_code in (200, 201, 202, 204):
70 # msg = json.loads(resp)
73 # raise ClientException("failed to update repo {} - {}".format(name, msg))
75 def get_id(self
, name
):
76 """Returns a repo id from a repo name
78 self
._client
.get_token()
79 for repo
in self
.list():
80 if name
== repo
['name']:
82 raise NotFound("Repo {} not found".format(name
))
84 def delete(self
, name
, force
=False):
85 self
._client
.get_token()
87 if not utils
.validate_uuid4(name
):
88 repo_id
= self
.get_id(name
)
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)
97 print('Deletion in progress')
98 elif http_code
== 204:
104 msg
= json
.loads(resp
)
107 raise ClientException("failed to delete repo {} - {}".format(name
, msg
))
109 def list(self
, filter=None):
110 """Returns a list of repos
112 self
._client
.get_token()
115 filter_string
= '?{}'.format(filter)
116 _
, resp
= self
._http
.get2_cmd('{}{}'.format(self
._apiBase
,filter_string
))
118 return json
.loads(resp
)
122 """Returns a repo based on name or id
124 self
._client
.get_token()
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))
133 return json
.loads(resp
)
134 raise NotFound("Repo {} not found".format(name
))