Repository navigation
feat(dns): Add flush command #1731
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
Changes from all commits
Commits
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,44 @@ | ||
| ## stackit dns flush | ||
|
|
||
| Flushes the cache for a domain on the STACKIT DNS resolvers | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Flushes the cache for a domain on the STACKIT DNS resolvers. | ||
|
|
||
| ``` | ||
| stackit dns flush DOMAIN [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Flush the cache for domain "www.example.com" | ||
| $ stackit dns flush www.example.com | ||
|
|
||
| Flush the cache for the A records of domain "www.example.com" | ||
| $ stackit dns flush www.example.com --type A | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help[=true|false] Help for "stackit dns flush" | ||
| --type string Record type to flush. If not set, the cache is flushed for all record types of the domain (one of: [A, AAAA, SOA, CNAME, NS, MX, TXT, SRV, PTR, DNAME, CAA, DNSKEY, DS, LOC, NAPTR, SSHFP, TLSA, URI, CERT, SVCB, CSYNC, HINFO, HTTPS]) | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| -y, --assume-yes[=true|false] If set, skips all confirmation prompts | ||
| --async[=true|false] 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 dns](./stackit_dns.md) - Provides functionality for DNS | ||
|
|
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,122 @@ | ||
| package flush | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/stackitcloud/stackit-cli/internal/pkg/types" | ||
|
|
||
| "github.com/stackitcloud/stackit-cli/internal/pkg/args" | ||
| "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/dns/client" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| dns "github.com/stackitcloud/stackit-sdk-go/services/dns/v1api" | ||
| ) | ||
|
|
||
| const ( | ||
| domainArg = "DOMAIN" | ||
| ) | ||
|
|
||
| var typeFlag = flags.StringEnumFlag( | ||
| "type", | ||
| dns.AllowedFlushDomainPayloadTypeEnumValues, | ||
| "Record type to flush. If not set, the cache is flushed for all record types of the domain", | ||
| ) | ||
|
|
||
| type inputModel struct { | ||
| *globalflags.GlobalFlagModel | ||
| Domain string | ||
| Type *dns.FlushDomainPayloadType | ||
| } | ||
|
|
||
| func NewCmd(params *types.CmdParams) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: fmt.Sprintf("flush %s", domainArg), | ||
| Short: "Flushes the cache for a domain on the STACKIT DNS resolvers", | ||
| Long: "Flushes the cache for a domain on the STACKIT DNS resolvers.", | ||
| Args: args.SingleArg(domainArg, nil), | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `Flush the cache for domain "www.example.com"`, | ||
| "$ stackit dns flush www.example.com"), | ||
| examples.NewExample( | ||
| `Flush the cache for the A records of domain "www.example.com"`, | ||
| "$ stackit dns flush www.example.com --type A"), | ||
| ), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := context.Background() | ||
| model, err := parseInput(params.Printer, cmd, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Configure API client | ||
| apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| prompt := fmt.Sprintf("Are you sure you want to flush the cache for domain %q?", model.Domain) | ||
| err = params.Printer.PromptForConfirmation(prompt) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Call API | ||
| req := buildRequest(ctx, model, apiClient) | ||
| resp, err := req.Execute() | ||
| if err != nil { | ||
| return fmt.Errorf("flush DNS cache: %w", err) | ||
| } | ||
|
|
||
| return outputResult(params.Printer, model, resp) | ||
| }, | ||
| } | ||
| configureFlags(cmd) | ||
| return cmd | ||
| } | ||
|
|
||
| func configureFlags(cmd *cobra.Command) { | ||
| typeFlag.Register(cmd.Flags()) | ||
| } | ||
|
|
||
| func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { | ||
| domain := inputArgs[0] | ||
| globalFlags := globalflags.Parse(p, cmd) | ||
|
|
||
| model := inputModel{ | ||
| GlobalFlagModel: globalFlags, | ||
| Domain: domain, | ||
| Type: typeFlag.Ptr(), | ||
| } | ||
|
|
||
| p.DebugInputModel(model) | ||
| return &model, nil | ||
| } | ||
|
|
||
| func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClient) dns.ApiFlushDomainRequest { | ||
| req := apiClient.DefaultAPI.FlushDomain(ctx) | ||
| req = req.FlushDomainPayload(dns.FlushDomainPayload{ | ||
| Domain: model.Domain, | ||
| Type: model.Type, | ||
| }) | ||
| return req | ||
| } | ||
|
|
||
| func outputResult(p *print.Printer, model *inputModel, resp *dns.FlushDomainResponse) error { | ||
| return p.OutputResult(model.OutputFormat, resp, func() error { | ||
| if resp == nil { | ||
| return fmt.Errorf("flush DNS cache response is empty") | ||
| } | ||
| if model.Type != nil { | ||
| p.Outputf("Triggered flush of the %s record cache for domain %q\n", *model.Type, model.Domain) | ||
| } else { | ||
| p.Outputf("Triggered flush of the cache for domain %q\n", model.Domain) | ||
| } | ||
| return 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.
Uh oh!
There was an error while loading. Please reload this page.