-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathidentity_agent.go
More file actions
131 lines (106 loc) · 3.63 KB
/
Copy pathidentity_agent.go
File metadata and controls
131 lines (106 loc) · 3.63 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
122
123
124
125
126
127
128
129
130
131
package delete
import (
"io"
"github.com/ibm-verify/verify-sdk-go/pkg/config/integrations"
"github.com/ibm-verify/verify-sdk-go/pkg/i18n"
"github.com/ibm-verify/verifyctl/pkg/config"
cmdutil "github.com/ibm-verify/verifyctl/pkg/util/cmd"
"github.com/ibm-verify/verifyctl/pkg/util/templates"
"github.com/spf13/cobra"
errorsx "github.com/ibm-verify/verify-sdk-go/pkg/core/errors"
)
const (
identityAgentUsage = `identityagent [flags]`
identityAgentMessagePrefix = "DeleteIdentityAgent"
identityAgentEntitlements = "Manage identityAgents"
identityAgentResourceName = "identityagent"
)
var (
identityAgentLongDesc = templates.LongDesc(cmdutil.TranslateLongDesc(identityAgentMessagePrefix, `
Delete Identity Agent based on identityAgentID.
Resources managed on Verify have specific entitlements, so ensure that the Identity agents used
with the 'auth' command is configured with the appropriate entitlements.
You can identify the entitlement required by running:
verifyctl delete identityagent --entitlements`))
identityAgentExamples = templates.Examples(cmdutil.TranslateExamples(messagePrefix, `
# Delete an Identity Agent by ID
verifyctl delete identityagent --identityAgentID "12345"`,
))
)
type identityAgentsOptions struct {
options
identityAgentID string
config *config.CLIConfig
}
func NewIdentityAgentCommand(config *config.CLIConfig, streams io.ReadWriter) *cobra.Command {
o := &identityAgentsOptions{
config: config,
}
cmd := &cobra.Command{
Use: identityAgentUsage,
Short: cmdutil.TranslateShortDesc(identityAgentMessagePrefix, "Delete Identity Agent based on its id."),
Long: identityAgentLongDesc,
Example: identityAgentExamples,
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 *identityAgentsOptions) AddFlags(cmd *cobra.Command) {
o.addCommonFlags(cmd)
cmd.Flags().StringVar(&o.identityAgentID, "identityAgentID", o.identityAgentID, i18n.Translate("identityAgentID to be deleted"))
}
func (o *identityAgentsOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}
func (o *identityAgentsOptions) Validate(cmd *cobra.Command, args []string) error {
if o.entitlements {
return nil
}
calledAs := cmd.CalledAs()
if calledAs == "identityagent" && o.identityAgentID == "" {
return errorsx.G11NError("'identityAgentID' flag is required")
}
return nil
}
func (o *identityAgentsOptions) Run(cmd *cobra.Command, args []string) error {
if o.entitlements {
cmdutil.WriteString(cmd, entitlementsMessage+" "+identityAgentEntitlements)
return nil
}
_, err := o.config.SetAuthToContext(cmd.Context())
if err != nil {
return err
}
// invoke the operation
if cmd.CalledAs() == "identityagent" {
// deal with single Identity Agent
return o.handleSingleIdentityAgent(cmd, args)
}
return nil
}
func (o *identityAgentsOptions) handleSingleIdentityAgent(cmd *cobra.Command, _ []string) error {
c := integrations.NewIdentityAgentClient()
var id string
var err error
if o.identityAgentID != "" {
id = o.identityAgentID
err = c.DeleteIdentityAgentByID(cmd.Context(), id)
if err != nil {
return err
}
} else {
return errorsx.G11NError("either clientName or clientId must be provided")
}
resourceIdentifier := o.identityAgentID
cmdutil.WriteString(cmd, "Resource deleted with ID: "+resourceIdentifier)
return nil
}