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