Skip to content

Commit 934b1a3

Browse files
committed
feat(credentials): Integrated review findings
1 parent 2ece9e5 commit 934b1a3

File tree

12 files changed

+53
-115
lines changed

12 files changed

+53
-115
lines changed

internal/cmd/beta/alb/alb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package alb
22

33
import (
44
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/create"
5-
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/credentials"
65
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/delete"
76
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/describe"
87
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/list"
8+
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/observability-credentials"
99
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/plans"
1010
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/pool"
1111
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/quotas"

internal/cmd/beta/alb/credentials/create/create.go renamed to internal/cmd/beta/alb/observability-credentials/add/add.go

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
package create
1+
package add
22

33
import (
4-
"bytes"
54
"context"
65
"encoding/json"
76
"fmt"
8-
"os"
97

108
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
119
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1210
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1311
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1412
"github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client"
1513
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
16-
"golang.org/x/term"
1714

1815
"github.com/goccy/go-yaml"
1916
"github.com/spf13/cobra"
2017
"github.com/stackitcloud/stackit-sdk-go/services/alb"
2118
)
2219

23-
const passwordEnv = "ALB_CREDENTIALS_PASSWORD"
24-
2520
const (
2621
usernameFlag = "username"
2722
displaynameFlag = "displayname"
23+
passwordFlag = "password"
2824
)
2925

3026
type inputModel struct {
@@ -36,24 +32,25 @@ type inputModel struct {
3632

3733
func NewCmd(p *print.Printer) *cobra.Command {
3834
cmd := &cobra.Command{
39-
Use: "create",
40-
Short: "Creates a credential",
41-
Long: "Creates a credential.",
35+
Use: "add",
36+
Short: "Adds observability credentials to an application load balancer",
37+
Long: "Adds observability credentials (username and password) to an application load balancer. The credentials can be for Observability or another monitoring tool.",
4238
Args: cobra.NoArgs,
4339
Example: examples.Build(
4440
examples.NewExample(
45-
`Create a new credential, the password is requested interactively or read from ENV variable `+passwordEnv,
46-
"$ stackit beta alb credential create --username some.user --displayname master-creds",
47-
),
41+
`Add observability credentials to an application load balancer with username "xxx" and display name "yyy". The password is entered using the terminal`,
42+
"$ stackit beta alb observability-credentials add --username xxx --display-name yyy"),
43+
examples.NewExample(
44+
`Add observability credentials to a load balancer with username "xxx" and display name "yyy", providing the path to a file with the password as flag`,
45+
"$ stackit beta alb observability-credentials add --username xxx --password @./password.txt --display-name yyy"),
46+
examples.NewExample(
47+
`Add observability credentials to a load balancer with username "xxx" and display name "yyy", providing the password via an environment variable`,
48+
"$ ALB_CREDENTIALS_PASSWORD stackit beta alb observability-credentials add --username xxx --display-name yyy"),
4849
),
4950
RunE: func(cmd *cobra.Command, _ []string) error {
5051
ctx := context.Background()
5152

52-
password, err := readPassword()
53-
if err != nil {
54-
return err
55-
}
56-
model, err := parseInput(p, cmd, password)
53+
model, err := parseInput(p, cmd)
5754
if err != nil {
5855
return err
5956
}
@@ -65,7 +62,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
6562
}
6663

6764
if !model.AssumeYes {
68-
prompt := "Are your sure you want to create a credential?"
65+
prompt := "Are your sure you want to add credentials?"
6966
err = p.PromptForConfirmation(prompt)
7067
if err != nil {
7168
return err
@@ -76,7 +73,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7673
req := buildRequest(ctx, model, apiClient)
7774
resp, err := req.Execute()
7875
if err != nil {
79-
return fmt.Errorf("create credential: %w", err)
76+
return fmt.Errorf("add credential: %w", err)
8077
}
8178

8279
return outputResult(p, model.GlobalFlagModel.OutputFormat, resp)
@@ -89,43 +86,19 @@ func NewCmd(p *print.Printer) *cobra.Command {
8986
func configureFlags(cmd *cobra.Command) {
9087
cmd.Flags().StringP(usernameFlag, "u", "", "the username for the credentials")
9188
cmd.Flags().StringP(displaynameFlag, "d", "", "the displayname for the credentials")
89+
cmd.Flags().Var(flags.ReadFromFileFlag(), passwordFlag, `Password. Can be a string or a file path, if prefixed with "@" (example: @./password.txt).`)
9290

93-
cobra.CheckErr(cmd.MarkFlagRequired(usernameFlag))
94-
cobra.CheckErr(cmd.MarkFlagRequired(displaynameFlag))
95-
}
96-
97-
func readPassword() (string, error) {
98-
if password, found := os.LookupEnv(passwordEnv); found {
99-
return password, nil
100-
}
101-
102-
fmt.Printf("please provide the password: ")
103-
password, err := term.ReadPassword(int(os.Stdout.Fd()))
104-
if err != nil {
105-
return "", fmt.Errorf("cannot read password: %w", err)
106-
}
107-
fmt.Println()
108-
fmt.Printf("please confirm the password: ")
109-
confirmation, err := term.ReadPassword(int(os.Stdout.Fd()))
110-
if err != nil {
111-
return "", fmt.Errorf("cannot read password: %w", err)
112-
}
113-
fmt.Println()
114-
if !bytes.Equal(password, confirmation) {
115-
return "", fmt.Errorf("the password and the confirmation do not match")
116-
}
117-
118-
return string(password), nil
91+
cobra.CheckErr(flags.MarkFlagsRequired(cmd, usernameFlag, displaynameFlag))
11992
}
12093

121-
func parseInput(p *print.Printer, cmd *cobra.Command, password string) (*inputModel, error) {
94+
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
12295
globalFlags := globalflags.Parse(p, cmd)
12396

12497
model := inputModel{
12598
GlobalFlagModel: globalFlags,
12699
Username: flags.FlagToStringPointer(p, cmd, usernameFlag),
127100
Displayname: flags.FlagToStringPointer(p, cmd, displaynameFlag),
128-
Password: &password,
101+
Password: flags.FlagToStringPointer(p, cmd, passwordFlag),
129102
}
130103

131104
if p.IsVerbosityDebug() {

internal/cmd/beta/alb/credentials/create/create_test.go renamed to internal/cmd/beta/alb/observability-credentials/add/add_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package create
1+
package add
22

33
import (
44
"context"
@@ -32,6 +32,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
3232
globalflags.RegionFlag: testRegion,
3333
usernameFlag: testUsername,
3434
displaynameFlag: testDisplayname,
35+
passwordFlag: testPassword,
3536
}
3637
for _, mod := range mods {
3738
mod(flagValues)
@@ -124,7 +125,7 @@ func TestParseInput(t *testing.T) {
124125
t.Fatalf("error validating flags: %v", err)
125126
}
126127

127-
model, err := parseInput(p, cmd, testPassword)
128+
model, err := parseInput(p, cmd)
128129
if err != nil {
129130
if !tt.isValid {
130131
return

internal/cmd/beta/alb/credentials/delete/delete.go renamed to internal/cmd/beta/alb/observability-credentials/delete/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
3232
Example: examples.Build(
3333
examples.NewExample(
3434
`Delete credential with name "credential-12345"`,
35-
"$ stackit beta alb credentials delete credential-12345",
35+
"$ stackit beta alb observability-credentials delete credential-12345",
3636
),
3737
),
3838
RunE: func(cmd *cobra.Command, args []string) error {

internal/cmd/beta/alb/credentials/describe/describe.go renamed to internal/cmd/beta/alb/observability-credentials/describe/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
3737
Example: examples.Build(
3838
examples.NewExample(
3939
`Get details about credentials with name "credential-12345"`,
40-
"$ stackit beta alb credential describe credential-12345",
40+
"$ stackit beta alb observability-credentials describe credential-12345",
4141
),
4242
),
4343
RunE: func(cmd *cobra.Command, args []string) error {

internal/cmd/beta/alb/credentials/list/list.go renamed to internal/cmd/beta/alb/observability-credentials/list/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ func NewCmd(p *print.Printer) *cobra.Command {
3939
Example: examples.Build(
4040
examples.NewExample(
4141
`Lists all credentials`,
42-
"$ stackit beta alb credential list",
42+
"$ stackit beta alb observability-credentials list",
4343
),
4444
examples.NewExample(
4545
`Lists all credentials in JSON format`,
46-
"$ stackit beta alb credential list --output-format json",
46+
"$ stackit beta alb observability-credentials list --output-format json",
4747
),
4848
examples.NewExample(
4949
`Lists up to 10 credentials`,
50-
"$ stackit beta alb credential list --limit 10",
50+
"$ stackit beta alb observability-credentials list --limit 10",
5151
),
5252
),
5353
RunE: func(cmd *cobra.Command, _ []string) error {

internal/cmd/beta/alb/credentials/credentials.go renamed to internal/cmd/beta/alb/observability-credentials/observability-credentials.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ package credentials
33
import (
44
"github.com/spf13/cobra"
55

6-
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/credentials/create"
7-
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/credentials/delete"
8-
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/credentials/describe"
9-
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/credentials/list"
10-
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/credentials/update"
6+
add "github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/observability-credentials/add"
7+
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/observability-credentials/delete"
8+
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/observability-credentials/describe"
9+
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/observability-credentials/list"
10+
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/alb/observability-credentials/update"
1111
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1212
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1313
)
1414

1515
func NewCmd(p *print.Printer) *cobra.Command {
1616
cmd := &cobra.Command{
17-
Use: "credentials",
17+
Use: "observability-credentials",
1818
Short: "Provides functionality for application loadbalancer credentials",
1919
Long: "Provides functionality for application loadbalancer credentials",
2020
Args: cobra.NoArgs,
@@ -25,7 +25,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
2525
}
2626

2727
func addSubcommands(cmd *cobra.Command, p *print.Printer) {
28-
cmd.AddCommand(create.NewCmd(p))
28+
cmd.AddCommand(add.NewCmd(p))
2929
cmd.AddCommand(delete.NewCmd(p))
3030
cmd.AddCommand(describe.NewCmd(p))
3131
cmd.AddCommand(list.NewCmd(p))

0 commit comments

Comments
 (0)