Skip to content

Commit 2329310

Browse files
authored
feat(resourcemanager): List and describe organizations (#1312)
relates to STACKITCLI-325
1 parent f9e97d0 commit 2329310

File tree

8 files changed

+738
-0
lines changed

8 files changed

+738
-0
lines changed

docs/stackit_organization.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ stackit organization [flags]
3131
### SEE ALSO
3232

3333
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
34+
* [stackit organization describe](./stackit_organization_describe.md) - Show an organization
35+
* [stackit organization list](./stackit_organization_list.md) - Lists all organizations
3436
* [stackit organization member](./stackit_organization_member.md) - Manages organization members
3537
* [stackit organization role](./stackit_organization_role.md) - Manages organization roles
3638

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## stackit organization describe
2+
3+
Show an organization
4+
5+
### Synopsis
6+
7+
Show an organization.
8+
9+
```
10+
stackit organization describe [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Describe the organization with the organization uuid "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
17+
$ stackit organization describe xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
18+
19+
Describe the organization with the container id "foo-bar-organization"
20+
$ stackit organization describe foo-bar-organization
21+
```
22+
23+
### Options
24+
25+
```
26+
-h, --help Help for "stackit organization describe"
27+
```
28+
29+
### Options inherited from parent commands
30+
31+
```
32+
-y, --assume-yes If set, skips all confirmation prompts
33+
--async If set, runs the command asynchronously
34+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
35+
-p, --project-id string Project ID
36+
--region string Target region for region-specific requests
37+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
38+
```
39+
40+
### SEE ALSO
41+
42+
* [stackit organization](./stackit_organization.md) - Manages organizations
43+

docs/stackit_organization_list.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## stackit organization list
2+
3+
Lists all organizations
4+
5+
### Synopsis
6+
7+
Lists all organizations.
8+
9+
```
10+
stackit organization list [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Lists organizations for your user
17+
$ stackit organization list
18+
19+
Lists the first 10 organizations
20+
$ stackit organization list --limit 10
21+
```
22+
23+
### Options
24+
25+
```
26+
-h, --help Help for "stackit organization list"
27+
--limit int Maximum number of entries to list (default 50)
28+
```
29+
30+
### Options inherited from parent commands
31+
32+
```
33+
-y, --assume-yes If set, skips all confirmation prompts
34+
--async If set, runs the command asynchronously
35+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
36+
-p, --project-id string Project ID
37+
--region string Target region for region-specific requests
38+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
39+
```
40+
41+
### SEE ALSO
42+
43+
* [stackit organization](./stackit_organization.md) - Manages organizations
44+
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package describe
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/resourcemanager/client"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
17+
"github.com/stackitcloud/stackit-sdk-go/services/resourcemanager"
18+
)
19+
20+
const (
21+
organizationIdArg = "ORGANIZATION_ID"
22+
)
23+
24+
type inputModel struct {
25+
*globalflags.GlobalFlagModel
26+
OrganizationId string
27+
}
28+
29+
func NewCmd(params *types.CmdParams) *cobra.Command {
30+
cmd := &cobra.Command{
31+
Use: "describe",
32+
Short: "Show an organization",
33+
Long: "Show an organization.",
34+
// the arg can be the organization uuid or the container id, which is not a uuid, so no validation needed
35+
Args: args.SingleArg(organizationIdArg, nil),
36+
Example: examples.Build(
37+
examples.NewExample(
38+
`Describe the organization with the organization uuid "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"`,
39+
"$ stackit organization describe xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
40+
),
41+
examples.NewExample(
42+
`Describe the organization with the container id "foo-bar-organization"`,
43+
"$ stackit organization describe foo-bar-organization",
44+
),
45+
),
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
ctx := context.Background()
48+
model, err := parseInput(params.Printer, cmd, args)
49+
if err != nil {
50+
return err
51+
}
52+
53+
// Configure API client
54+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
55+
if err != nil {
56+
return err
57+
}
58+
59+
// Call API
60+
req := buildRequest(ctx, model, apiClient)
61+
resp, err := req.Execute()
62+
if err != nil {
63+
return err
64+
}
65+
66+
return outputResult(params.Printer, model.OutputFormat, resp)
67+
},
68+
}
69+
return cmd
70+
}
71+
72+
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
73+
organizationId := inputArgs[0]
74+
globalFlags := globalflags.Parse(p, cmd)
75+
76+
model := inputModel{
77+
GlobalFlagModel: globalFlags,
78+
OrganizationId: organizationId,
79+
}
80+
81+
p.DebugInputModel(model)
82+
return &model, nil
83+
}
84+
85+
func buildRequest(ctx context.Context, model *inputModel, apiClient *resourcemanager.APIClient) resourcemanager.ApiGetOrganizationRequest {
86+
req := apiClient.GetOrganization(ctx, model.OrganizationId)
87+
return req
88+
}
89+
90+
func outputResult(p *print.Printer, outputFormat string, organization *resourcemanager.OrganizationResponse) error {
91+
return p.OutputResult(outputFormat, organization, func() error {
92+
if organization == nil {
93+
p.Outputln("show organization: empty response")
94+
return nil
95+
}
96+
97+
table := tables.NewTable()
98+
99+
table.AddRow("ORGANIZATION ID", utils.PtrString(organization.OrganizationId))
100+
table.AddSeparator()
101+
table.AddRow("NAME", utils.PtrString(organization.Name))
102+
table.AddSeparator()
103+
table.AddRow("CONTAINER ID", utils.PtrString(organization.ContainerId))
104+
table.AddSeparator()
105+
table.AddRow("STATUS", utils.PtrString(organization.LifecycleState))
106+
table.AddSeparator()
107+
table.AddRow("CREATION TIME", utils.PtrString(organization.CreationTime))
108+
table.AddSeparator()
109+
table.AddRow("UPDATE TIME", utils.PtrString(organization.UpdateTime))
110+
table.AddSeparator()
111+
table.AddRow("LABELS", utils.JoinStringMap(utils.PtrValue(organization.Labels), ": ", ", "))
112+
113+
err := table.Display(p)
114+
if err != nil {
115+
return fmt.Errorf("render table: %w", err)
116+
}
117+
return nil
118+
})
119+
}

0 commit comments

Comments
 (0)