-
Notifications
You must be signed in to change notification settings - Fork 72
/
create.go
220 lines (181 loc) · 7.81 KB
/
create.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package create
import (
"context"
"fmt"
"os"
"github.com/redhat-developer/app-services-cli/pkg/cmd/serviceaccount/svcaccountcmdutil"
"github.com/redhat-developer/app-services-cli/pkg/cmd/serviceaccount/svcaccountcmdutil/credentials"
"github.com/redhat-developer/app-services-cli/pkg/cmd/serviceaccount/svcaccountcmdutil/validation"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/config"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/color"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/spinner"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/core/logging"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
svcacctmgmtclient "github.com/redhat-developer/app-services-sdk-core/app-services-sdk-go/serviceaccountmgmt/apiv1/client"
svcacctmgmterrors "github.com/redhat-developer/app-services-sdk-core/app-services-sdk-go/serviceaccountmgmt/apiv1/error"
"github.com/AlecAivazis/survey/v2"
"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
fileFormat string
overwrite bool
shortDescription string
filename string
interactive bool
}
// NewCreateCommand creates a new command to create service accounts
func NewCreateCommand(f *factory.Factory) *cobra.Command {
opts := &options{
IO: f.IOStreams,
Config: f.Config,
Connection: f.Connection,
Logger: f.Logger,
localizer: f.Localizer,
Context: f.Context,
}
cmd := &cobra.Command{
Use: "create",
Short: opts.localizer.MustLocalize("serviceAccount.create.cmd.shortDescription"),
Long: opts.localizer.MustLocalize("serviceAccount.create.cmd.longDescription"),
Example: opts.localizer.MustLocalize("serviceAccount.create.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) (err error) {
if !opts.IO.CanPrompt() && opts.shortDescription == "" {
return opts.localizer.MustLocalizeError("flag.error.requiredWhenNonInteractive", localize.NewEntry("Flag", "short-description"))
} else if opts.shortDescription == "" {
opts.interactive = true
}
if !opts.interactive {
validator := &validation.Validator{
Localizer: opts.localizer,
}
if opts.fileFormat == "" {
return opts.localizer.MustLocalizeError("flag.error.requiredWhenNonInteractive", localize.NewEntry("Flag", "file-format"))
}
if err = validator.ValidateShortDescription(opts.shortDescription); err != nil {
return err
}
}
// check that a valid --file-format flag value is used
validOutput := flagutil.IsValidInput(opts.fileFormat, svcaccountcmdutil.CredentialsOutputFormats...)
if !validOutput && opts.fileFormat != "" {
return flagutil.InvalidValueError("file-format", opts.fileFormat, svcaccountcmdutil.CredentialsOutputFormats...)
}
return runCreate(opts)
},
}
cmd.Flags().StringVar(&opts.shortDescription, "short-description", "", opts.localizer.MustLocalize("serviceAccount.create.flag.shortDescription.description"))
cmd.Flags().BoolVar(&opts.overwrite, "overwrite", false, opts.localizer.MustLocalize("serviceAccount.common.flag.overwrite.description"))
cmd.Flags().StringVar(&opts.filename, "output-file", "", opts.localizer.MustLocalize("serviceAccount.common.flag.fileLocation.description"))
cmd.Flags().StringVar(&opts.fileFormat, "file-format", "", opts.localizer.MustLocalize("serviceAccount.common.flag.fileFormat.description"))
flagutil.EnableStaticFlagCompletion(cmd, "file-format", svcaccountcmdutil.CredentialsOutputFormats)
return cmd
}
// nolint:funlen
func runCreate(opts *options) error {
conn, err := opts.Connection()
if err != nil {
return err
}
if opts.interactive {
// run the create command interactively
err = runInteractivePrompt(opts)
if err != nil {
return err
}
} else if opts.filename == "" {
// obtain the absolute path to where credentials will be saved
opts.filename = credentials.GetDefaultPath(opts.fileFormat)
}
// If the credentials file already exists, and the --overwrite flag is not set then return an error
// indicating that the user should explicitly request overwriting of the file
_, err = os.Stat(opts.filename)
if err == nil && !opts.overwrite {
return opts.localizer.MustLocalizeError("serviceAccount.common.error.credentialsFileAlreadyExists", localize.NewEntry("FilePath", opts.filename))
}
spinner := spinner.New(opts.IO.ErrOut, opts.localizer)
spinner.SetSuffix(opts.localizer.MustLocalize("serviceAccount.create.log.info.creating"))
spinner.Start()
// create the service account
serviceAccountPayload := svcacctmgmtclient.ServiceAccountCreateRequestData{Name: opts.shortDescription}
serviceacct, httpRes, err := conn.API().
ServiceAccountMgmt().
CreateServiceAccount(opts.Context).
ServiceAccountCreateRequestData(serviceAccountPayload).
Execute()
spinner.Stop()
if httpRes != nil {
defer httpRes.Body.Close()
}
if apiErr := svcacctmgmterrors.GetAPIError(err); apiErr != nil {
switch apiErr.GetError() {
case "service_account_limit_exceeded":
return opts.localizer.MustLocalizeError("serviceAccount.common.error.limitExceeded")
default:
return err
}
}
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("serviceAccount.create.log.info.createdSuccessfully", localize.NewEntry("ID", serviceacct.GetId())))
creds := &credentials.Credentials{
ClientID: serviceacct.GetClientId(),
ClientSecret: serviceacct.GetSecret(),
TokenURL: conn.API().GetConfig().AuthURL.String() + "/protocol/openid-connect/token",
}
// save the credentials to a file
err = credentials.Write(opts.fileFormat, opts.filename, creds)
if err != nil {
return fmt.Errorf("%v: %w", opts.localizer.MustLocalize("serviceAccount.common.error.couldNotSaveCredentialsFile"), err)
}
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("serviceAccount.common.log.info.credentialsSaved",
localize.NewEntry("FilePath", color.CodeSnippet(opts.filename)),
localize.NewEntry("ClientID", color.Success(creds.ClientID)),
))
return nil
}
func runInteractivePrompt(opts *options) (err error) {
_, err = opts.Connection()
if err != nil {
return err
}
validator := &validation.Validator{
Localizer: opts.localizer,
}
opts.Logger.Debug(opts.localizer.MustLocalize("common.log.debug.startingInteractivePrompt"))
promptName := &survey.Input{
Message: opts.localizer.MustLocalize("serviceAccount.create.input.shortDescription.message"),
Help: opts.localizer.MustLocalize("serviceAccount.create.input.shortDescription.help"),
}
err = survey.AskOne(promptName, &opts.shortDescription, survey.WithValidator(survey.Required), survey.WithValidator(validator.ValidateShortDescription))
if err != nil {
return err
}
// if the --file-format flag was not used, ask in the prompt
if opts.fileFormat == "" {
opts.Logger.Debug(opts.localizer.MustLocalize("serviceAccount.common.log.debug.interactive.fileFormatNotSet"))
fileFormatPrompt := &survey.Select{
Message: opts.localizer.MustLocalize("serviceAccount.create.input.fileFormat.message"),
Help: opts.localizer.MustLocalize("serviceAccount.create.input.fileFormat.help"),
Options: svcaccountcmdutil.CredentialsOutputFormats,
Default: credentials.EnvFormat,
}
err = survey.AskOne(fileFormatPrompt, &opts.fileFormat)
if err != nil {
return err
}
}
opts.filename, opts.overwrite, err = credentials.ChooseFileLocation(opts.fileFormat, opts.filename, opts.overwrite)
if err != nil {
return err
}
return nil
}