github.com/xgoffin/jenkins-library@v1.154.0/cmd/cloudFoundryDeleteService.go (about) 1 package cmd 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 8 "github.com/SAP/jenkins-library/pkg/cloudfoundry" 9 "github.com/SAP/jenkins-library/pkg/command" 10 "github.com/SAP/jenkins-library/pkg/log" 11 "github.com/SAP/jenkins-library/pkg/telemetry" 12 ) 13 14 func cloudFoundryDeleteService(options cloudFoundryDeleteServiceOptions, telemetryData *telemetry.CustomData) { 15 16 c := command.Command{} 17 18 // reroute command output to logging framework 19 c.Stdout(log.Writer()) 20 c.Stderr(log.Writer()) 21 22 cfUtils := cloudfoundry.CFUtils{ 23 Exec: &c, 24 } 25 26 err := runCloudFoundryDeleteService(options, &c, &cfUtils) 27 if err != nil { 28 log.Entry(). 29 WithError(err). 30 Fatal("Error occurred during step.") 31 } 32 } 33 34 func runCloudFoundryDeleteService(options cloudFoundryDeleteServiceOptions, c command.ExecRunner, cfUtils cloudfoundry.AuthenticationUtils) (returnedError error) { 35 36 config := cloudfoundry.LoginOptions{ 37 CfAPIEndpoint: options.CfAPIEndpoint, 38 CfOrg: options.CfOrg, 39 CfSpace: options.CfSpace, 40 Username: options.Username, 41 Password: options.Password, 42 } 43 loginErr := cfUtils.Login(config) 44 if loginErr != nil { 45 return fmt.Errorf("Error while logging in occurred: %w", loginErr) 46 } 47 defer func() { 48 logoutErr := cfUtils.Logout() 49 if logoutErr != nil && returnedError == nil { 50 returnedError = fmt.Errorf("Error while logging out occurred: %w", logoutErr) 51 } 52 }() 53 54 if options.CfDeleteServiceKeys == true { 55 err := cloudFoundryDeleteServiceKeys(options, c) 56 if err != nil { 57 return err 58 } 59 } 60 61 err := cloudFoundryDeleteServiceFunction(options.CfServiceInstance, c) 62 if err != nil { 63 return err 64 } 65 66 return returnedError 67 } 68 69 func cloudFoundryDeleteServiceKeys(options cloudFoundryDeleteServiceOptions, c command.ExecRunner) error { 70 71 log.Entry().Info("Deleting inherent Service Keys") 72 73 var cfFindServiceKeysScript = []string{"service-keys", options.CfServiceInstance} 74 75 var serviceKeyBytes bytes.Buffer 76 c.Stdout(&serviceKeyBytes) 77 78 err := c.RunExecutable("cf", cfFindServiceKeysScript...) 79 80 if err != nil { 81 return fmt.Errorf("Failed to Delete Service Key, most likely your service doesn't exist: %w", err) 82 } 83 84 if len(serviceKeyBytes.String()) == 0 { 85 log.Entry().Info("No service key could be retrieved for your requested Service") 86 return nil 87 } 88 89 var lines []string = strings.Split(serviceKeyBytes.String(), "\n") 90 if len(lines) <= 4 { 91 log.Entry().Info("No Service Keys active to be deleted") 92 return nil 93 } 94 var numberOfLines = len(lines) 95 log.Entry().WithField("Number of service keys :", numberOfLines-4).Info("ServiceKey") 96 //Deleting all matched Service Keys for Service 97 for i := 3; i <= numberOfLines-2; i++ { 98 log.Entry().WithField("Deleting Service Key", lines[i]).Info("ServiceKeyDeletion") 99 var cfDeleteServiceKeyScript = []string{"delete-service-key", options.CfServiceInstance, lines[i], "-f"} 100 err := c.RunExecutable("cf", cfDeleteServiceKeyScript...) 101 if err != nil { 102 return fmt.Errorf("Failed to Delete Service Key: %w", err) 103 } 104 } 105 log.Entry().Info("ServiceKeys have been deleted!") 106 return nil 107 } 108 109 func cloudFoundryDeleteServiceFunction(service string, c command.ExecRunner) error { 110 var cfdeleteServiceScript = []string{"delete-service", service, "-f"} 111 112 log.Entry().WithField("cfService", service).Info("Deleting the requested Service") 113 114 err := c.RunExecutable("cf", cfdeleteServiceScript...) 115 116 if err != nil { 117 return fmt.Errorf("Failed to delete Service: %w", err) 118 } 119 log.Entry().Info("Deletion of Service is finished or the Service has never existed") 120 return nil 121 }