1- package create
1+ package add
22
33import (
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-
2520const (
2621 usernameFlag = "username"
2722 displaynameFlag = "displayname"
23+ passwordFlag = "password"
2824)
2925
3026type inputModel struct {
@@ -36,24 +32,25 @@ type inputModel struct {
3632
3733func 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 {
8986func 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 () {
0 commit comments