Skip to content

Onboard Argus: plans command #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 8, 2024
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.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ stackit [flags]

### SEE ALSO

* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
* [stackit auth](./stackit_auth.md) - Provides authentication functionality
* [stackit config](./stackit_config.md) - Provides functionality for CLI configuration options
* [stackit curl](./stackit_curl.md) - Executes an authenticated HTTP request to an endpoint
Expand Down
32 changes: 32 additions & 0 deletions docs/stackit_argus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## stackit argus

Provides functionality for Argus

### Synopsis

Provides functionality for Argus.

```
stackit argus [flags]
```

### Options

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

### 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"]
-p, --project-id string Project ID
```

### SEE ALSO

* [stackit](./stackit.md) - Manage STACKIT resources using the command line
* [stackit argus plans](./stackit_argus_plans.md) - Lists all Argus service plans

45 changes: 45 additions & 0 deletions docs/stackit_argus_plans.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## stackit argus plans

Lists all Argus service plans

### Synopsis

Lists all Argus service plans.

```
stackit argus plans [flags]
```

### Examples

```
List all Argus service plans
$ stackit argus plans

List all Argus service plans in JSON format
$ stackit argus plans --output-format json

List up to 10 Argus service plans
$ stackit argus plans --limit 10
```

### Options

```
-h, --help Help for "stackit argus plans"
--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"]
-p, --project-id string Project ID
```

### SEE ALSO

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

1 change: 1 addition & 0 deletions docs/stackit_config_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ stackit config set [flags]
### Options

```
--argus-custom-endpoint string Argus API base URL, used in calls to this API
--authorization-custom-endpoint string Authorization API base URL, used in calls to this API
--dns-custom-endpoint string DNS API base URL, used in calls to this API
-h, --help Help for "stackit config set"
Expand Down
1 change: 1 addition & 0 deletions docs/stackit_config_unset.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ stackit config unset [flags]
### Options

```
--argus-custom-endpoint Argus API base URL. If unset, uses the default base URL
--async Configuration option to run commands asynchronously
--authorization-custom-endpoint Authorization API base URL. If unset, uses the default base URL
--dns-custom-endpoint DNS API base URL. If unset, uses the default base URL
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5
github.com/stackitcloud/stackit-sdk-go/services/logme v0.10.1
github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.10.1
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v0.8.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
github.com/stackitcloud/stackit-sdk-go/core v0.10.0 h1:IcY8xa/6wo8EhRE9mpCvz4EtTkkoiIa2ZwPHuc5zGyw=
github.com/stackitcloud/stackit-sdk-go/core v0.10.0/go.mod h1:B5dkVm2HlBRG7liBVIFNqncDb6TUHnJ7t0GsKhAFuRk=
github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5 h1:RLsA2jO9sMNpn7NYdtFyumY5Vj4n4WtBm2J2NBKlsnw=
github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5/go.mod h1:lzGbqwV0hqeX/kUvaaFTgjOJRxUlsZ911TX1YAcKwqc=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.1.1 h1:h7dCaBlbU34WSGuEXREmCdCzQafZgdXDZuairAzeuo8=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.1.1/go.mod h1:V+wTIfuJRV8PiSOfMX6GCTaHWltGaLCz8ImOKeHIaIA=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.8.4 h1:n/X2pVdETDXGHk+vCsg0p3b2zGxSRMJ065to/aAoncg=
Expand Down
25 changes: 25 additions & 0 deletions internal/cmd/argus/argus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package argus

import (
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/plans"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "argus",
Short: "Provides functionality for Argus",
Long: "Provides functionality for Argus.",
Args: args.NoArgs,
Run: utils.CmdHelp,
}
addSubcommands(cmd)
return cmd
}

func addSubcommands(cmd *cobra.Command) {
cmd.AddCommand(plans.NewCmd())
}
144 changes: 144 additions & 0 deletions internal/cmd/argus/plans/plans.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package plans

import (
"context"
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"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/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"

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

const (
limitFlag = "limit"
)

type inputModel struct {
*globalflags.GlobalFlagModel
Limit *int64
}

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "plans",
Short: "Lists all Argus service plans",
Long: "Lists all Argus service plans.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`List all Argus service plans`,
"$ stackit argus plans"),
examples.NewExample(
`List all Argus service plans in JSON format`,
"$ stackit argus plans --output-format json"),
examples.NewExample(
`List up to 10 Argus service plans`,
"$ stackit argus plans --limit 10"),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(cmd)
if err != nil {
return err
}

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

// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("get Argus service plans: %w", err)
}
plans := *resp.Plans
if len(plans) == 0 {
projectLabel, err := projectname.GetProjectName(ctx, cmd)
if err != nil {
projectLabel = model.ProjectId
}
cmd.Printf("No plans found for project %q\n", projectLabel)
return nil
}

// Truncate output
if model.Limit != nil && len(plans) > int(*model.Limit) {
plans = plans[:*model.Limit]
}

return outputResult(cmd, model.OutputFormat, plans)
},
}

configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
}

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

limit := flags.FlagToInt64Pointer(cmd, limitFlag)
if limit != nil && *limit < 1 {
return nil, &errors.FlagValidationError{
Flag: limitFlag,
Details: "must be greater than 0",
}
}

return &inputModel{
GlobalFlagModel: globalFlags,
Limit: limit,
}, nil
}

func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiListPlansRequest {
req := apiClient.ListPlans(ctx, model.ProjectId)
return req
}

func outputResult(cmd *cobra.Command, outputFormat string, plans []argus.Plan) error {
switch outputFormat {
case globalflags.JSONOutputFormat:
details, err := json.MarshalIndent(plans, "", " ")
if err != nil {
return fmt.Errorf("marshal Argus plans: %w", err)
}
cmd.Println(string(details))

return nil
default:
table := tables.NewTable()
table.SetHeader("ID", "PLAN NAME", "DESCRIPTION")
for i := range plans {
o := plans[i]
table.AddRow(*o.Id, *o.Name, *o.Description)
table.AddSeparator()
}
table.EnableAutoMergeOnColumns(1)
err := table.Display(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
}
}
Loading