Skip to content

Commit 9f098bf

Browse files
authored
Onboard Argus: plans command (#129)
* Onboard Argus: config and client impl * Onboard Argus: plans command * integrate argus in unset tests * Addressed comments
1 parent 058a4ca commit 9f098bf

File tree

16 files changed

+504
-1
lines changed

16 files changed

+504
-1
lines changed

docs/stackit.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ stackit [flags]
2525

2626
### SEE ALSO
2727

28+
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
2829
* [stackit auth](./stackit_auth.md) - Provides authentication functionality
2930
* [stackit config](./stackit_config.md) - Provides functionality for CLI configuration options
3031
* [stackit curl](./stackit_curl.md) - Executes an authenticated HTTP request to an endpoint

docs/stackit_argus.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## stackit argus
2+
3+
Provides functionality for Argus
4+
5+
### Synopsis
6+
7+
Provides functionality for Argus.
8+
9+
```
10+
stackit argus [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Help for "stackit argus"
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+
```
27+
28+
### SEE ALSO
29+
30+
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
31+
* [stackit argus plans](./stackit_argus_plans.md) - Lists all Argus service plans
32+

docs/stackit_argus_plans.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## stackit argus plans
2+
3+
Lists all Argus service plans
4+
5+
### Synopsis
6+
7+
Lists all Argus service plans.
8+
9+
```
10+
stackit argus plans [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
List all Argus service plans
17+
$ stackit argus plans
18+
19+
List all Argus service plans in JSON format
20+
$ stackit argus plans --output-format json
21+
22+
List up to 10 Argus service plans
23+
$ stackit argus plans --limit 10
24+
```
25+
26+
### Options
27+
28+
```
29+
-h, --help Help for "stackit argus plans"
30+
--limit int Maximum number of entries to list
31+
```
32+
33+
### Options inherited from parent commands
34+
35+
```
36+
-y, --assume-yes If set, skips all confirmation prompts
37+
--async If set, runs the command asynchronously
38+
-o, --output-format string Output format, one of ["json" "pretty"]
39+
-p, --project-id string Project ID
40+
```
41+
42+
### SEE ALSO
43+
44+
* [stackit argus](./stackit_argus.md) - Provides functionality for Argus
45+

docs/stackit_config_set.md

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

3131
```
32+
--argus-custom-endpoint string Argus API base URL, used in calls to this API
3233
--authorization-custom-endpoint string Authorization API base URL, used in calls to this API
3334
--dns-custom-endpoint string DNS API base URL, used in calls to this API
3435
-h, --help Help for "stackit config set"

docs/stackit_config_unset.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ stackit config unset [flags]
2626
### Options
2727

2828
```
29+
--argus-custom-endpoint Argus API base URL. If unset, uses the default base URL
2930
--async Configuration option to run commands asynchronously
3031
--authorization-custom-endpoint Authorization API base URL. If unset, uses the default base URL
3132
--dns-custom-endpoint DNS API base URL. If unset, uses the default base URL

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/sourcegraph/conc v0.3.0 // indirect
4949
github.com/spf13/afero v1.11.0 // indirect
5050
github.com/spf13/cast v1.6.0 // indirect
51+
github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5
5152
github.com/stackitcloud/stackit-sdk-go/services/logme v0.10.1
5253
github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.10.1
5354
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v0.8.6

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
7575
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
7676
github.com/stackitcloud/stackit-sdk-go/core v0.10.0 h1:IcY8xa/6wo8EhRE9mpCvz4EtTkkoiIa2ZwPHuc5zGyw=
7777
github.com/stackitcloud/stackit-sdk-go/core v0.10.0/go.mod h1:B5dkVm2HlBRG7liBVIFNqncDb6TUHnJ7t0GsKhAFuRk=
78+
github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5 h1:RLsA2jO9sMNpn7NYdtFyumY5Vj4n4WtBm2J2NBKlsnw=
79+
github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5/go.mod h1:lzGbqwV0hqeX/kUvaaFTgjOJRxUlsZ911TX1YAcKwqc=
7880
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.1.1 h1:h7dCaBlbU34WSGuEXREmCdCzQafZgdXDZuairAzeuo8=
7981
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.1.1/go.mod h1:V+wTIfuJRV8PiSOfMX6GCTaHWltGaLCz8ImOKeHIaIA=
8082
github.com/stackitcloud/stackit-sdk-go/services/dns v0.8.4 h1:n/X2pVdETDXGHk+vCsg0p3b2zGxSRMJ065to/aAoncg=

internal/cmd/argus/argus.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package argus
2+
3+
import (
4+
"github.com/stackitcloud/stackit-cli/internal/cmd/argus/plans"
5+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
6+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func NewCmd() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "argus",
14+
Short: "Provides functionality for Argus",
15+
Long: "Provides functionality for Argus.",
16+
Args: args.NoArgs,
17+
Run: utils.CmdHelp,
18+
}
19+
addSubcommands(cmd)
20+
return cmd
21+
}
22+
23+
func addSubcommands(cmd *cobra.Command) {
24+
cmd.AddCommand(plans.NewCmd())
25+
}

internal/cmd/argus/plans/plans.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package plans
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/client"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
16+
17+
"github.com/spf13/cobra"
18+
"github.com/stackitcloud/stackit-sdk-go/services/argus"
19+
)
20+
21+
const (
22+
limitFlag = "limit"
23+
)
24+
25+
type inputModel struct {
26+
*globalflags.GlobalFlagModel
27+
Limit *int64
28+
}
29+
30+
func NewCmd() *cobra.Command {
31+
cmd := &cobra.Command{
32+
Use: "plans",
33+
Short: "Lists all Argus service plans",
34+
Long: "Lists all Argus service plans.",
35+
Args: args.NoArgs,
36+
Example: examples.Build(
37+
examples.NewExample(
38+
`List all Argus service plans`,
39+
"$ stackit argus plans"),
40+
examples.NewExample(
41+
`List all Argus service plans in JSON format`,
42+
"$ stackit argus plans --output-format json"),
43+
examples.NewExample(
44+
`List up to 10 Argus service plans`,
45+
"$ stackit argus plans --limit 10"),
46+
),
47+
RunE: func(cmd *cobra.Command, args []string) error {
48+
ctx := context.Background()
49+
model, err := parseInput(cmd)
50+
if err != nil {
51+
return err
52+
}
53+
54+
// Configure API client
55+
apiClient, err := client.ConfigureClient(cmd)
56+
if err != nil {
57+
return err
58+
}
59+
60+
// Call API
61+
req := buildRequest(ctx, model, apiClient)
62+
resp, err := req.Execute()
63+
if err != nil {
64+
return fmt.Errorf("get Argus service plans: %w", err)
65+
}
66+
plans := *resp.Plans
67+
if len(plans) == 0 {
68+
projectLabel, err := projectname.GetProjectName(ctx, cmd)
69+
if err != nil {
70+
projectLabel = model.ProjectId
71+
}
72+
cmd.Printf("No plans found for project %q\n", projectLabel)
73+
return nil
74+
}
75+
76+
// Truncate output
77+
if model.Limit != nil && len(plans) > int(*model.Limit) {
78+
plans = plans[:*model.Limit]
79+
}
80+
81+
return outputResult(cmd, model.OutputFormat, plans)
82+
},
83+
}
84+
85+
configureFlags(cmd)
86+
return cmd
87+
}
88+
89+
func configureFlags(cmd *cobra.Command) {
90+
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
91+
}
92+
93+
func parseInput(cmd *cobra.Command) (*inputModel, error) {
94+
globalFlags := globalflags.Parse(cmd)
95+
if globalFlags.ProjectId == "" {
96+
return nil, &errors.ProjectIdError{}
97+
}
98+
99+
limit := flags.FlagToInt64Pointer(cmd, limitFlag)
100+
if limit != nil && *limit < 1 {
101+
return nil, &errors.FlagValidationError{
102+
Flag: limitFlag,
103+
Details: "must be greater than 0",
104+
}
105+
}
106+
107+
return &inputModel{
108+
GlobalFlagModel: globalFlags,
109+
Limit: limit,
110+
}, nil
111+
}
112+
113+
func buildRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiListPlansRequest {
114+
req := apiClient.ListPlans(ctx, model.ProjectId)
115+
return req
116+
}
117+
118+
func outputResult(cmd *cobra.Command, outputFormat string, plans []argus.Plan) error {
119+
switch outputFormat {
120+
case globalflags.JSONOutputFormat:
121+
details, err := json.MarshalIndent(plans, "", " ")
122+
if err != nil {
123+
return fmt.Errorf("marshal Argus plans: %w", err)
124+
}
125+
cmd.Println(string(details))
126+
127+
return nil
128+
default:
129+
table := tables.NewTable()
130+
table.SetHeader("ID", "PLAN NAME", "DESCRIPTION")
131+
for i := range plans {
132+
o := plans[i]
133+
table.AddRow(*o.Id, *o.Name, *o.Description)
134+
table.AddSeparator()
135+
}
136+
table.EnableAutoMergeOnColumns(1)
137+
err := table.Display(cmd)
138+
if err != nil {
139+
return fmt.Errorf("render table: %w", err)
140+
}
141+
142+
return nil
143+
}
144+
}

0 commit comments

Comments
 (0)