1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/cloudFoundryDeleteService.go
2020-02-10 14:53:12 +01:00

75 lines
2.4 KiB
Go

package cmd
import (
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
)
func cloudFoundryDeleteService(CloudFoundryDeleteServiceOptions cloudFoundryDeleteServiceOptions, telemetryData *telemetry.CustomData) error {
c := command.Command{}
// reroute command output to logging framework
c.Stdout(log.Entry().Writer())
c.Stderr(log.Entry().Writer())
cloudFoundryLogin(CloudFoundryDeleteServiceOptions, &c)
err := cloudFoundryDeleteServiceFunction(CloudFoundryDeleteServiceOptions.CfServiceInstance, &c)
if err != nil {
cloudFoundryLogout(&c)
log.Entry().
WithError(err).
Fatal("Failed to delete Service!")
return err
}
cloudFoundryLogout(&c)
return nil
}
func cloudFoundryLogin(CloudFoundryDeleteServiceOptions cloudFoundryDeleteServiceOptions, c execRunner) error {
var cfLoginScript = []string{"login", "-a", CloudFoundryDeleteServiceOptions.CfAPIEndpoint, "-o", CloudFoundryDeleteServiceOptions.CfOrg, "-s", CloudFoundryDeleteServiceOptions.CfSpace, "-u", CloudFoundryDeleteServiceOptions.Username, "-p", CloudFoundryDeleteServiceOptions.Password}
log.Entry().WithField("cfAPI:", CloudFoundryDeleteServiceOptions.CfAPIEndpoint).WithField("cfOrg", CloudFoundryDeleteServiceOptions.CfOrg).WithField("space", CloudFoundryDeleteServiceOptions.CfSpace).WithField("password", CloudFoundryDeleteServiceOptions.Password).Info("Logging into Cloud Foundry..")
err := c.RunExecutable("cf", cfLoginScript...)
if err != nil {
log.Entry().
WithError(err).
Fatal("Failed to login to Cloud Foundry")
}
log.Entry().Info("Logged in successfully to Cloud Foundry..")
return err
}
func cloudFoundryDeleteServiceFunction(service string, c execRunner) error {
var cfdeleteServiceScript = []string{"delete-service", service, "-f"}
log.Entry().WithField("cfService", service).Info("Deleting the requested Service")
err := c.RunExecutable("cf", cfdeleteServiceScript...)
if err != nil {
return err
}
log.Entry().Info("Deletion of Service is finished or the Service has never existed")
return err
}
func cloudFoundryLogout(c execRunner) error {
var cfLogoutScript = "logout"
log.Entry().Info("Logging out of Cloud Foundry")
err := c.RunExecutable("cf", cfLogoutScript)
if err != nil {
log.Entry().
WithError(err).
Fatal("Failed to Logout of Cloud Foudnry")
}
log.Entry().Info("Logged out successfully")
return err
}