-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
api_zones.go
128 lines (120 loc) · 3.39 KB
/
api_zones.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package dns
import (
"context"
"errors"
"strings"
"beryju.io/gravity/pkg/roles/dns/types"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
)
type APIZonesGetInput struct {
Name string `query:"name" description:"Optionally get DNS Zone by name"`
}
type APIZone struct {
Name string `json:"name" required:"true"`
HandlerConfigs []map[string]string `json:"handlerConfigs" required:"true"`
DefaultTTL uint32 `json:"defaultTTL" required:"true"`
Authoritative bool `json:"authoritative" required:"true"`
}
type APIZonesGetOutput struct {
Zones []APIZone `json:"zones" required:"true"`
}
func (r *Role) APIZonesGet() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input APIZonesGetInput, output *APIZonesGetOutput) error {
key := r.i.KV().Key(
types.KeyRole,
types.KeyZones,
)
if input.Name == "" {
key = key.Prefix(true)
} else {
key = key.Add(input.Name)
}
rawZones, err := r.i.KV().Get(
ctx,
key.String(),
clientv3.WithPrefix(),
)
if err != nil {
r.log.Warn("failed to get zones", zap.Error(err))
return status.Wrap(errors.New("failed to get zones"), status.Internal)
}
for _, rawZone := range rawZones.Kvs {
_zone, err := r.zoneFromKV(rawZone)
if err != nil {
r.log.Warn("failed to parse zone", zap.Error(err))
continue
}
if strings.Contains(_zone.Name, "/") {
continue
}
output.Zones = append(output.Zones, APIZone{
Name: _zone.Name,
Authoritative: _zone.Authoritative,
DefaultTTL: _zone.DefaultTTL,
HandlerConfigs: _zone.HandlerConfigs,
})
}
return nil
})
u.SetName("dns.get_zones")
u.SetTitle("DNS Zones")
u.SetTags("roles/dns")
u.SetExpectedErrors(status.Internal)
return u
}
type APIZonesPutInput struct {
Name string `query:"zone" required:"true" maxLength:"255"`
HandlerConfigs []map[string]string `json:"handlerConfigs" required:"true"`
DefaultTTL uint32 `json:"defaultTTL" required:"true"`
Authoritative bool `json:"authoritative" required:"true"`
}
func (r *Role) APIZonesPut() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input APIZonesPutInput, output *struct{}) error {
z := r.newZone(input.Name)
z.Name = input.Name
if !strings.HasSuffix(z.Name, ".") {
z.Name += "."
}
z.Authoritative = input.Authoritative
z.HandlerConfigs = input.HandlerConfigs
z.DefaultTTL = input.DefaultTTL
err := z.put(ctx)
if err != nil {
return status.Wrap(err, status.Internal)
}
return nil
})
u.SetName("dns.put_zones")
u.SetTitle("DNS Zones")
u.SetTags("roles/dns")
u.SetExpectedErrors(status.Internal, status.InvalidArgument)
return u
}
type APIZonesDeleteInput struct {
Zone string `query:"zone"`
}
func (r *Role) APIZonesDelete() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input APIZonesDeleteInput, output *struct{}) error {
_, err := r.i.KV().Delete(
ctx,
r.i.KV().Key(
types.KeyRole,
types.KeyZones,
input.Zone,
).String(),
clientv3.WithPrefix(),
)
if err != nil {
return status.Wrap(err, status.Internal)
}
return nil
})
u.SetName("dns.delete_zones")
u.SetTitle("DNS Zones")
u.SetTags("roles/dns")
u.SetExpectedErrors(status.Internal, status.InvalidArgument)
return u
}