Skip to content

Commit 0bce871

Browse files
authored
fix(init): project ID prompt (#3201)
1 parent d67d00d commit 0bce871

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

internal/namespaces/init/init.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Default path for configuration file is based on the following priority order:
169169
}
170170

171171
if args.ProjectID == "" {
172-
args.ProjectID = getAPIKeyDefaultProjectID(ctx, args.AccessKey, args.SecretKey)
172+
args.ProjectID = getAPIKeyDefaultProjectID(ctx, args.AccessKey, args.SecretKey, args.OrganizationID)
173173
args.ProjectID, err = promptProjectID(ctx, args.AccessKey, args.SecretKey, args.OrganizationID, args.ProjectID)
174174
if err != nil {
175175
return nil, err
@@ -295,20 +295,20 @@ func loadConfigOrEmpty(configPath string) (*scw.Config, error) {
295295
}
296296

297297
// getAPIKeyDefaultProjectID tries to find the api-key default project ID
298-
// return an empty string if it cannot find it
299-
func getAPIKeyDefaultProjectID(ctx context.Context, accessKey string, secretKey string) string {
298+
// return default project ID (organization ID) if it cannot find it
299+
func getAPIKeyDefaultProjectID(ctx context.Context, accessKey string, secretKey string, organizationID string) string {
300300
client := core.ExtractClient(ctx)
301301
api := iam.NewAPI(client)
302302

303303
apiKey, err := api.GetAPIKey(&iam.GetAPIKeyRequest{AccessKey: accessKey}, scw.WithAuthRequest(accessKey, secretKey))
304304
if err != nil && !is403Error(err) {
305305
// If 403 Unauthorized, API Key does not have permissions to get himself
306306
// It requires IAM permission to fetch an API Key
307-
return ""
307+
return organizationID
308308
}
309309

310310
if apiKey == nil {
311-
return ""
311+
return organizationID
312312
}
313313

314314
return apiKey.DefaultProjectID

internal/namespaces/init/prompt.go

+28
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,31 @@ func promptOrganizationID(ctx context.Context) (string, error) {
2828
})
2929
}
3030

31+
func promptManualProjectID(ctx context.Context, defaultProjectID string) (string, error) {
32+
_, _ = interactive.Println()
33+
return interactive.PromptStringWithConfig(&interactive.PromptStringConfig{
34+
Ctx: ctx,
35+
Prompt: "Choose your default project ID",
36+
DefaultValue: defaultProjectID,
37+
DefaultValueDoc: defaultProjectID,
38+
ValidateFunc: func(s string) error {
39+
if !validation.IsProjectID(s) {
40+
return fmt.Errorf("organization id is not a valid uuid")
41+
}
42+
return nil
43+
},
44+
})
45+
}
46+
3147
func promptProjectID(ctx context.Context, accessKey string, secretKey string, organizationID string, defaultProjectID string) (string, error) {
48+
if defaultProjectID == "" {
49+
defaultProjectID = organizationID
50+
}
51+
52+
if !interactive.IsInteractive {
53+
return defaultProjectID, nil
54+
}
55+
3256
client := core.ExtractClient(ctx)
3357
api := account.NewAPI(client)
3458

@@ -39,6 +63,10 @@ func promptProjectID(ctx context.Context, accessKey string, secretKey string, or
3963
return "", fmt.Errorf("failed to list projects: %w", err)
4064
}
4165

66+
if len(res.Projects) == 0 {
67+
return promptManualProjectID(ctx, defaultProjectID)
68+
}
69+
4270
defaultIndex := 0
4371

4472
projects := make([]string, len(res.Projects))

0 commit comments

Comments
 (0)