-
Notifications
You must be signed in to change notification settings - Fork 72
/
delete.go
143 lines (119 loc) · 3.99 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
package delete
import (
"context"
"github.com/redhat-developer/app-services-cli/pkg/icon"
"net/http"
"github.com/AlecAivazis/survey/v2"
"github.com/redhat-developer/app-services-cli/internal/config"
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory"
"github.com/redhat-developer/app-services-cli/pkg/cmd/flag"
"github.com/redhat-developer/app-services-cli/pkg/connection"
"github.com/redhat-developer/app-services-cli/pkg/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/localize"
"github.com/redhat-developer/app-services-cli/pkg/logging"
"github.com/redhat-developer/app-services-cli/pkg/serviceaccount/validation"
"github.com/spf13/cobra"
)
type options struct {
IO *iostreams.IOStreams
Config config.IConfig
Connection factory.ConnectionFunc
Logger logging.Logger
localizer localize.Localizer
Context context.Context
id string
force bool
}
// NewDeleteCommand creates a new command to delete a service account
func NewDeleteCommand(f *factory.Factory) *cobra.Command {
opts := &options{
Config: f.Config,
Connection: f.Connection,
Logger: f.Logger,
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
}
cmd := &cobra.Command{
Use: "delete",
Short: opts.localizer.MustLocalize("serviceAccount.delete.cmd.shortDescription"),
Long: opts.localizer.MustLocalize("serviceAccount.delete.cmd.longDescription"),
Example: opts.localizer.MustLocalize("serviceAccount.delete.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
if !opts.IO.CanPrompt() && !opts.force {
return flag.RequiredWhenNonInteractiveError("yes")
}
validator := &validation.Validator{
Localizer: opts.localizer,
}
validID := validator.ValidateUUID(opts.id)
if validID != nil {
return validID
}
return runDelete(opts)
},
}
cmd.Flags().StringVar(&opts.id, "id", "", opts.localizer.MustLocalize("serviceAccount.delete.flag.id.description"))
cmd.Flags().BoolVarP(&opts.force, "yes", "y", false, opts.localizer.MustLocalize("serviceAccount.delete.flag.yes.description"))
_ = cmd.MarkFlagRequired("id")
return cmd
}
func runDelete(opts *options) (err error) {
conn, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
_, httpRes, err := conn.API().ServiceAccount().GetServiceAccountById(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
if httpRes == nil {
return err
}
if httpRes.StatusCode == http.StatusNotFound {
return opts.localizer.MustLocalizeError("serviceAccount.common.error.notFoundError", localize.NewEntry("ID", opts.id))
}
}
if !opts.force {
var confirmDelete bool
promptConfirmDelete := &survey.Confirm{
Message: opts.localizer.MustLocalize("serviceAccount.delete.input.confirmDelete.message", localize.NewEntry("ID", opts.id)),
}
err = survey.AskOne(promptConfirmDelete, &confirmDelete)
if err != nil {
return err
}
if !confirmDelete {
opts.Logger.Debug(opts.localizer.MustLocalize("serviceAccount.delete.log.debug.deleteNotConfirmed"))
return nil
}
}
return deleteServiceAccount(opts)
}
func deleteServiceAccount(opts *options) error {
conn, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
_, httpRes, err := conn.API().ServiceAccount().DeleteServiceAccountById(opts.Context, opts.id).Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
if httpRes == nil {
return err
}
switch httpRes.StatusCode {
case http.StatusForbidden:
return opts.localizer.MustLocalizeError("serviceAccount.common.error.forbidden", localize.NewEntry("Operation", "delete"))
case http.StatusInternalServerError:
return opts.localizer.MustLocalizeError("serviceAccount.common.error.internalServerError")
default:
return err
}
}
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("serviceAccount.delete.log.info.deleteSuccess"))
return nil
}