-
Notifications
You must be signed in to change notification settings - Fork 583
/
apiProviderUpload.go
71 lines (61 loc) · 2.37 KB
/
apiProviderUpload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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)
}