Skip to content

Commit fef9517

Browse files
authored
Onboard Argus credentials: create command (#216)
* argus credentials create command implementation * improve documentation, add test cases for hide-password
1 parent 69041f2 commit fef9517

File tree

8 files changed

+453
-13
lines changed

8 files changed

+453
-13
lines changed

docs/stackit.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,22 @@ stackit [flags]
2626

2727
### SEE ALSO
2828

29-
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
3029
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
3130
* [stackit auth](./stackit_auth.md) - Provides authentication functionality
3231
* [stackit config](./stackit_config.md) - Provides functionality for CLI configuration options
3332
* [stackit curl](./stackit_curl.md) - Executes an authenticated HTTP request to an endpoint
3433
* [stackit dns](./stackit_dns.md) - Provides functionality for DNS
3534
* [stackit logme](./stackit_logme.md) - Provides functionality for LogMe
36-
* [stackit logme](./stackit_logme.md) - Provides functionality for LogMe
3735
* [stackit mariadb](./stackit_mariadb.md) - Provides functionality for MariaDB
38-
* [stackit mariadb](./stackit_mariadb.md) - Provides functionality for MariaDB
39-
* [stackit mongodbflex](./stackit_mongodbflex.md) - Provides functionality for MongoDB Flex
4036
* [stackit mongodbflex](./stackit_mongodbflex.md) - Provides functionality for MongoDB Flex
4137
* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage
42-
* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage
43-
* [stackit opensearch](./stackit_opensearch.md) - Provides functionality for OpenSearch
4438
* [stackit opensearch](./stackit_opensearch.md) - Provides functionality for OpenSearch
4539
* [stackit organization](./stackit_organization.md) - Provides functionality regarding organizations
46-
* [stackit organization](./stackit_organization.md) - Provides functionality regarding organizations
47-
* [stackit postgresflex](./stackit_postgresflex.md) - Provides functionality for PostgreSQL Flex
4840
* [stackit postgresflex](./stackit_postgresflex.md) - Provides functionality for PostgreSQL Flex
4941
* [stackit project](./stackit_project.md) - Provides functionality regarding projects
5042
* [stackit rabbitmq](./stackit_rabbitmq.md) - Provides functionality for RabbitMQ
51-
* [stackit rabbitmq](./stackit_rabbitmq.md) - Provides functionality for RabbitMQ
52-
* [stackit redis](./stackit_redis.md) - Provides functionality for Redis
5343
* [stackit redis](./stackit_redis.md) - Provides functionality for Redis
5444
* [stackit secrets-manager](./stackit_secrets-manager.md) - Provides functionality for Secrets Manager
55-
* [stackit secrets-manager](./stackit_secrets-manager.md) - Provides functionality for Secrets Manager
56-
* [stackit service-account](./stackit_service-account.md) - Provides functionality for service accounts
5745
* [stackit service-account](./stackit_service-account.md) - Provides functionality for service accounts
5846
* [stackit ske](./stackit_ske.md) - Provides functionality for SKE
59-
* [stackit ske](./stackit_ske.md) - Provides functionality for SKE
6047

docs/stackit_argus.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ stackit argus [flags]
2929
### SEE ALSO
3030

3131
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
32+
* [stackit argus credentials](./stackit_argus_credentials.md) - Provides functionality for Argus credentials
3233
* [stackit argus instance](./stackit_argus_instance.md) - Provides functionality for Argus instances
3334
* [stackit argus plans](./stackit_argus_plans.md) - Lists all Argus service plans
3435

docs/stackit_argus_credentials.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## stackit argus credentials
2+
3+
Provides functionality for Argus credentials
4+
5+
### Synopsis
6+
7+
Provides functionality for Argus credentials.
8+
9+
```
10+
stackit argus credentials [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Help for "stackit argus credentials"
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
-y, --assume-yes If set, skips all confirmation prompts
23+
--async If set, runs the command asynchronously
24+
-o, --output-format string Output format, one of ["json" "pretty"]
25+
-p, --project-id string Project ID
26+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
27+
```
28+
29+
### SEE ALSO
30+
31+
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
32+
* [stackit argus credentials create](./stackit_argus_credentials_create.md) - Creates credentials for an Argus instance.
33+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## stackit argus credentials create
2+
3+
Creates credentials for an Argus instance.
4+
5+
### Synopsis
6+
7+
Creates credentials for an Argus instance.
8+
9+
```
10+
stackit argus credentials create [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Create credentials for Argus instance with ID "xxx"
17+
$ stackit argus credentials create --instance-id xxx
18+
19+
Create credentials for Argus instance with ID "xxx" and hide the password in the output
20+
$ stackit argus credentials create --instance-id xxx --hide-password
21+
```
22+
23+
### Options
24+
25+
```
26+
-h, --help Help for "stackit argus credentials create"
27+
--hide-password Hide password in output
28+
--instance-id string Instance ID
29+
```
30+
31+
### Options inherited from parent commands
32+
33+
```
34+
-y, --assume-yes If set, skips all confirmation prompts
35+
--async If set, runs the command asynchronously
36+
-o, --output-format string Output format, one of ["json" "pretty"]
37+
-p, --project-id string Project ID
38+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
39+
```
40+
41+
### SEE ALSO
42+
43+
* [stackit argus credentials](./stackit_argus_credentials.md) - Provides functionality for Argus credentials
44+

internal/cmd/argus/argus.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package argus
22

33
import (
4+
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/credentials"
45
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/instance"
56
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/plans"
67
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -25,4 +26,5 @@ func NewCmd(p *print.Printer) *cobra.Command {
2526
func addSubcommands(cmd *cobra.Command, p *print.Printer) {
2627
cmd.AddCommand(plans.NewCmd(p))
2728
cmd.AddCommand(instance.NewCmd(p))
29+
cmd.AddCommand(credentials.NewCmd(p))
2830
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package create
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
8+
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client"
14+
argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils"
15+
16+
"github.com/spf13/cobra"
17+
"github.com/stackitcloud/stackit-sdk-go/services/argus"
18+
)
19+
20+
const (
21+
instanceIdFlag = "instance-id"
22+
hidePasswordFlag = "hide-password"
23+
)
24+
25+
type inputModel struct {
26+
*globalflags.GlobalFlagModel
27+
28+
HidePassword bool
29+
InstanceId string
30+
}
31+
32+
func NewCmd(p *print.Printer) *cobra.Command {
33+
cmd := &cobra.Command{
34+
Use: "create",
35+
Short: "Creates credentials for an Argus instance.",
36+
Long: "Creates credentials for an Argus instance.",
37+
Args: args.NoArgs,
38+
Example: examples.Build(
39+
examples.NewExample(
40+
`Create credentials for Argus instance with ID "xxx"`,
41+
"$ stackit argus credentials create --instance-id xxx"),
42+
examples.NewExample(
43+
`Create credentials for Argus instance with ID "xxx" and hide the password in the output`,
44+
"$ stackit argus credentials create --instance-id xxx --hide-password"),
45+
),
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
ctx := context.Background()
48+
model, err := parseInput(cmd)
49+
if err != nil {
50+
return err
51+
}
52+
53+
// Configure API client
54+
apiClient, err := client.ConfigureClient(p)
55+
if err != nil {
56+
return err
57+
}
58+
59+
instanceLabel, err := argusUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
60+
if err != nil {
61+
instanceLabel = model.InstanceId
62+
}
63+
64+
if !model.AssumeYes {
65+
prompt := fmt.Sprintf("Are you sure you want to create credentials for instance %q?", instanceLabel)
66+
err = p.PromptForConfirmation(prompt)
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
72+
// Call API
73+
req := buildRequest(ctx, model, apiClient)
74+
if err != nil {
75+
return err
76+
}
77+
resp, err := req.Execute()
78+
if err != nil {
79+
return fmt.Errorf("create credentials for Argus instance: %w", err)
80+
}
81+
82+
p.Outputf("Created credentials for instance %q.\n\n", instanceLabel)
83+
// The username field cannot be set by the user so we only display it if it's not returned empty
84+
username := *resp.Credentials.Username
85+
if username != "" {
86+
p.Outputf("Username: %s\n", username)
87+
}
88+
if model.HidePassword {
89+
p.Outputf("Password: <hidden>\n")
90+
} else {
91+
p.Outputf("Password: %s\n", *resp.Credentials.Password)
92+
}
93+
return nil
94+
},
95+
}
96+
configureFlags(cmd)
97+
return cmd
98+
}
99+
100+
func configureFlags(cmd *cobra.Command) {
101+
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")
102+
cmd.Flags().Bool(hidePasswordFlag, false, "Hide password in output")
103+
104+
err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
105+
cobra.CheckErr(err)
106+
}
107+
108+
func parseInput(cmd *cobra.Command) (*inputModel, error) {
109+
globalFlags := globalflags.Parse(cmd)
110+
if globalFlags.ProjectId == "" {
111+
return nil, &cliErr.ProjectIdError{}
112+
}
113+
114+
return &inputModel{
115+
GlobalFlagModel: globalFlags,
116+
InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag),
117+
HidePassword: flags.FlagToBoolValue(cmd, hidePasswordFlag),
118+
}, nil
119+
}
120+
121+
func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiCreateCredentialsRequest {
122+
req := apiClient.CreateCredentials(ctx, model.InstanceId, model.ProjectId)
123+
return req
124+
}

0 commit comments

Comments
 (0)