1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/cnbutils/registry/search.go
Pavel Busko 372cef04b4
feat(cnbBuild): added support for project.toml (#3163)
Co-authored-by: Sumit Kulhadia <sumit.kulhadia@sap.com>
Co-authored-by: Johannes Dillmann <j.dillmann@sap.com>
2021-10-11 11:10:21 +02:00

87 lines
2.1 KiB
Go

// Package registry provides utilities to search buildpacks using registry API
package registry
import (
"encoding/json"
"fmt"
"net/http"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
)
const (
defaultRegistryAPI = "https://registry.buildpacks.io/api/v1/buildpacks"
)
type latest struct {
Version string `json:"version"`
Misc map[string]interface{} `json:"-"`
}
type version struct {
Version string `json:"version"`
Link string `json:"_link"`
}
type response struct {
Latest latest `json:"latest"`
Versions []version `json:"versions"`
}
type versionResponse struct {
Addr string `json:"addr"`
Misc map[string]interface{} `json:"-"`
}
func SearchBuildpack(id, version string, httpClient piperhttp.Sender, baseApiURL string) (string, error) {
var apiResponse response
if baseApiURL == "" {
baseApiURL = defaultRegistryAPI
}
apiURL := fmt.Sprintf("%s/%s", baseApiURL, id)
rawResponse, err := httpClient.SendRequest(http.MethodGet, apiURL, nil, nil, nil)
if err != nil {
return "", err
}
defer rawResponse.Body.Close()
err = json.NewDecoder(rawResponse.Body).Decode(&apiResponse)
if err != nil {
return "", fmt.Errorf("unable to parse response from the %s, error: %s", apiURL, err.Error())
}
if version == "" {
version = apiResponse.Latest.Version
log.Entry().Infof("Version for the buildpack '%s' is not specified, using the latest '%s'", id, version)
}
for _, ver := range apiResponse.Versions {
if ver.Version == version {
return getImageAddr(ver.Link, httpClient)
}
}
return "", fmt.Errorf("version '%s' was not found for the buildpack '%s'", version, id)
}
func getImageAddr(link string, httpClient piperhttp.Sender) (string, error) {
var verResponse versionResponse
rawResponse, err := httpClient.SendRequest(http.MethodGet, link, nil, nil, nil)
if err != nil {
return "", err
}
defer rawResponse.Body.Close()
err = json.NewDecoder(rawResponse.Body).Decode(&verResponse)
if err != nil {
return "", fmt.Errorf("unable to parse response from the %s, error: %s", link, err.Error())
}
return verResponse.Addr, nil
}