-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrbac_create_subject.go
More file actions
72 lines (60 loc) · 1.88 KB
/
rbac_create_subject.go
File metadata and controls
72 lines (60 loc) · 1.88 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
//nolint:dupl
package rbac
import (
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
)
type CreateSubjectArgs struct {
Subject string
Scope string
CreateOnly bool
}
func CreateSubjectCommand(deps *di.Deps) *cobra.Command {
args := &CreateSubjectArgs{}
cmd := &cobra.Command{
Use: "create-subject",
Short: "Create a new security subject",
Long: "Create a new security subject for RBAC. Subjects can be users or groups that can be granted permissions.",
Example: `# create a new subject
sts rbac create-subject --subject my-team
# create a subject with a scope query
sts rbac create-subject --subject my-team --scope "label = 'production'"`,
RunE: deps.CmdRunEWithApi(RunCreateSubjectCommand(args)),
}
cmd.Flags().StringVar(&args.Subject, Subject, "", SubjectUsage)
cmd.MarkFlagRequired(Subject) //nolint:errcheck
cmd.Flags().StringVar(&args.Scope, Scope, "", ScopeUsage)
cmd.Flags().BoolVar(&args.CreateOnly, CreateOnly, false, CreateOnlyUsage)
return cmd
}
func RunCreateSubjectCommand(args *CreateSubjectArgs) di.CmdWithApiFn {
return func(
cmd *cobra.Command,
cli *di.Deps,
api *stackstate_api.APIClient,
serverInfo *stackstate_api.ServerInfo,
) common.CLIError {
subject := stackstate_api.NewCreateSubject()
if args.Scope != "" {
subject.SetQuery(args.Scope)
subject.SetVersion("0.0.1")
}
resp, err := api.SubjectApi.CreateSubject(cli.Context, args.Subject).
CreateSubject(*subject).
CreateOnly(args.CreateOnly).
Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"created-subject": args.Subject,
})
} else {
cli.Printer.Successf("Created subject '%s'", args.Subject)
}
return nil
}
}