github.com/SAP/jenkins-library@v1.362.0/cmd/integrationArtifactGetServiceEndpoint.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "io" 6 "net/http" 7 "strings" 8 9 "github.com/Jeffail/gabs/v2" 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 integrationArtifactGetServiceEndpoint(config integrationArtifactGetServiceEndpointOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *integrationArtifactGetServiceEndpointCommonPipelineEnvironment) { 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 := runIntegrationArtifactGetServiceEndpoint(&config, telemetryData, httpClient, commonPipelineEnvironment) 29 if err != nil { 30 log.Entry().WithError(err).Fatal("step execution failed") 31 } 32 } 33 34 func runIntegrationArtifactGetServiceEndpoint(config *integrationArtifactGetServiceEndpointOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender, commonPipelineEnvironment *integrationArtifactGetServiceEndpointCommonPipelineEnvironment) 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 servieEndpointURL := fmt.Sprintf("%s/api/v1/ServiceEndpoints?$expand=EntryPoints", serviceKey.OAuth.Host) 43 tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, 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 := "GET" 51 serviceEndpointResp, httpErr := httpClient.SendRequest(httpMethod, servieEndpointURL, nil, header, nil) 52 53 if httpErr != nil { 54 return errors.Wrapf(httpErr, "HTTP %v request to %v failed with error", httpMethod, servieEndpointURL) 55 } 56 57 if serviceEndpointResp != nil && serviceEndpointResp.Body != nil { 58 defer serviceEndpointResp.Body.Close() 59 } 60 61 if serviceEndpointResp == nil { 62 return errors.Errorf("did not retrieve a HTTP response: %v", httpErr) 63 } 64 65 if serviceEndpointResp.StatusCode == 200 { 66 bodyText, readErr := io.ReadAll(serviceEndpointResp.Body) 67 if readErr != nil { 68 return errors.Wrap(readErr, "HTTP response body could not be read") 69 } 70 jsonResponse, parsingErr := gabs.ParseJSON([]byte(bodyText)) 71 if parsingErr != nil { 72 return errors.Wrapf(parsingErr, "HTTP response body could not be parsed as JSON: %v", string(bodyText)) 73 } 74 75 for _, child := range jsonResponse.S("d", "results").Children() { 76 iflowID := strings.ReplaceAll(child.Path("Name").String(), "\"", "") 77 if iflowID == config.IntegrationFlowID { 78 entryPoints := child.S("EntryPoints") 79 finalEndpoint := entryPoints.Path("results.0.Url").Data().(string) 80 commonPipelineEnvironment.custom.integrationFlowServiceEndpoint = finalEndpoint 81 return nil 82 } 83 } 84 return errors.Errorf("Unable to get integration flow service endpoint '%v', Response body: %v, Response Status code: %v", 85 config.IntegrationFlowID, string(bodyText), serviceEndpointResp.StatusCode) 86 } 87 responseBody, readErr := io.ReadAll(serviceEndpointResp.Body) 88 89 if readErr != nil { 90 return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code: %v", serviceEndpointResp.StatusCode) 91 } 92 93 log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code: %v", string(responseBody), serviceEndpointResp.StatusCode) 94 return errors.Errorf("Unable to get integration flow service endpoint, Response Status code: %v", serviceEndpointResp.StatusCode) 95 }