2020-02-10 15:53:12 +02:00
package cmd
import (
2020-03-05 16:35:43 +02:00
"bytes"
"fmt"
"strings"
2020-02-10 15:53:12 +02:00
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
)
2020-03-05 16:35:43 +02:00
func cloudFoundryDeleteService ( options cloudFoundryDeleteServiceOptions , telemetryData * telemetry . CustomData ) {
2020-02-10 15:53:12 +02:00
c := command . Command { }
// reroute command output to logging framework
2020-05-06 13:35:40 +02:00
c . Stdout ( log . Writer ( ) )
c . Stderr ( log . Writer ( ) )
2020-02-10 15:53:12 +02:00
2020-03-05 16:35:43 +02:00
var err error
2020-02-10 15:53:12 +02:00
2020-03-05 16:35:43 +02:00
err = cloudFoundryLogin ( options , & c )
if err == nil && options . CfDeleteServiceKeys == true {
err = cloudFoundryDeleteServiceKeys ( options , & c )
}
if err == nil {
err = cloudFoundryDeleteServiceFunction ( options . CfServiceInstance , & c )
}
var logoutErr error
if err == nil {
logoutErr = cloudFoundryLogout ( & c )
if logoutErr != nil {
log . Entry ( ) .
WithError ( logoutErr ) .
Fatal ( "Error while logging out occured." )
}
} else if err != nil {
logoutErr = cloudFoundryLogout ( & c )
if logoutErr != nil {
log . Entry ( ) .
WithError ( logoutErr ) .
Fatal ( "Error while logging out occured." )
}
2020-02-10 15:53:12 +02:00
log . Entry ( ) .
WithError ( err ) .
2020-03-05 16:35:43 +02:00
Fatal ( "Error occured." )
}
}
2020-07-01 11:28:16 +02:00
func cloudFoundryDeleteServiceKeys ( options cloudFoundryDeleteServiceOptions , c command . ExecRunner ) error {
2020-03-05 16:35:43 +02:00
log . Entry ( ) . Info ( "Deleting inherent Service Keys" )
var cfFindServiceKeysScript = [ ] string { "service-keys" , options . CfServiceInstance }
var serviceKeyBytes bytes . Buffer
c . Stdout ( & serviceKeyBytes )
err := c . RunExecutable ( "cf" , cfFindServiceKeysScript ... )
if err != nil {
return fmt . Errorf ( "Failed to Delete Service Key, most likely your service doesn't exist: %w" , err )
2020-02-10 15:53:12 +02:00
}
2020-03-05 16:35:43 +02:00
if len ( serviceKeyBytes . String ( ) ) == 0 {
log . Entry ( ) . Info ( "No service key could be retrieved for your requested Service" )
return nil
}
2020-02-10 15:53:12 +02:00
2020-03-05 16:35:43 +02:00
var lines [ ] string = strings . Split ( serviceKeyBytes . String ( ) , "\n" )
if len ( lines ) <= 4 {
log . Entry ( ) . Info ( "No Service Keys active to be deleted" )
return nil
}
var numberOfLines = len ( lines )
log . Entry ( ) . WithField ( "Number of service keys :" , numberOfLines - 4 ) . Info ( "ServiceKey" )
//Deleting all matched Service Keys for Service
for i := 3 ; i <= numberOfLines - 2 ; i ++ {
log . Entry ( ) . WithField ( "Deleting Service Key" , lines [ i ] ) . Info ( "ServiceKeyDeletion" )
var cfDeleteServiceKeyScript = [ ] string { "delete-service-key" , options . CfServiceInstance , lines [ i ] , "-f" }
err := c . RunExecutable ( "cf" , cfDeleteServiceKeyScript ... )
if err != nil {
return fmt . Errorf ( "Failed to Delete Service Key: %w" , err )
}
}
log . Entry ( ) . Info ( "ServiceKeys have been deleted!" )
2020-02-10 15:53:12 +02:00
return nil
}
2020-07-01 11:28:16 +02:00
func cloudFoundryLogin ( options cloudFoundryDeleteServiceOptions , c command . ExecRunner ) error {
2020-03-05 16:35:43 +02:00
var cfLoginScript = [ ] string { "login" , "-a" , options . CfAPIEndpoint , "-o" , options . CfOrg , "-s" , options . CfSpace , "-u" , options . Username , "-p" , options . Password }
2020-02-10 15:53:12 +02:00
2020-03-05 16:35:43 +02:00
log . Entry ( ) . WithField ( "cfAPI:" , options . CfAPIEndpoint ) . WithField ( "cfOrg" , options . CfOrg ) . WithField ( "space" , options . CfSpace ) . Info ( "Logging into Cloud Foundry.." )
2020-02-10 15:53:12 +02:00
err := c . RunExecutable ( "cf" , cfLoginScript ... )
2020-03-05 16:35:43 +02:00
2020-02-10 15:53:12 +02:00
if err != nil {
2020-03-05 16:35:43 +02:00
return fmt . Errorf ( "Failed to login to Cloud Foundry: %w" , err )
2020-02-10 15:53:12 +02:00
}
log . Entry ( ) . Info ( "Logged in successfully to Cloud Foundry.." )
2020-03-05 16:35:43 +02:00
return nil
2020-02-10 15:53:12 +02:00
}
2020-07-01 11:28:16 +02:00
func cloudFoundryDeleteServiceFunction ( service string , c command . ExecRunner ) error {
2020-02-10 15:53:12 +02:00
var cfdeleteServiceScript = [ ] string { "delete-service" , service , "-f" }
log . Entry ( ) . WithField ( "cfService" , service ) . Info ( "Deleting the requested Service" )
err := c . RunExecutable ( "cf" , cfdeleteServiceScript ... )
2020-03-05 16:35:43 +02:00
2020-02-10 15:53:12 +02:00
if err != nil {
2020-03-05 16:35:43 +02:00
return fmt . Errorf ( "Failed to delete Service: %w" , err )
2020-02-10 15:53:12 +02:00
}
log . Entry ( ) . Info ( "Deletion of Service is finished or the Service has never existed" )
2020-03-05 16:35:43 +02:00
return nil
2020-02-10 15:53:12 +02:00
}
2020-07-01 11:28:16 +02:00
func cloudFoundryLogout ( c command . ExecRunner ) error {
2020-02-10 15:53:12 +02:00
var cfLogoutScript = "logout"
log . Entry ( ) . Info ( "Logging out of Cloud Foundry" )
err := c . RunExecutable ( "cf" , cfLogoutScript )
if err != nil {
2020-03-05 16:35:43 +02:00
return fmt . Errorf ( "Failed to Logout of Cloud Foundry: %w" , err )
2020-02-10 15:53:12 +02:00
}
log . Entry ( ) . Info ( "Logged out successfully" )
2020-03-05 16:35:43 +02:00
return nil
2020-02-10 15:53:12 +02:00
}