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