github.com/SAP/jenkins-library@v1.362.0/cmd/apiProviderUpload.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/SAP/jenkins-library/pkg/apim"
    10  	"github.com/SAP/jenkins-library/pkg/cpi"
    11  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    12  	"github.com/SAP/jenkins-library/pkg/log"
    13  	"github.com/SAP/jenkins-library/pkg/piperutils"
    14  	"github.com/SAP/jenkins-library/pkg/telemetry"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  func apiProviderUpload(config apiProviderUploadOptions, telemetryData *telemetry.CustomData) {
    19  	httpClient := &piperhttp.Client{}
    20  	err := runApiProviderUpload(&config, telemetryData, httpClient)
    21  	if err != nil {
    22  		log.Entry().WithError(err).Fatal("step execution failed")
    23  	}
    24  }
    25  
    26  func runApiProviderUpload(config *apiProviderUploadOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
    27  
    28  	apimData := apim.Bundle{APIServiceKey: config.APIServiceKey, Client: httpClient}
    29  	err := apim.Utils.InitAPIM(&apimData)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	return createApiProvider(config, apimData, os.ReadFile)
    34  }
    35  
    36  func createApiProvider(config *apiProviderUploadOptions, apim apim.Bundle, readFile func(string) ([]byte, error)) error {
    37  	httpClient := apim.Client
    38  	httpMethod := http.MethodPost
    39  	uploadApiProviderStatusURL := fmt.Sprintf("%s/apiportal/api/1.0/Management.svc/APIProviders", apim.Host)
    40  	header := make(http.Header)
    41  	header.Add("Content-Type", "application/json")
    42  	header.Add("Accept", "application/json")
    43  
    44  	exists, _ := piperutils.FileExists(config.FilePath)
    45  	if !exists {
    46  		return errors.New("Missing API Provider input file")
    47  	}
    48  
    49  	payload, err := readFile(config.FilePath)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	apim.Payload = string(payload)
    54  
    55  	if !apim.IsPayloadJSON() {
    56  		return errors.New("invalid JSON content in the input file")
    57  	}
    58  
    59  	apiProviderUploadStatusResp, httpErr := httpClient.SendRequest(httpMethod, uploadApiProviderStatusURL, bytes.NewBuffer(payload), header, nil)
    60  	failureMessage := "Failed to create API provider artefact"
    61  	successMessage := "Successfully created api provider artefact in API Portal"
    62  	httpFileUploadRequestParameters := cpi.HttpFileUploadRequestParameters{
    63  		ErrMessage:     failureMessage,
    64  		FilePath:       config.FilePath,
    65  		Response:       apiProviderUploadStatusResp,
    66  		HTTPMethod:     httpMethod,
    67  		HTTPURL:        uploadApiProviderStatusURL,
    68  		HTTPErr:        httpErr,
    69  		SuccessMessage: successMessage}
    70  	return cpi.HTTPUploadUtils.HandleHTTPFileUploadResponse(httpFileUploadRequestParameters)
    71  }