1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/apiProviderUpload.go
Jordi van Liempt 0ba4c2206c
chore(deps): Replace io/ioutil package (#4494)
* update all deprecated ioutil usages

* forgotten changes

* add missing imports

* undo changing comment

* add missing 'os' import

* fix integration test

---------

Co-authored-by: I557621 <jordi.van.liempt@sap.com>
Co-authored-by: Gulom Alimov <gulomjon.alimov@sap.com>
2023-08-16 12:57:04 +02:00

72 lines
2.4 KiB
Go

package cmd
import (
"bytes"
"fmt"
"net/http"
"os"
"github.com/SAP/jenkins-library/pkg/apim"
"github.com/SAP/jenkins-library/pkg/cpi"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)
func apiProviderUpload(config apiProviderUploadOptions, telemetryData *telemetry.CustomData) {
httpClient := &piperhttp.Client{}
err := runApiProviderUpload(&config, telemetryData, httpClient)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runApiProviderUpload(config *apiProviderUploadOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
apimData := apim.Bundle{APIServiceKey: config.APIServiceKey, Client: httpClient}
err := apim.Utils.InitAPIM(&apimData)
if err != nil {
return err
}
return createApiProvider(config, apimData, os.ReadFile)
}
func createApiProvider(config *apiProviderUploadOptions, apim apim.Bundle, readFile func(string) ([]byte, error)) error {
httpClient := apim.Client
httpMethod := http.MethodPost
uploadApiProviderStatusURL := fmt.Sprintf("%s/apiportal/api/1.0/Management.svc/APIProviders", apim.Host)
header := make(http.Header)
header.Add("Content-Type", "application/json")
header.Add("Accept", "application/json")
exists, _ := piperutils.FileExists(config.FilePath)
if !exists {
return errors.New("Missing API Provider input file")
}
payload, err := readFile(config.FilePath)
if err != nil {
return err
}
apim.Payload = string(payload)
if !apim.IsPayloadJSON() {
return errors.New("invalid JSON content in the input file")
}
apiProviderUploadStatusResp, httpErr := httpClient.SendRequest(httpMethod, uploadApiProviderStatusURL, bytes.NewBuffer(payload), header, nil)
failureMessage := "Failed to create API provider artefact"
successMessage := "Successfully created api provider artefact in API Portal"
httpFileUploadRequestParameters := cpi.HttpFileUploadRequestParameters{
ErrMessage: failureMessage,
FilePath: config.FilePath,
Response: apiProviderUploadStatusResp,
HTTPMethod: httpMethod,
HTTPURL: uploadApiProviderStatusURL,
HTTPErr: httpErr,
SuccessMessage: successMessage}
return cpi.HTTPUploadUtils.HandleHTTPFileUploadResponse(httpFileUploadRequestParameters)
}