mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
ef2db4c2b0
* ApiProviderUpload Command * formatting fix * formatting fix * formatting fix * formatting fix * CodeReview Changes * CodeReview Fix * CodeReview fix * CodeReview Fix * CodeReview FIx * CodeReview FIxes * CodeReview Fixes * CodeReview Fixes * CodeReview Fixes * CodeReview Fix * CodeReview Fix * CodeReview Fixes * CodeReview Fix * Doc Fixes * CodeReview Fix * CodeReview Fixes * Doc Fixes * Update cmd/apiProviderUpload_test.go Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * CodeReview FIxes * Update cmd/apiProviderUpload.go Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * CodeReview Fixes Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>
72 lines
2.4 KiB
Go
72 lines
2.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"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, ioutil.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)
|
|
}
|