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 ClientException
21 from osmclient
.common
.exceptions
import NotFound
25 def __init__(self
, http
=None, client
=None):
28 self
._apiName
= '/admin'
29 self
._apiVersion
= '/v1'
30 self
._apiResource
= '/k8srepos'
31 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
32 self
._apiVersion
, self
._apiResource
)
34 def create(self
, name
, repo
):
35 self
._client
.get_token()
36 http_code
, resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
38 #print 'HTTP CODE: {}'.format(http_code)
39 #print 'RESP: {}'.format(resp)
40 #if http_code in (200, 201, 202, 204):
42 resp
= json
.loads(resp
)
43 if not resp
or 'id' not in resp
:
44 raise ClientException('unexpected response from server - {}'.format(
51 # msg = json.loads(resp)
54 # raise ClientException("failed to add repo {} - {}".format(name, msg))
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']),
61 # print 'HTTP CODE: {}'.format(http_code)
62 # print 'RESP: {}'.format(resp)
63 #if http_code in (200, 201, 202, 204):
69 # msg = json.loads(resp)
72 # raise ClientException("failed to update repo {} - {}".format(name, msg))
74 def get_id(self
, name
):
75 """Returns a repo id from a repo name
77 self
._client
.get_token()
78 for repo
in self
.list():
79 if name
== repo
['name']:
81 raise NotFound("Repo {} not found".format(name
))
83 def delete(self
, name
, force
=False):
84 self
._client
.get_token()
86 if not utils
.validate_uuid4(name
):
87 repo_id
= self
.get_id(name
)
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)
96 print('Deletion in progress')
97 elif http_code
== 204:
103 # msg = json.loads(resp)
106 raise ClientException("failed to delete repo {} - {}".format(name
, msg
))
108 def list(self
, filter=None):
109 """Returns a list of repos
111 self
._client
.get_token()
114 filter_string
= '?{}'.format(filter)
115 _
, resp
= self
._http
.get2_cmd('{}{}'.format(self
._apiBase
,filter_string
))
117 return json
.loads(resp
)
121 """Returns a repo based on name or id
123 self
._client
.get_token()
125 if not utils
.validate_uuid4(name
):
126 repo_id
= self
.get_id(name
)
128 _
, resp
= self
._http
.get2_cmd('{}/{}'.format(self
._apiBase
,repo_id
))
130 resp
= json
.loads(resp
)
131 if not resp
or '_id' not in resp
:
132 raise ClientException('failed to get repo info: {}'.format(resp
))
135 raise NotFound("Repo {} not found".format(name
))