Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/stackit_argus.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ stackit argus [flags]
### SEE ALSO

* [stackit](./stackit.md) - Manage STACKIT resources using the command line
* [stackit argus credentials](./stackit_argus_credentials.md) - Provides functionality for Argus credentials
* [stackit argus grafana](./stackit_argus_grafana.md) - Provides functionality for the Grafana configuration of Argus instances
* [stackit argus instance](./stackit_argus_instance.md) - Provides functionality for Argus instances
* [stackit argus plans](./stackit_argus_plans.md) - Lists all Argus service plans
Expand Down
35 changes: 35 additions & 0 deletions docs/stackit_argus_credentials.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## stackit argus credentials

Provides functionality for Argus credentials

### Synopsis

Provides functionality for Argus credentials.

```
stackit argus credentials [flags]
```

### Options

```
-h, --help Help for "stackit argus credentials"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
* [stackit argus credentials create](./stackit_argus_credentials_create.md) - Creates credentials for an Argus instance.
* [stackit argus credentials delete](./stackit_argus_credentials_delete.md) - Deletes credentials of an Argus instance
* [stackit argus credentials list](./stackit_argus_credentials_list.md) - Lists the usernames of all credentials for an Argus instance

41 changes: 41 additions & 0 deletions docs/stackit_argus_credentials_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## stackit argus credentials create

Creates credentials for an Argus instance.

### Synopsis

Creates credentials (username and password) for an Argus instance.
The credentials will be generated and included in the response. You won't be able to retrieve the password later.

```
stackit argus credentials create [flags]
```

### Examples

```
Create credentials for Argus instance with ID "xxx"
$ stackit argus credentials create --instance-id xxx
```

### Options

```
-h, --help Help for "stackit argus credentials create"
--instance-id string Instance ID
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit argus credentials](./stackit_argus_credentials.md) - Provides functionality for Argus credentials

40 changes: 40 additions & 0 deletions docs/stackit_argus_credentials_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit argus credentials delete

Deletes credentials of an Argus instance

### Synopsis

Deletes credentials of an Argus instance.

```
stackit argus credentials delete USERNAME [flags]
```

### Examples

```
Delete credentials of username "xxx" for Argus instance with ID "yyy"
$ stackit argus credentials delete xxx --instance-id yyy
```

### Options

```
-h, --help Help for "stackit argus credentials delete"
--instance-id string Instance ID
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit argus credentials](./stackit_argus_credentials.md) - Provides functionality for Argus credentials

47 changes: 47 additions & 0 deletions docs/stackit_argus_credentials_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## stackit argus credentials list

Lists the usernames of all credentials for an Argus instance

### Synopsis

Lists the usernames of all credentials for an Argus instance.

```
stackit argus credentials list [flags]
```

### Examples

```
List the usernames of all credentials for an Argus instance with ID "xxx"
$ stackit argus credentials list --instance-id xxx

List the usernames of all credentials for an Argus instance in JSON format
$ stackit argus credentials list --instance-id xxx --output-format json

List the usernames of up to 10 credentials for an Argus instance
$ stackit argus credentials list --instance-id xxx --limit 10
```

### Options

```
-h, --help Help for "stackit argus credentials list"
--instance-id string Instance ID
--limit int Maximum number of entries to list
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit argus credentials](./stackit_argus_credentials.md) - Provides functionality for Argus credentials

2 changes: 2 additions & 0 deletions internal/cmd/argus/argus.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package argus

import (
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/credentials"
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/grafana"
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/instance"
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/plans"
Expand All @@ -27,6 +28,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
func addSubcommands(cmd *cobra.Command, p *print.Printer) {
cmd.AddCommand(grafana.NewCmd(p))
cmd.AddCommand(instance.NewCmd(p))
cmd.AddCommand(credentials.NewCmd(p))
cmd.AddCommand(scrapeconfig.NewCmd(p))
cmd.AddCommand(plans.NewCmd(p))
}
117 changes: 117 additions & 0 deletions internal/cmd/argus/credentials/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package create

import (
"context"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client"
argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/argus"
)

const (
instanceIdFlag = "instance-id"
)

type inputModel struct {
*globalflags.GlobalFlagModel

InstanceId string
}

func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates credentials for an Argus instance.",
Long: fmt.Sprintf("%s\n%s",
"Creates credentials (username and password) for an Argus instance.",
"The credentials will be generated and included in the response. You won't be able to retrieve the password later."),
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create credentials for Argus instance with ID "xxx"`,
"$ stackit argus credentials create --instance-id xxx"),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(p, cmd)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(p)
if err != nil {
return err
}

instanceLabel, err := argusUtils.GetInstanceName(ctx, apiClient, model.InstanceId, model.ProjectId)
if err != nil {
p.Debug(print.ErrorLevel, "get instance name: %v", err)
instanceLabel = model.InstanceId
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create credentials for instance %q?", instanceLabel)
err = p.PromptForConfirmation(prompt)
if err != nil {
return err
}
}

// Call API
req := buildRequest(ctx, model, apiClient)
if err != nil {
return err
}
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("create credentials for Argus instance: %w", err)
}

p.Outputf("Created credentials for instance %q.\n\n", instanceLabel)
// The username field cannot be set by the user so we only display it if it's not returned empty
username := *resp.Credentials.Username
if username != "" {
p.Outputf("Username: %s\n", username)
}

p.Outputf("Password: %s\n", *resp.Credentials.Password)
return nil
},
}
configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID")

err := flags.MarkFlagsRequired(cmd, instanceIdFlag)
cobra.CheckErr(err)
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &cliErr.ProjectIdError{}
}

return &inputModel{
GlobalFlagModel: globalFlags,
InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag),
}, nil
}

func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiCreateCredentialsRequest {
req := apiClient.CreateCredentials(ctx, model.InstanceId, model.ProjectId)
return req
}
Loading