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