blob: 0473f29dd1cd703e8c3e853296b3501479d8093c [file] [log] [blame]
Mykyta Karpinf4a1c272019-05-29 18:47:01 +03001package com.mirantis.mk
2
3/**
4 *
5 * Functions to work with Harbor Api
6 *
7 */
8
9 /**
10 * Returns a list of Harbor projects (hash maps) containing common pattern in name
11 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
12 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
13 * Auth field should provide string with base64 encoded presentation
14 * of string 'username:password' for connection to Harbor Api
15 * @param projectPattern String contains full name or part of project name
16 * @return list
17 */
18def getHarborProjectsByPattern(harborApi, projectPattern){
19 def http = new com.mirantis.mk.Http()
20 def common = new com.mirantis.mk.Common()
21 def auth = harborApi['auth']
22 def headers = ['Authorization': "Basic ${auth}"]
23 def res = http.restGet(harborApi, "/projects?name=${projectPattern}", null, headers)
24 if (!(res instanceof List)){
25 common.warningMsg("No projects found for the pattern ${projectPattern}, The result is ${res}")
26 return []
27 }
28 return res
29}
30
31 /**
32 * Returns an id of a project
33 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
34 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
35 * Auth field should provide string with base64 encoded presentation
36 * of string 'username:password' for connection to Harbor Api
37 * @param projectName String project name to search for
38 * @return string
39 */
40def getHarborProjectByName(harborApi, projectName){
41 def plist = getHarborProjectsByPattern(harborApi, projectName)
42 def id = null
43 for (i in plist){
44 if (i.get('name') == projectName){
45 id = i['project_id']
46 }
47 }
48 return id
49}
50
51 /**
52 * Creates a project in Harbor, returns string
53 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
54 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
55 * Auth field should provide string with base64 encoded presentation
56 * of string 'username:password' for connection to Harbor Api
57 * @param projectName String project name to create
58 * @param isPublic Bool whether to create public project
59 * @return string
60 */
61def createHarborProject(harborApi, projectName, isPublic = true){
62 def http = new com.mirantis.mk.Http()
63 def data = ['project_name': projectName, 'metadata': ['public': isPublic.toString()]]
64 def auth = harborApi['auth']
65 def headers = ['Authorization': "Basic ${auth}"]
66 return http.restPost(harborApi, '/projects/', data, headers)
67}
68
69 /**
70 * Deletes a project in Harbor returns string
71 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
72 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
73 * Auth field should provide string with base64 encoded presentation
74 * of string 'username:password' for connection to Harbor Api
75 * @param projectId String project id to delete
76 * @return string
77 */
78def deleteHarborProject(harborApi, projectId){
79 def http = new com.mirantis.mk.Http()
80 def auth = harborApi['auth']
81 def headers = ['Authorization': "Basic ${auth}"]
82 return http.restDelete(harborApi, "/projects/${projectId}", null, headers)
83}
84
85 /**
86 * Returns a list of Helm charts (hash maps) from repo
87 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
88 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
89 * Auth field should provide string with base64 encoded presentation
90 * of string 'username:password' for connection to Harbor Api
91 * @param repoName String name of Helm charts repo
92 * @return list
93 */
94def getHarborHelmCharts(harborApi, repoName){
95 def http = new com.mirantis.mk.Http()
96 def auth = harborApi['auth']
97 def headers = ['Authorization': "Basic ${auth}"]
98 return http.restGet(harborApi, "/chartrepo/${repoName}/charts", null, headers)
99}
100
101 /**
102 * Deletes a Helm chart from repo, returns string
103 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
104 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
105 * Auth field should provide string with base64 encoded presentation
106 * of string 'username:password' for connection to Harbor Api
107 * @param repoName String name of Helm charts repo
108 * @param chartName String name of chart to remove
109 * @return string
110 */
111 def deleteHarborHelmChart(harborApi, repoName, chartName){
112 def http = new com.mirantis.mk.Http()
113 def auth = harborApi['auth']
114 def headers = ['Authorization': "Basic ${auth}"]
115 return http.restDelete(harborApi, "/chartrepo/${repoName}/charts/${chartName}", null, headers)
116}
117
118 /**
119 * Publishes a Helm chart to Harbor repo. TODO(mkarpin): currently http.restCall doesn't support
120 * multipart/form-data content type for POST request, when this support is added, this method
121 * should be switched to restCall.
122 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
123 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
124 * Auth field should provide string with base64 encoded presentation
125 * of string 'username:password' for connection to Harbor Api
126 * @param repoName String name of Helm charts repo
127 * @param chartFile String name of file with Helm chart
128 * @param chartFile String name of file with signature of Helm chart
129 * @return string
130 */
131def publishHarborHelmChart(harborApi, repoName, chartFile, provFile = null){
132 def http = new com.mirantis.mk.Http()
133 def common = new com.mirantis.mk.Common()
134 def data = "-F 'chart=@${chartFile}'"
135 def res = [:]
136 if (provFile){
137 data += ' '
138 data += "-F 'prov=@${provFile}'"
139 }
140 def auth = harborApi['auth']
141 def headers = "-H 'Authorization: Basic ${auth}'"
142 def repo_url = "${harborApi['url']}/chartrepo/${repoName}/charts"
143
144 common.infoMsg("Publishing chart ${chartFile} to ${repo_url}")
145 res = common.shCmdStatus("set +x; curl --fail ${headers} ${data} ${repo_url}")
146 if (res['status'] == 0){
147 return res['stdout']
148 } else {
149 error "Chart publishing failed with error ${res['stderr']}"
150 }
151}