Files
sap-jenkins-library/cmd/btpDeleteServiceBinding_generated.go
T

313 lines
12 KiB
Go

// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/eventing"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/splunk"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/validation"
"github.com/spf13/cobra"
)
type btpDeleteServiceBindingOptions struct {
BtpAPIEndpoint string `json:"btpApiEndpoint,omitempty"`
BtpSubdomain string `json:"btpSubdomain,omitempty"`
BtpIDp string `json:"btpIdp,omitempty"`
BtpSubaccount string `json:"btpSubaccount,omitempty"`
BtpServiceInstanceName string `json:"btpServiceInstanceName,omitempty"`
BtpServiceBindingName string `json:"btpServiceBindingName,omitempty"`
Timeout int `json:"timeout,omitempty"`
PollInterval int `json:"pollInterval,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
}
// BtpDeleteServiceBindingCommand Delete a service binding in BTP
func BtpDeleteServiceBindingCommand() *cobra.Command {
const STEP_NAME = "btpDeleteServiceBinding"
metadata := btpDeleteServiceBindingMetadata()
var stepConfig btpDeleteServiceBindingOptions
var startTime time.Time
var logCollector *log.CollectorHook
var splunkClient *splunk.Splunk
telemetryClient := &telemetry.Telemetry{}
var createBtpDeleteServiceBindingCmd = &cobra.Command{
Use: STEP_NAME,
Short: "Delete a service binding in BTP",
Long: `Delete a service binding in SAP Business Technology Platform (BTP) using ` + "`" + `btp delete services/binding` + "`" + ` ([documentation](https://help.sap.com/docs/BTP/btp-cli_command-documentation_btp-cli/btp-delete-services-binding.html?locale=en-US&version=Cloud)).`,
PreRunE: func(cmd *cobra.Command, _ []string) error {
startTime = time.Now()
log.SetStepName(STEP_NAME)
log.SetVerbose(GeneralConfig.Verbose)
GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
path, err := os.Getwd()
if err != nil {
return err
}
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
log.RegisterHook(fatalHook)
err = PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
if err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return err
}
// Set step error patterns for improved error detection
stepErrors := make([]log.StepError, len(metadata.Metadata.Errors))
for i, err := range metadata.Metadata.Errors {
stepErrors[i] = log.StepError{
Pattern: err.Pattern,
Message: err.Message,
Category: err.Category,
}
}
log.SetStepErrors(stepErrors)
log.RegisterSecret(stepConfig.User)
log.RegisterSecret(stepConfig.Password)
if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
log.RegisterHook(&sentryHook)
}
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 || len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
splunkClient = &splunk.Splunk{}
logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
log.RegisterHook(logCollector)
}
if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
}
validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
if err != nil {
return err
}
if err = validation.ValidateStruct(stepConfig); err != nil {
log.SetErrorCategory(log.ErrorConfiguration)
return err
}
return nil
},
Run: func(_ *cobra.Command, _ []string) {
vaultClient := config.GlobalVaultClient()
var oidcTokenProvider func(string) (string, error)
if vaultClient != nil {
defer vaultClient.MustRevokeToken()
oidcTokenProvider = vaultClient.GetOIDCTokenByValidation
}
stepTelemetryData := telemetry.CustomData{}
stepTelemetryData.ErrorCode = "1"
handler := func() {
config.RemoveVaultSecretFiles()
stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
stepTelemetryData.PiperCommitHash = GitCommit
telemetryClient.SetData(&stepTelemetryData)
telemetryClient.LogStepTelemetryData()
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
splunkClient.Initialize(GeneralConfig.CorrelationID,
GeneralConfig.HookConfig.SplunkConfig.Dsn,
GeneralConfig.HookConfig.SplunkConfig.Token,
GeneralConfig.HookConfig.SplunkConfig.Index,
GeneralConfig.HookConfig.SplunkConfig.SendLogs)
splunkClient.Send(telemetryClient.GetData(), logCollector)
}
if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
splunkClient.Initialize(GeneralConfig.CorrelationID,
GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
GeneralConfig.HookConfig.SplunkConfig.SendLogs)
splunkClient.Send(telemetryClient.GetData(), logCollector)
}
if len(GeneralConfig.HookConfig.GCPPubSubConfig.ProjectNumber) > 0 {
if err := eventing.PublishTaskRunFinishedEvent(
oidcTokenProvider,
&GeneralConfig,
eventing.EventContext{
StepName: STEP_NAME,
StageName: telemetryClient.GetData().StageName,
ErrorCode: stepTelemetryData.ErrorCode,
PipelineID: telemetryClient.GetBuildURL(),
},
); err != nil {
log.Entry().WithError(err).Warn("failed to publish GCP Pub/Sub event")
}
}
}
log.DeferExitHandler(handler)
defer handler()
telemetryClient.Initialize(STEP_NAME)
btpDeleteServiceBinding(stepConfig, &stepTelemetryData)
stepTelemetryData.ErrorCode = "0"
log.Entry().Info("SUCCESS")
},
}
addBtpDeleteServiceBindingFlags(createBtpDeleteServiceBindingCmd, &stepConfig)
return createBtpDeleteServiceBindingCmd
}
func addBtpDeleteServiceBindingFlags(cmd *cobra.Command, stepConfig *btpDeleteServiceBindingOptions) {
cmd.Flags().StringVar(&stepConfig.BtpAPIEndpoint, "btpApiEndpoint", `https://cli.btp.cloud.sap`, "BTP API endpoint")
cmd.Flags().StringVar(&stepConfig.BtpSubdomain, "btpSubdomain", os.Getenv("PIPER_btpSubdomain"), "BTP subdomain (Global Account). It will be used during login.")
cmd.Flags().StringVar(&stepConfig.BtpIDp, "btpIdp", os.Getenv("PIPER_btpIdp"), "BTP idp (Identity Provider) (optional). It will be used during login.")
cmd.Flags().StringVar(&stepConfig.BtpSubaccount, "btpSubaccount", os.Getenv("PIPER_btpSubaccount"), "BTP subaccount where the service instance will be deleted")
cmd.Flags().StringVar(&stepConfig.BtpServiceInstanceName, "btpServiceInstanceName", os.Getenv("PIPER_btpServiceInstanceName"), "Name of the service instance for which the service binding will be deleted")
cmd.Flags().StringVar(&stepConfig.BtpServiceBindingName, "btpServiceBindingName", os.Getenv("PIPER_btpServiceBindingName"), "Name of the service binding to create")
cmd.Flags().IntVar(&stepConfig.Timeout, "timeout", 600, "Timeout in seconds for deletion operation")
cmd.Flags().IntVar(&stepConfig.PollInterval, "pollInterval", 5, "Poll interval in seconds for checking instance readiness")
cmd.Flags().StringVar(&stepConfig.User, "user", os.Getenv("PIPER_user"), "User or E-Mail for BTP")
cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for BTP")
cmd.MarkFlagRequired("btpApiEndpoint")
cmd.MarkFlagRequired("btpSubdomain")
cmd.MarkFlagRequired("btpSubaccount")
cmd.MarkFlagRequired("btpServiceInstanceName")
cmd.MarkFlagRequired("btpServiceBindingName")
cmd.MarkFlagRequired("user")
cmd.MarkFlagRequired("password")
}
// retrieve step metadata
func btpDeleteServiceBindingMetadata() config.StepData {
var theMetaData = config.StepData{
Metadata: config.StepMetadata{
Name: "btpDeleteServiceBinding",
Aliases: []config.Alias{},
Description: "Delete a service binding in BTP",
},
Spec: config.StepSpec{
Inputs: config.StepInputs{
Secrets: []config.StepSecrets{
{Name: "abapCredentialsId", Description: "Jenkins 'Username with password' credentials ID containing user and password to authenticate to BTP.", Type: "jenkins", Aliases: []config.Alias{{Name: "btp/credentialsId", Deprecated: false}}},
},
Parameters: []config.StepParameters{
{
Name: "btpApiEndpoint",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "btp/url"}},
Default: `https://cli.btp.cloud.sap`,
},
{
Name: "btpSubdomain",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "btp/subdomain"}},
Default: os.Getenv("PIPER_btpSubdomain"),
},
{
Name: "btpIdp",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{{Name: "btp/idp"}},
Default: os.Getenv("PIPER_btpIdp"),
},
{
Name: "btpSubaccount",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "btp/subaccount"}},
Default: os.Getenv("PIPER_btpSubaccount"),
},
{
Name: "btpServiceInstanceName",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "btp/instanceName"}},
Default: os.Getenv("PIPER_btpServiceInstanceName"),
},
{
Name: "btpServiceBindingName",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{{Name: "btp/bindingName"}},
Default: os.Getenv("PIPER_btpServiceBindingName"),
},
{
Name: "timeout",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
Type: "int",
Mandatory: false,
Aliases: []config.Alias{{Name: "btp/timeout"}},
Default: 600,
},
{
Name: "pollInterval",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
Type: "int",
Mandatory: false,
Aliases: []config.Alias{{Name: "btp/pollInterval"}},
Default: 5,
},
{
Name: "user",
ResourceRef: []config.ResourceReference{
{
Name: "abapCredentialsId",
Param: "username",
Type: "secret",
},
},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_user"),
},
{
Name: "password",
ResourceRef: []config.ResourceReference{
{
Name: "abapCredentialsId",
Param: "password",
Type: "secret",
},
},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: true,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_password"),
},
},
},
Containers: []config.Container{
{Name: "btp", Image: "ppiper/cf-cli:latest", WorkingDir: "/home/piper"},
},
},
}
return theMetaData
}