blob: 5790fdfacf2011e7775ada8e3fd18cf773c09e02 [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)
Mykyta Karpinf4a1c272019-05-29 18:47:01 +030042 for (i in plist){
43 if (i.get('name') == projectName){
Mykyta Karpin5f1fd852019-06-04 15:46:16 +000044 return i['project_id']
Mykyta Karpinf4a1c272019-05-29 18:47:01 +030045 }
46 }
Mykyta Karpinf4a1c272019-05-29 18:47:01 +030047}
48
49 /**
50 * Creates a project in Harbor, returns string
51 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
52 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
53 * Auth field should provide string with base64 encoded presentation
54 * of string 'username:password' for connection to Harbor Api
55 * @param projectName String project name to create
56 * @param isPublic Bool whether to create public project
57 * @return string
58 */
59def createHarborProject(harborApi, projectName, isPublic = true){
60 def http = new com.mirantis.mk.Http()
61 def data = ['project_name': projectName, 'metadata': ['public': isPublic.toString()]]
62 def auth = harborApi['auth']
63 def headers = ['Authorization': "Basic ${auth}"]
64 return http.restPost(harborApi, '/projects/', data, headers)
65}
66
67 /**
68 * Deletes a project in Harbor returns string
69 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
70 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
71 * Auth field should provide string with base64 encoded presentation
72 * of string 'username:password' for connection to Harbor Api
73 * @param projectId String project id to delete
74 * @return string
75 */
76def deleteHarborProject(harborApi, projectId){
77 def http = new com.mirantis.mk.Http()
78 def auth = harborApi['auth']
79 def headers = ['Authorization': "Basic ${auth}"]
80 return http.restDelete(harborApi, "/projects/${projectId}", null, headers)
81}
82
83 /**
84 * Returns a list of Helm charts (hash maps) from repo
85 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
86 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
87 * Auth field should provide string with base64 encoded presentation
88 * of string 'username:password' for connection to Harbor Api
89 * @param repoName String name of Helm charts repo
90 * @return list
91 */
92def getHarborHelmCharts(harborApi, repoName){
93 def http = new com.mirantis.mk.Http()
94 def auth = harborApi['auth']
95 def headers = ['Authorization': "Basic ${auth}"]
96 return http.restGet(harborApi, "/chartrepo/${repoName}/charts", null, headers)
97}
98
99 /**
100 * Deletes a Helm chart from repo, returns string
101 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
102 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
103 * Auth field should provide string with base64 encoded presentation
104 * of string 'username:password' for connection to Harbor Api
105 * @param repoName String name of Helm charts repo
106 * @param chartName String name of chart to remove
107 * @return string
108 */
109 def deleteHarborHelmChart(harborApi, repoName, chartName){
110 def http = new com.mirantis.mk.Http()
111 def auth = harborApi['auth']
112 def headers = ['Authorization': "Basic ${auth}"]
113 return http.restDelete(harborApi, "/chartrepo/${repoName}/charts/${chartName}", null, headers)
114}
115
116 /**
117 * Publishes a Helm chart to Harbor repo. TODO(mkarpin): currently http.restCall doesn't support
118 * multipart/form-data content type for POST request, when this support is added, this method
119 * should be switched to restCall.
120 * @param harborApi HashMap contains 'url' and 'auth' fields. Url field is a string
121 * containing Harbor api url - e.g. http://[harbor_api_host]/api,
122 * Auth field should provide string with base64 encoded presentation
123 * of string 'username:password' for connection to Harbor Api
124 * @param repoName String name of Helm charts repo
125 * @param chartFile String name of file with Helm chart
126 * @param chartFile String name of file with signature of Helm chart
127 * @return string
128 */
129def publishHarborHelmChart(harborApi, repoName, chartFile, provFile = null){
130 def http = new com.mirantis.mk.Http()
131 def common = new com.mirantis.mk.Common()
132 def data = "-F 'chart=@${chartFile}'"
133 def res = [:]
134 if (provFile){
135 data += ' '
136 data += "-F 'prov=@${provFile}'"
137 }
138 def auth = harborApi['auth']
139 def headers = "-H 'Authorization: Basic ${auth}'"
140 def repo_url = "${harborApi['url']}/chartrepo/${repoName}/charts"
141
142 common.infoMsg("Publishing chart ${chartFile} to ${repo_url}")
143 res = common.shCmdStatus("set +x; curl --fail ${headers} ${data} ${repo_url}")
144 if (res['status'] == 0){
145 return res['stdout']
146 } else {
147 error "Chart publishing failed with error ${res['stderr']}"
148 }
149}