-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from datum-cloud/feature/org-list
Support listing organizations
- Loading branch information
Showing
8 changed files
with
278 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package organizations | ||
|
||
import ( | ||
"fmt" | ||
|
||
resourcemanagerv1alpha "buf.build/gen/go/datum-cloud/datum-os/protocolbuffers/go/datum/os/resourcemanager/v1alpha" | ||
"buf.build/go/protoyaml" | ||
"github.com/rodaine/table" | ||
"github.com/spf13/cobra" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
|
||
"go.datum.net/datumctl/internal/keyring" | ||
"go.datum.net/datumctl/internal/resourcemanager" | ||
) | ||
|
||
func listOrgsCommand() *cobra.Command { | ||
var hostname, outputFormat string | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "List organizations for the authenticated user", | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
token, err := keyring.Get("datumctl", "datumctl") | ||
if err != nil { | ||
return fmt.Errorf("failed to get token from keyring: %w", err) | ||
} | ||
|
||
organizationsAPI := &resourcemanager.OrganizationsAPI{ | ||
PAT: token, | ||
Hostname: hostname, | ||
} | ||
|
||
listOrgs, err := organizationsAPI.ListOrganizations(cmd.Context(), &resourcemanagerv1alpha.ListOrganizationsRequest{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to list organizations: %w", err) | ||
} | ||
|
||
// TODO: We should look at abstracting the formatting here into a library | ||
// that can be used by multiple commands needing to offer multiple | ||
// output formats from a command. | ||
switch outputFormat { | ||
case "yaml": | ||
marshaller := &protoyaml.MarshalOptions{ | ||
Indent: 2, | ||
} | ||
output, err := marshaller.Marshal(listOrgs) | ||
if err != nil { | ||
return fmt.Errorf("failed to list organizations: %w", err) | ||
} | ||
fmt.Print(string(output)) | ||
case "json": | ||
output, err := protojson.Marshal(listOrgs) | ||
if err != nil { | ||
return fmt.Errorf("failed to list organizations: %w", err) | ||
} | ||
fmt.Print(string(output)) | ||
case "table": | ||
orgTable := table.New("DISPLAY NAME", "RESOURCE ID") | ||
if len(listOrgs.Organizations) == 0 { | ||
fmt.Printf("No organizations found") | ||
} else { | ||
for _, org := range listOrgs.Organizations { | ||
orgTable.AddRow(org.DisplayName, org.OrganizationId) | ||
} | ||
} | ||
orgTable.Print() | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&hostname, "hostname", "api.datum.net", "The hostname of the Datum Cloud instance to authenticate with") | ||
cmd.Flags().StringVar(&outputFormat, "output", "table", "Specify the output format to use. Supported options: table, json, yaml") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package organizations | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "organizations", | ||
Short: "Manage organizations", | ||
} | ||
|
||
cmd.AddCommand( | ||
listOrgsCommand(), | ||
) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.