Skip to content

Commit 02f49cc

Browse files
committed
feat(dns): Add flush command
STACKITCLI-453 Signed-off-by: Alexander Dahmen <[email protected]>
1 parent bc4b561 commit 02f49cc

7 files changed

Lines changed: 415 additions & 3 deletions

File tree

‎docs/stackit_dns.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ stackit dns [flags]
3030
### SEE ALSO
3131

3232
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
33+
* [stackit dns flush](./stackit_dns_flush.md) - Flushes the cache for a domain on the STACKIT DNS resolvers
3334
* [stackit dns record-set](./stackit_dns_record-set.md) - Provides functionality for DNS record set
3435
* [stackit dns zone](./stackit_dns_zone.md) - Provides functionality for DNS zones
3536

‎docs/stackit_dns_flush.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## stackit dns flush
2+
3+
Flushes the cache for a domain on the STACKIT DNS resolvers
4+
5+
### Synopsis
6+
7+
Flushes the cache for a domain on the STACKIT DNS resolvers.
8+
9+
```
10+
stackit dns flush DOMAIN [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Flush the cache for domain "www.example.com"
17+
$ stackit dns flush www.example.com
18+
19+
Flush the cache for the A records of domain "www.example.com"
20+
$ stackit dns flush www.example.com --type A
21+
```
22+
23+
### Options
24+
25+
```
26+
-h, --help Help for "stackit dns flush"
27+
--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])
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 dns](./stackit_dns.md) - Provides functionality for DNS
44+

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.16.0
2222
github.com/stackitcloud/stackit-sdk-go/services/automation v0.2.1
2323
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.22.0
24-
github.com/stackitcloud/stackit-sdk-go/services/dns v0.25.0
24+
github.com/stackitcloud/stackit-sdk-go/services/dns v0.26.0
2525
github.com/stackitcloud/stackit-sdk-go/services/edge v0.14.0
2626
github.com/stackitcloud/stackit-sdk-go/services/git v0.16.0
2727
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.15.1

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,8 @@ github.com/stackitcloud/stackit-sdk-go/services/automation v0.2.1 h1:VxYQ4fCkuZA
620620
github.com/stackitcloud/stackit-sdk-go/services/automation v0.2.1/go.mod h1:pDhpbmeDlthML6h4jyI7ZRGrG2C4Tx3aIGru5Ts6ISo=
621621
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.22.0 h1:NyG5PvXB9+ldlKqbTfpp4YMb/Afg8LRDG5BIa+mBf+g=
622622
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.22.0/go.mod h1:sr7UP8YdI18bH+2mfpy9qjsDqcPQU/+YduqH1Zw2JgY=
623-
github.com/stackitcloud/stackit-sdk-go/services/dns v0.25.0 h1:Yn4etadrdEEPU8YcbvF7a/UExSqri+jUC62w62ic4N8=
624-
github.com/stackitcloud/stackit-sdk-go/services/dns v0.25.0/go.mod h1:D0iv6XBYkaEwTjbBeom0VTZnwQMfwb48FC8R2jRg80c=
623+
github.com/stackitcloud/stackit-sdk-go/services/dns v0.26.0 h1:WFOUn8EnbMvQC8tuEyB65sVLzA6mXsNTEWOVnQhOoYY=
624+
github.com/stackitcloud/stackit-sdk-go/services/dns v0.26.0/go.mod h1:D0iv6XBYkaEwTjbBeom0VTZnwQMfwb48FC8R2jRg80c=
625625
github.com/stackitcloud/stackit-sdk-go/services/edge v0.14.0 h1:TTwlI34FUb0AisiApxKs5brGNP3Ma9cTvdmbxzNxs40=
626626
github.com/stackitcloud/stackit-sdk-go/services/edge v0.14.0/go.mod h1:FBBnDsQns6SjJYQrox3YSY7ydOa4T8FR3sXNwFRTsPc=
627627
github.com/stackitcloud/stackit-sdk-go/services/git v0.16.0 h1:M75DCyPbTzTx4mPdIkBvOVE3/6Eda0rp8xLm40d02qc=

‎internal/cmd/dns/dns.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dns
22

33
import (
4+
"github.com/stackitcloud/stackit-cli/internal/cmd/dns/flush"
45
recordset "github.com/stackitcloud/stackit-cli/internal/cmd/dns/record-set"
56
"github.com/stackitcloud/stackit-cli/internal/cmd/dns/zone"
67
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -25,4 +26,5 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
2526
func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
2627
cmd.AddCommand(zone.NewCmd(params))
2728
cmd.AddCommand(recordset.NewCmd(params))
29+
cmd.AddCommand(flush.NewCmd(params))
2830
}

‎internal/cmd/dns/flush/flush.go‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package flush
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8+
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
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/dns/client"
15+
16+
"github.com/spf13/cobra"
17+
dns "github.com/stackitcloud/stackit-sdk-go/services/dns/v1api"
18+
)
19+
20+
const (
21+
domainArg = "DOMAIN"
22+
)
23+
24+
var typeFlag = flags.StringEnumFlag(
25+
"type",
26+
dns.AllowedFlushDomainPayloadTypeEnumValues,
27+
"Record type to flush. If not set, the cache is flushed for all record types of the domain",
28+
)
29+
30+
type inputModel struct {
31+
*globalflags.GlobalFlagModel
32+
Domain string
33+
Type *dns.FlushDomainPayloadType
34+
}
35+
36+
func NewCmd(params *types.CmdParams) *cobra.Command {
37+
cmd := &cobra.Command{
38+
Use: fmt.Sprintf("flush %s", domainArg),
39+
Short: "Flushes the cache for a domain on the STACKIT DNS resolvers",
40+
Long: "Flushes the cache for a domain on the STACKIT DNS resolvers.",
41+
Args: args.SingleArg(domainArg, nil),
42+
Example: examples.Build(
43+
examples.NewExample(
44+
`Flush the cache for domain "www.example.com"`,
45+
"$ stackit dns flush www.example.com"),
46+
examples.NewExample(
47+
`Flush the cache for the A records of domain "www.example.com"`,
48+
"$ stackit dns flush www.example.com --type A"),
49+
),
50+
RunE: func(cmd *cobra.Command, args []string) error {
51+
ctx := context.Background()
52+
model, err := parseInput(params.Printer, cmd, args)
53+
if err != nil {
54+
return err
55+
}
56+
57+
// Configure API client
58+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
59+
if err != nil {
60+
return err
61+
}
62+
63+
prompt := fmt.Sprintf("Are you sure you want to flush the cache for domain %q?", model.Domain)
64+
err = params.Printer.PromptForConfirmation(prompt)
65+
if err != nil {
66+
return err
67+
}
68+
69+
// Call API
70+
req := buildRequest(ctx, model, apiClient)
71+
resp, err := req.Execute()
72+
if err != nil {
73+
return fmt.Errorf("flush DNS cache: %w", err)
74+
}
75+
76+
return outputResult(params.Printer, model, resp)
77+
},
78+
}
79+
configureFlags(cmd)
80+
return cmd
81+
}
82+
83+
func configureFlags(cmd *cobra.Command) {
84+
typeFlag.Register(cmd.Flags())
85+
}
86+
87+
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
88+
domain := inputArgs[0]
89+
globalFlags := globalflags.Parse(p, cmd)
90+
91+
model := inputModel{
92+
GlobalFlagModel: globalFlags,
93+
Domain: domain,
94+
Type: typeFlag.Ptr(),
95+
}
96+
97+
p.DebugInputModel(model)
98+
return &model, nil
99+
}
100+
101+
func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClient) dns.ApiFlushDomainRequest {
102+
req := apiClient.DefaultAPI.FlushDomain(ctx)
103+
req = req.FlushDomainPayload(dns.FlushDomainPayload{
104+
Domain: model.Domain,
105+
Type: model.Type,
106+
})
107+
return req
108+
}
109+
110+
func outputResult(p *print.Printer, model *inputModel, resp *dns.FlushDomainResponse) error {
111+
return p.OutputResult(model.OutputFormat, resp, func() error {
112+
if resp == nil {
113+
return fmt.Errorf("flush DNS cache response is empty")
114+
}
115+
if model.Type != nil {
116+
p.Outputf("Triggered flush of the %s record cache for domain %q\n", *model.Type, model.Domain)
117+
} else {
118+
p.Outputf("Triggered flush of the cache for domain %q\n", model.Domain)
119+
}
120+
return nil
121+
})
122+
}

0 commit comments

Comments
 (0)