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