-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAPI_client.go
More file actions
121 lines (97 loc) · 3.26 KB
/
Copy pathAPI_client.go
File metadata and controls
121 lines (97 loc) · 3.26 KB
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
package delete
import (
"io"
"github.com/spf13/cobra"
"github.ibm.com/sec-ci/devops-experiments/pkg/config"
"github.ibm.com/sec-ci/devops-experiments/pkg/i18n"
"github.ibm.com/sec-ci/devops-experiments/pkg/module"
"github.ibm.com/sec-ci/devops-experiments/pkg/module/directory"
cmdutil "github.ibm.com/sec-ci/devops-experiments/pkg/util/cmd"
"github.ibm.com/sec-ci/devops-experiments/pkg/util/templates"
)
const (
apiclientUsage = `apiclient [flags]`
apiclientMessagePrefix = "DeleteApiclient"
apiclientEntitlements = "Manage apiclients"
apiclientResourceName = "apiclient"
)
var (
apiclientLongDesc = templates.LongDesc(cmdutil.TranslateLongDesc(apiclientMessagePrefix, `
Delete Verify API client based on clientName.
Resources managed on Verify have specific entitlements, so ensure that the application or API client used
with the 'auth' command is configured with the appropriate entitlements.
You can identify the entitlement required by running:
verifyctl delete apiclient --entitlements`))
apiclientExamples = templates.Examples(cmdutil.TranslateExamples(messagePrefix, `
# Delete an API client
verifyctl delete apiclient --clientName="clientName"`,
))
)
type apiclientsOptions struct {
options
config *config.CLIConfig
}
func NewAPIclientCommand(config *config.CLIConfig, streams io.ReadWriter) *cobra.Command {
o := &apiclientsOptions{
config: config,
}
cmd := &cobra.Command{
Use: apiclientUsage,
Short: cmdutil.TranslateShortDesc(apiclientMessagePrefix, "Delete Verify API client based on an id."),
Long: apiclientLongDesc,
Example: apiclientExamples,
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.ExitOnError(cmd, o.Complete(cmd, args))
cmdutil.ExitOnError(cmd, o.Validate(cmd, args))
cmdutil.ExitOnError(cmd, o.Run(cmd, args))
},
}
cmd.SetOut(streams)
cmd.SetErr(streams)
cmd.SetIn(streams)
o.AddFlags(cmd)
return cmd
}
func (o *apiclientsOptions) AddFlags(cmd *cobra.Command) {
o.addCommonFlags(cmd)
cmd.Flags().StringVar(&o.name, "clientName", o.name, i18n.Translate("clientName to be deleted"))
}
func (o *apiclientsOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}
func (o *apiclientsOptions) Validate(cmd *cobra.Command, args []string) error {
if o.entitlements {
return nil
}
calledAs := cmd.CalledAs()
if calledAs == "apiclient" && o.name == "" {
return module.MakeSimpleError(i18n.Translate("'clientName' flag is required"))
}
return nil
}
func (o *apiclientsOptions) Run(cmd *cobra.Command, args []string) error {
if o.entitlements {
cmdutil.WriteString(cmd, entitlementsMessage+" "+apiclientEntitlements)
return nil
}
auth, err := o.config.GetCurrentAuth()
if err != nil {
return err
}
// invoke the operation
if cmd.CalledAs() == "apiclient" || len(o.name) > 0 {
// deal with single API client
return o.handleSingleApiClient(cmd, auth, args)
}
return nil
}
func (o *apiclientsOptions) handleSingleApiClient(cmd *cobra.Command, auth *config.AuthConfig, _ []string) error {
c := directory.NewApiClient()
err := c.DeleteApiclient(cmd.Context(), auth, o.name)
if err != nil {
return err
}
cmdutil.WriteString(cmd, "Resource deleted: "+o.name)
return nil
}