Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/stackit_dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ stackit dns [flags]
### SEE ALSO

* [stackit](./stackit.md) - Manage STACKIT resources using the command line
* [stackit dns flush](./stackit_dns_flush.md) - Flushes the cache for a domain on the STACKIT DNS resolvers
* [stackit dns lookup](./stackit_dns_lookup.md) - Looks up a domain on the STACKIT DNS resolvers
* [stackit dns record-set](./stackit_dns_record-set.md) - Provides functionality for DNS record set
* [stackit dns zone](./stackit_dns_zone.md) - Provides functionality for DNS zones
Expand Down
44 changes: 44 additions & 0 deletions docs/stackit_dns_flush.md
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

2 changes: 2 additions & 0 deletions internal/cmd/dns/dns.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dns

import (
"github.com/stackitcloud/stackit-cli/internal/cmd/dns/flush"
"github.com/stackitcloud/stackit-cli/internal/cmd/dns/lookup"
recordset "github.com/stackitcloud/stackit-cli/internal/cmd/dns/record-set"
"github.com/stackitcloud/stackit-cli/internal/cmd/dns/zone"
Expand All @@ -26,5 +27,6 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
cmd.AddCommand(zone.NewCmd(params))
cmd.AddCommand(recordset.NewCmd(params))
cmd.AddCommand(flush.NewCmd(params))
cmd.AddCommand(lookup.NewCmd(params))
}
122 changes: 122 additions & 0 deletions internal/cmd/dns/flush/flush.go
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",
)
Comment thread
GokceGK marked this conversation as resolved.
Resolved

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
})
}
Loading
Loading