|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/AlecAivazis/survey/v2" |
| 6 | + "github.com/MakeNowJust/heredoc/v2" |
| 7 | + "github.com/OctopusDeploy/cli/pkg/cmd" |
| 8 | + "github.com/OctopusDeploy/cli/pkg/cmd/account/helper" |
| 9 | + "github.com/OctopusDeploy/cli/pkg/constants" |
| 10 | + "github.com/OctopusDeploy/cli/pkg/factory" |
| 11 | + "github.com/OctopusDeploy/cli/pkg/output" |
| 12 | + "github.com/OctopusDeploy/cli/pkg/question" |
| 13 | + "github.com/OctopusDeploy/cli/pkg/question/selectors" |
| 14 | + "github.com/OctopusDeploy/cli/pkg/surveyext" |
| 15 | + "github.com/OctopusDeploy/cli/pkg/util" |
| 16 | + "github.com/OctopusDeploy/cli/pkg/util/flag" |
| 17 | + "github.com/OctopusDeploy/cli/pkg/validation" |
| 18 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/accounts" |
| 19 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments" |
| 20 | + "github.com/spf13/cobra" |
| 21 | + "os" |
| 22 | +) |
| 23 | + |
| 24 | +type CreateFlags struct { |
| 25 | + Name *flag.Flag[string] |
| 26 | + Description *flag.Flag[string] |
| 27 | + Environments *flag.Flag[[]string] |
| 28 | + ExecutionSubjectKeys *flag.Flag[[]string] |
| 29 | + Audience *flag.Flag[string] |
| 30 | +} |
| 31 | + |
| 32 | +type CreateOptions struct { |
| 33 | + *CreateFlags |
| 34 | + *cmd.Dependencies |
| 35 | + selectors.GetAllEnvironmentsCallback |
| 36 | +} |
| 37 | + |
| 38 | +func NewCreateFlags() *CreateFlags { |
| 39 | + return &CreateFlags{ |
| 40 | + Name: flag.New[string]("name", false), |
| 41 | + Description: flag.New[string]("description", false), |
| 42 | + Environments: flag.New[[]string]("environment", false), |
| 43 | + ExecutionSubjectKeys: flag.New[[]string]("execution-subject-keys", false), |
| 44 | + Audience: flag.New[string]("audience", false), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func NewCreateOptions(flags *CreateFlags, dependencies *cmd.Dependencies) *CreateOptions { |
| 49 | + return &CreateOptions{ |
| 50 | + CreateFlags: flags, |
| 51 | + Dependencies: dependencies, |
| 52 | + GetAllEnvironmentsCallback: func() ([]*environments.Environment, error) { |
| 53 | + return selectors.GetAllEnvironments(dependencies.Client) |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func NewCmdCreate(f factory.Factory) *cobra.Command { |
| 59 | + createFlags := NewCreateFlags() |
| 60 | + descriptionFilePath := "" |
| 61 | + |
| 62 | + cmd := &cobra.Command{ |
| 63 | + Use: "create", |
| 64 | + Short: "Create an Generic OpenID Connect account", |
| 65 | + Long: "Create an Generic OpenID Connect account in Octopus Deploy", |
| 66 | + Example: heredoc.Docf("$ %s account generic-oidc create", constants.ExecutableName), |
| 67 | + Aliases: []string{"new"}, |
| 68 | + RunE: func(c *cobra.Command, _ []string) error { |
| 69 | + opts := NewCreateOptions(createFlags, cmd.NewDependencies(f, c)) |
| 70 | + if descriptionFilePath != "" { |
| 71 | + if err := validation.IsExistingFile(descriptionFilePath); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + data, err := os.ReadFile(descriptionFilePath) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + opts.Description.Value = string(data) |
| 79 | + } |
| 80 | + opts.NoPrompt = !f.IsPromptEnabled() |
| 81 | + |
| 82 | + if opts.Environments.Value != nil { |
| 83 | + env, err := helper.ResolveEnvironmentNames(opts.Environments.Value, opts.Client) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + opts.Environments.Value = env |
| 88 | + } |
| 89 | + return CreateRun(opts) |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + flags := cmd.Flags() |
| 94 | + flags.StringVarP(&createFlags.Name.Value, createFlags.Name.Name, "n", "", "A short, memorable, unique name for this account.") |
| 95 | + flags.StringVarP(&createFlags.Description.Value, createFlags.Description.Value, "d", "", "A summary explaining the use of the account to other users.") |
| 96 | + flags.StringArrayVarP(&createFlags.Environments.Value, createFlags.Environments.Name, "e", nil, "The environments that are allowed to use this account") |
| 97 | + flags.StringArrayVarP(&createFlags.ExecutionSubjectKeys.Value, createFlags.ExecutionSubjectKeys.Name, "E", nil, "The subject keys used for a deployment or runbook") |
| 98 | + flags.StringVar(&createFlags.Audience.Value, createFlags.Audience.Name, "", "The audience claim for the federated credentials. Defaults to api://default") |
| 99 | + flags.StringVarP(&descriptionFilePath, "description-file", "D", "", "Read the description from `file`") |
| 100 | + |
| 101 | + return cmd |
| 102 | +} |
| 103 | + |
| 104 | +func CreateRun(opts *CreateOptions) error { |
| 105 | + if !opts.NoPrompt { |
| 106 | + if err := PromptMissing(opts); err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + } |
| 110 | + var createdAccount accounts.IAccount |
| 111 | + oidcAccount, err := accounts.NewGenericOIDCAccount( |
| 112 | + opts.Name.Value, |
| 113 | + ) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + oidcAccount.DeploymentSubjectKeys = opts.ExecutionSubjectKeys.Value |
| 118 | + oidcAccount.Audience = opts.Audience.Value |
| 119 | + oidcAccount.Description = opts.Description.Value |
| 120 | + |
| 121 | + createdAccount, err = opts.Client.Accounts.Add(oidcAccount) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + _, err = fmt.Fprintf(opts.Out, "Successfully created Generic account %s %s.\n", createdAccount.GetName(), output.Dimf("(%s)", createdAccount.GetSlug())) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + link := output.Bluef("%s/app#/%s/infrastructure/accounts/%s", opts.Host, opts.Space.GetID(), createdAccount.GetID()) |
| 131 | + fmt.Fprintf(opts.Out, "\nView this account on Octopus Deploy: %s\n", link) |
| 132 | + if !opts.NoPrompt { |
| 133 | + autoCmd := flag.GenerateAutomationCmd( |
| 134 | + opts.CmdPath, |
| 135 | + opts.Name, |
| 136 | + opts.Description, |
| 137 | + opts.Environments, |
| 138 | + opts.ExecutionSubjectKeys, |
| 139 | + opts.Audience, |
| 140 | + ) |
| 141 | + fmt.Fprintf(opts.Out, "\nAutomation Command: %s\n", autoCmd) |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func PromptMissing(opts *CreateOptions) error { |
| 147 | + if opts.Name.Value == "" { |
| 148 | + if err := opts.Ask(&survey.Input{ |
| 149 | + Message: "Name", |
| 150 | + Help: "A short, memorable, unique name for this account.", |
| 151 | + }, &opts.Name.Value, survey.WithValidator(survey.ComposeValidators( |
| 152 | + survey.MaxLength(200), |
| 153 | + survey.MinLength(1), |
| 154 | + survey.Required, |
| 155 | + ))); err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if opts.Description.Value == "" { |
| 161 | + if err := opts.Ask(&surveyext.OctoEditor{ |
| 162 | + Editor: &survey.Editor{ |
| 163 | + Message: "Description", |
| 164 | + Help: "A summary explaining the use of the account to other users.", |
| 165 | + FileName: "*.md", |
| 166 | + }, |
| 167 | + Optional: true, |
| 168 | + }, &opts.Description.Value); err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + var err error |
| 174 | + if len(opts.ExecutionSubjectKeys.Value) == 0 { |
| 175 | + opts.ExecutionSubjectKeys.Value, err = promptSubjectKeys(opts.Ask, "Deployment and Runbook subject keys", []string{"space", "environment", "project", "tenant", "runbook", "account", "type"}) |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + if opts.Audience.Value == "" { |
| 182 | + if err := opts.Ask(&survey.Input{ |
| 183 | + Message: "Audience", |
| 184 | + Default: "api://default", |
| 185 | + Help: "Set this if you need to override the default Audience value.", |
| 186 | + }, &opts.Audience.Value); err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if opts.Environments.Value == nil { |
| 192 | + envs, err := selectors.EnvironmentsMultiSelect(opts.Ask, opts.GetAllEnvironmentsCallback, |
| 193 | + "Choose the environments that are allowed to use this account.\n"+ |
| 194 | + output.Dim("If nothing is selected, the account can be used for deployments to any environment."), false) |
| 195 | + if err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + opts.Environments.Value = util.SliceTransform(envs, func(e *environments.Environment) string { return e.ID }) |
| 199 | + } |
| 200 | + return nil |
| 201 | +} |
| 202 | + |
| 203 | +func promptSubjectKeys(ask question.Asker, message string, opts []string) ([]string, error) { |
| 204 | + keys, err := question.MultiSelectMap(ask, message, opts, func(item string) string { return item }, false) |
| 205 | + if err != nil { |
| 206 | + return nil, err |
| 207 | + } |
| 208 | + if len(keys) > 0 { |
| 209 | + return keys, nil |
| 210 | + } |
| 211 | + |
| 212 | + return nil, nil |
| 213 | +} |
0 commit comments