github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/apiKeyValueMapDownload.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "io" 6 "net/http" 7 "os" 8 9 "github.com/SAP/jenkins-library/pkg/cpi" 10 piperhttp "github.com/SAP/jenkins-library/pkg/http" 11 "github.com/SAP/jenkins-library/pkg/log" 12 "github.com/SAP/jenkins-library/pkg/telemetry" 13 "github.com/pkg/errors" 14 ) 15 16 func apiKeyValueMapDownload(config apiKeyValueMapDownloadOptions, telemetryData *telemetry.CustomData) { 17 // Utils can be used wherever the command.ExecRunner interface is expected. 18 // It can also be used for example as a mavenExecRunner. 19 httpClient := &piperhttp.Client{} 20 21 // For HTTP calls import piperhttp "github.com/SAP/jenkins-library/pkg/http" 22 // and use a &piperhttp.Client{} in a custom system 23 // Example: step checkmarxExecuteScan.go 24 25 // Error situations should be bubbled up until they reach the line below which will then stop execution 26 // through the log.Entry().Fatal() call leading to an os.Exit(1) in the end. 27 err := runApiKeyValueMapDownload(&config, telemetryData, httpClient) 28 if err != nil { 29 log.Entry().WithError(err).Fatal("step execution failed") 30 } 31 } 32 33 func runApiKeyValueMapDownload(config *apiKeyValueMapDownloadOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error { 34 clientOptions := piperhttp.ClientOptions{} 35 header := make(http.Header) 36 header.Add("Accept", "application/json") 37 serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey) 38 if err != nil { 39 return err 40 } 41 downloadkeyValueMapArtifactURL := fmt.Sprintf("%s/apiportal/api/1.0/Management.svc/KeyMapEntries('%s')", serviceKey.OAuth.Host, config.KeyValueMapName) 42 tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, 43 Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, Client: httpClient} 44 token, err := cpi.CommonUtils.GetBearerToken(tokenParameters) 45 if err != nil { 46 return errors.Wrap(err, "failed to fetch Bearer Token") 47 } 48 clientOptions.Token = fmt.Sprintf("Bearer %s", token) 49 httpClient.SetOptions(clientOptions) 50 httpMethod := http.MethodGet 51 downloadResp, httpErr := httpClient.SendRequest(httpMethod, downloadkeyValueMapArtifactURL, nil, header, nil) 52 if httpErr != nil { 53 return errors.Wrapf(httpErr, "HTTP %v request to %v failed with error", httpMethod, downloadkeyValueMapArtifactURL) 54 } 55 if downloadResp == nil { 56 return errors.Errorf("did not retrieve a HTTP response: %v", httpErr) 57 } 58 if downloadResp != nil && downloadResp.Body != nil { 59 defer downloadResp.Body.Close() 60 } 61 if downloadResp.StatusCode == 200 { 62 csvFilePath := config.DownloadPath 63 file, err := os.Create(csvFilePath) 64 if err != nil { 65 return errors.Wrapf(err, "Failed to create api key value map CSV file") 66 } 67 _, err = io.Copy(file, downloadResp.Body) 68 if err != nil { 69 return err 70 } 71 return nil 72 } 73 responseBody, readErr := io.ReadAll(downloadResp.Body) 74 75 if readErr != nil { 76 return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code : %v", downloadResp.StatusCode) 77 } 78 log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code : %v", responseBody, downloadResp.StatusCode) 79 return errors.Errorf("api Key value map download failed, Response Status code: %v", downloadResp.StatusCode) 80 }