-
Notifications
You must be signed in to change notification settings - Fork 28
feat(cdn): add cdn client, config, list command #1100
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
Open
cgoetz-inovex
wants to merge
8
commits into
main
Choose a base branch
from
feat/STACKITCLI-70-add-cdn-distribution-beta-cmds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
863828c
feat(cdn): add cdn client, config, list command
cgoetz-inovex f198a95
fix(cdn): generate docs
cgoetz-inovex d5f4304
fix(cdn) fix linting issues
cgoetz-inovex 8c71293
Update internal/cmd/beta/cdn/distribution/list/list.go
cgoetz-inovex 82f7d49
Update internal/cmd/beta/cdn/distribution/list/list.go
cgoetz-inovex a87cd60
fix(cdn): rename sortBy params, use EnumSliceToStringSlice
cgoetz-inovex 42d1196
fix(cdn): make `testNextPageID`, `testID` and `testStatus` constant
cgoetz-inovex 0d7729a
fix(cdn): add cdn subcommand to beta, generate docs
cgoetz-inovex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,34 @@ | ||
| ## stackit beta cdn | ||
|
|
||
| Manage CDN resources | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Manage the lifecycle of CDN resources. | ||
|
|
||
| ``` | ||
| stackit beta cdn [flags] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit beta cdn" | ||
| ``` | ||
|
|
||
| ### 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" "yaml"] | ||
| -p, --project-id string Project ID | ||
| --region string Target region for region-specific requests | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit beta](./stackit_beta.md) - Contains beta STACKIT CLI commands | ||
| * [stackit beta cdn distribution](./stackit_beta_cdn_distribution.md) - Manage CDN distributions | ||
|
|
This file contains hidden or 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,34 @@ | ||
| ## stackit beta cdn distribution | ||
|
|
||
| Manage CDN distributions | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Manage the lifecycle of CDN distributions. | ||
|
|
||
| ``` | ||
| stackit beta cdn distribution [flags] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit beta cdn distribution" | ||
| ``` | ||
|
|
||
| ### 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" "yaml"] | ||
| -p, --project-id string Project ID | ||
| --region string Target region for region-specific requests | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit beta cdn](./stackit_beta_cdn.md) - Manage CDN resources | ||
| * [stackit beta cdn distribution list](./stackit_beta_cdn_distribution_list.md) - List CDN distributions | ||
|
|
This file contains hidden or 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,44 @@ | ||
| ## stackit beta cdn distribution list | ||
|
|
||
| List CDN distributions | ||
|
|
||
| ### Synopsis | ||
|
|
||
| List all CDN distributions in your account. | ||
|
|
||
| ``` | ||
| stackit beta cdn distribution list [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| List all CDN distributions | ||
| $ stackit beta cdn distribution list | ||
|
|
||
| List all CDN distributions sorted by id | ||
| $ stackit beta cdn distribution list --sort-by=id | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit beta cdn distribution list" | ||
| --sort-by string Sort entries by a specific field, one of ["id" "createdAt" "updatedAt" "originUrl" "status" "originUrlRelated"] (default "createdAt") | ||
| ``` | ||
|
|
||
| ### 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" "yaml"] | ||
| -p, --project-id string Project ID | ||
| --region string Target region for region-specific requests | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit beta cdn distribution](./stackit_beta_cdn_distribution.md) - Manage CDN distributions | ||
|
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| package cdn | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| "github.com/stackitcloud/stackit-cli/internal/cmd/beta/cdn/distribution" | ||
| "github.com/stackitcloud/stackit-cli/internal/cmd/params" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/utils" | ||
| ) | ||
|
|
||
| func NewCmd(params *params.CmdParams) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "cdn", | ||
| Short: "Manage CDN resources", | ||
| Long: "Manage the lifecycle of CDN resources.", | ||
| Args: args.NoArgs, | ||
| Run: utils.CmdHelp, | ||
| } | ||
| addSubcommands(cmd, params) | ||
| return cmd | ||
| } | ||
|
|
||
| func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { | ||
| cmd.AddCommand(distribution.NewCommand(params)) | ||
| } |
This file contains hidden or 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,24 @@ | ||
| package distribution | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| "github.com/stackitcloud/stackit-cli/internal/cmd/beta/cdn/distribution/list" | ||
| "github.com/stackitcloud/stackit-cli/internal/cmd/params" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/utils" | ||
| ) | ||
|
|
||
| func NewCommand(params *params.CmdParams) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "distribution", | ||
| Short: "Manage CDN distributions", | ||
| Long: "Manage the lifecycle of CDN distributions.", | ||
| Args: cobra.NoArgs, | ||
| Run: utils.CmdHelp, | ||
| } | ||
| addSubcommands(cmd, params) | ||
| return cmd | ||
| } | ||
|
|
||
| func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { | ||
| cmd.AddCommand(list.NewCmd(params)) | ||
| } |
This file contains hidden or 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,153 @@ | ||
| package list | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackitcloud/stackit-cli/internal/cmd/params" | ||
| "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/print" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/services/cdn/client" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/tables" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/utils" | ||
| sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/cdn" | ||
| ) | ||
|
|
||
| type inputModel struct { | ||
| *globalflags.GlobalFlagModel | ||
| SortBy string | ||
| } | ||
|
|
||
| const ( | ||
| sortByFlag = "sort-by" | ||
| ) | ||
|
|
||
| func NewCmd(params *params.CmdParams) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List CDN distributions", | ||
| Long: "List all CDN distributions in your account.", | ||
| Args: args.NoArgs, | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `List all CDN distributions`, | ||
| `$ stackit beta cdn distribution list`, | ||
| ), | ||
| examples.NewExample( | ||
| `List all CDN distributions sorted by id`, | ||
| `$ stackit beta cdn distribution list --sort-by=id`, | ||
| ), | ||
| ), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := context.Background() // should this be cancellable? | ||
|
|
||
| model, err := parseInput(params.Printer, cmd, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| distributions, err := fetchDistributions(ctx, model, apiClient) | ||
| if err != nil { | ||
| return fmt.Errorf("fetch distributions: %w", err) | ||
| } | ||
|
|
||
| return outputResult(params.Printer, model.OutputFormat, distributions) | ||
| }, | ||
| } | ||
|
|
||
| configureFlags(cmd) | ||
| return cmd | ||
| } | ||
|
|
||
| var sortByFlagOptions = []string{"id", "createdAt", "updatedAt", "originUrl", "status", "originUrlRelated"} | ||
|
|
||
| func configureFlags(cmd *cobra.Command) { | ||
| // same default as apiClient | ||
| cmd.Flags().Var(flags.EnumFlag(false, "createdAt", sortByFlagOptions...), sortByFlag, fmt.Sprintf("Sort entries by a specific field, one of %q", sortByFlagOptions)) | ||
| } | ||
|
|
||
| func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) { | ||
| globalFlags := globalflags.Parse(p, cmd) | ||
| if globalFlags.ProjectId == "" { | ||
| return nil, &errors.ProjectIdError{} | ||
| } | ||
|
|
||
| model := inputModel{ | ||
| GlobalFlagModel: globalFlags, | ||
| SortBy: flags.FlagWithDefaultToStringValue(p, cmd, sortByFlag), | ||
| } | ||
|
|
||
| p.DebugInputModel(model) | ||
| return &model, nil | ||
| } | ||
|
|
||
| func buildRequest(ctx context.Context, model *inputModel, apiClient *cdn.APIClient, nextPageID cdn.ListDistributionsResponseGetNextPageIdentifierAttributeType) cdn.ApiListDistributionsRequest { | ||
| req := apiClient.ListDistributions(ctx, model.GlobalFlagModel.ProjectId) | ||
| req = req.SortBy(model.SortBy) | ||
| req = req.PageSize(100) | ||
| if nextPageID != nil { | ||
| req = req.PageIdentifier(*nextPageID) | ||
| } | ||
| return req | ||
| } | ||
|
|
||
| func outputResult(p *print.Printer, outputFormat string, distributions []cdn.Distribution) error { | ||
| if distributions == nil { | ||
| distributions = make([]cdn.Distribution, 0) // otherwise prints null in json output | ||
| } | ||
| return p.OutputResult(outputFormat, distributions, func() error { | ||
| if len(distributions) == 0 { | ||
| p.Outputln("No CDN distributions found") | ||
| return nil | ||
| } | ||
|
|
||
| table := tables.NewTable() | ||
| table.SetHeader("ID", "REGIONS", "STATUS") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rubenhoenle, when using the |
||
| for i := range distributions { | ||
| d := &distributions[i] | ||
| joinedRegions := strings.Join(sdkUtils.EnumSliceToStringSlice(*d.Config.Regions), ", ") | ||
| table.AddRow( | ||
| utils.PtrString(d.Id), | ||
| joinedRegions, | ||
cgoetz-inovex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| utils.PtrString(d.Status), | ||
| ) | ||
| } | ||
| err := table.Display(p) | ||
| if err != nil { | ||
| return fmt.Errorf("render table: %w", err) | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| func fetchDistributions(ctx context.Context, model *inputModel, apiClient *cdn.APIClient) ([]cdn.Distribution, error) { | ||
| var nextPageID cdn.ListDistributionsResponseGetNextPageIdentifierAttributeType | ||
| var distributions []cdn.Distribution | ||
| for { | ||
| request := buildRequest(ctx, model, apiClient, nextPageID) | ||
| response, err := request.Execute() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("list distributions: %w", err) | ||
| } | ||
| nextPageID = response.NextPageIdentifier | ||
| if response.Distributions != nil { | ||
| distributions = append(distributions, *response.Distributions...) | ||
| } | ||
| if nextPageID == nil { | ||
| break | ||
| } | ||
| } | ||
| return distributions, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good catch indeed! Any chance we can solve this in a central place for all commands?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea, I'll investigate this a bit, don't know much about go reflection yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reflection is considered a no-no, see https://principles.schwarz/principles/engineering-principles/go/avoid-using-reflection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With reflection I'd add this in
Printer.OutputResult():The linked document suggests type switches, which would not work here.
Another solution without reflection, that comes to mind, would be to introduce specialized
OutputResultfunctions, likeOutputResultSlice/Map.