Skip to content

Commit 347472f

Browse files
Monitobojamesbquantumsheep
authored
feat(domain): add helper check if record exist (#1789)
Co-authored-by: jbernabe <[email protected]> Co-authored-by: Nathanael Demacon <[email protected]>
1 parent 36a2219 commit 347472f

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

api/domain/v2beta1/domain_utils.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const (
1818
// ErrCodeNoSuchDNSZone for service response error code
1919
//
2020
// The specified dns zone does not exist.
21-
ErrCodeNoSuchDNSZone = "NoSuchDNSZone"
21+
ErrCodeNoSuchDNSZone = "NoSuchDNSZone"
22+
ErrCodeNoSuchDNSRecord = "NoSuchDNSRecord"
2223
)
2324

2425
// WaitForDNSZoneRequest is used by WaitForDNSZone method.
@@ -79,3 +80,57 @@ func (s *API) WaitForDNSZone(
7980

8081
return dns.(*DNSZone), nil
8182
}
83+
84+
// WaitForDNSRecordExistRequest is used by WaitForDNSRecordExist method.
85+
type WaitForDNSRecordExistRequest struct {
86+
DNSZone string
87+
RecordName string
88+
RecordType RecordType
89+
Timeout *time.Duration
90+
RetryInterval *time.Duration
91+
}
92+
93+
func (s *API) WaitForDNSRecordExist(
94+
req *WaitForDNSRecordExistRequest,
95+
opts ...scw.RequestOption,
96+
) (*Record, error) {
97+
timeout := defaultTimeout
98+
if req.Timeout != nil {
99+
timeout = *req.Timeout
100+
}
101+
retryInterval := defaultRetryInterval
102+
if req.RetryInterval != nil {
103+
retryInterval = *req.RetryInterval
104+
}
105+
106+
dns, err := async.WaitSync(&async.WaitSyncConfig{
107+
Get: func() (interface{}, bool, error) {
108+
// listing dns zone records and take the first one
109+
DNSRecords, err := s.ListDNSZoneRecords(&ListDNSZoneRecordsRequest{
110+
Name: req.RecordName,
111+
Type: req.RecordType,
112+
DNSZone: req.DNSZone,
113+
}, opts...)
114+
115+
if err != nil {
116+
return nil, false, err
117+
}
118+
119+
if DNSRecords.TotalCount == 0 {
120+
return nil, false, fmt.Errorf(ErrCodeNoSuchDNSRecord)
121+
}
122+
123+
record := DNSRecords.Records[0]
124+
125+
return record, true, nil
126+
},
127+
Timeout: timeout,
128+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
129+
})
130+
131+
if err != nil {
132+
return nil, errors.Wrap(err, "check for DNS Record exist failed")
133+
}
134+
135+
return dns.(*Record), nil
136+
}

0 commit comments

Comments
 (0)