-
Notifications
You must be signed in to change notification settings - Fork 72
/
delete.go
168 lines (135 loc) · 4.68 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package delete
import (
"context"
"fmt"
kafkaflagutil "github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/servicecontext"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/redhat-developer/app-services-cli/pkg/shared/kafkautil"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
)
type options struct {
id string
name string
skipConfirm bool
IO *iostreams.IOStreams
Connection factory.ConnectionFunc
Logger logging.Logger
localizer localize.Localizer
Context context.Context
ServiceContext servicecontext.IContext
}
// NewDeleteCommand command for deleting kafkas.
func NewDeleteCommand(f *factory.Factory) *cobra.Command {
opts := &options{
Connection: f.Connection,
Logger: f.Logger,
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
ServiceContext: f.ServiceContext,
}
cmd := &cobra.Command{
Use: "delete",
Short: opts.localizer.MustLocalize("kafka.delete.cmd.shortDescription"),
Long: opts.localizer.MustLocalize("kafka.delete.cmd.longDescription"),
Example: opts.localizer.MustLocalize("kafka.delete.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if !opts.IO.CanPrompt() && !opts.skipConfirm {
return flagutil.RequiredWhenNonInteractiveError("yes")
}
if opts.name != "" && opts.id != "" {
return opts.localizer.MustLocalizeError("service.error.idAndNameCannotBeUsed")
}
if opts.id != "" || opts.name != "" {
return runDelete(opts)
}
kafkaInstance, err := contextutil.GetCurrentKafkaInstance(f)
if err != nil {
return err
}
opts.id = kafkaInstance.GetId()
return runDelete(opts)
},
}
flags := flagutil.NewFlagSet(cmd, opts.localizer)
flags.AddYes(&opts.skipConfirm)
flags.StringVar(&opts.id, "id", "", opts.localizer.MustLocalize("kafka.delete.flag.id"))
flags.StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("kafka.delete.flag.name"))
if err := kafkaflagutil.RegisterNameFlagCompletionFunc(cmd, f); err != nil {
opts.Logger.Debug(opts.localizer.MustLocalize("kafka.common.error.load.completions.name.flag"), err)
}
return cmd
}
func runDelete(opts *options) error {
svcContext, err := opts.ServiceContext.Load()
if err != nil {
return err
}
currCtx, err := contextutil.GetCurrentContext(svcContext, opts.localizer)
if err != nil {
return err
}
conn, err := opts.Connection()
if err != nil {
return err
}
api := conn.API()
var response *kafkamgmtclient.KafkaRequest
if opts.name != "" {
response, _, err = kafkautil.GetKafkaByName(opts.Context, api.KafkaMgmt(), opts.name)
if err != nil {
return err
}
} else {
response, _, err = kafkautil.GetKafkaByID(opts.Context, api.KafkaMgmt(), opts.id)
if err != nil {
return err
}
}
kafkaName := response.GetName()
if !opts.skipConfirm {
promptConfirmName := &survey.Input{
Message: opts.localizer.MustLocalize("kafka.delete.input.confirmName.message", localize.NewEntry("Name", kafkaName)),
}
var confirmedKafkaName string
err = survey.AskOne(promptConfirmName, &confirmedKafkaName)
if err != nil {
return err
}
if confirmedKafkaName != kafkaName {
opts.Logger.Info(opts.localizer.MustLocalize("kafka.delete.log.info.incorrectNameConfirmation"))
return nil
}
}
// delete the Kafka
opts.Logger.Debug(opts.localizer.MustLocalize("kafka.delete.log.debug.deletingKafka"), fmt.Sprintf("\"%s\"", kafkaName))
a := api.KafkaMgmt().DeleteKafkaById(opts.Context, response.GetId())
a = a.Async(true)
_, _, err = a.Execute()
if err != nil {
return err
}
opts.Logger.Info(opts.localizer.MustLocalize("kafka.delete.log.info.deleting", localize.NewEntry("Name", kafkaName)))
currentKafka := currCtx.KafkaID
// this is not the current instance, our work here is done
if currentKafka != response.GetId() {
return nil
}
// the Kafka that was deleted is set as the user's current cluster
// since it was deleted it should be removed from the context
currCtx.KafkaID = ""
svcContext.Contexts[svcContext.CurrentContext] = *currCtx
if err := opts.ServiceContext.Save(svcContext); err != nil {
return err
}
return nil
}