-
Notifications
You must be signed in to change notification settings - Fork 4
/
mock_route53_helper.go
127 lines (109 loc) · 4.62 KB
/
mock_route53_helper.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
package goacm
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/aws/aws-sdk-go-v2/service/route53/types"
)
// NewMockRoute53API returns MockRoute53API.
func NewMockRoute53API(mockParams []MockRoute53Params) MockRoute53API {
return MockRoute53API{
ListHostedZonesAPI: NewMockListHostedZonesAPI(mockParams),
ListResourceRecordSetsAPI: NewMockListResourceRecordSetsAPI(mockParams),
ChangeResourceRecordSetsAPI: NewMockChangeResourceRecordSetsAPI(mockParams),
}
}
// NewMockListHostedZonesAPI returns MockListHostedZonesAPI.
func NewMockListHostedZonesAPI(mockParams []MockRoute53Params) MockListHostedZonesAPI {
return MockListHostedZonesAPI(func(ctx context.Context, params *route53.ListHostedZonesInput, optFns ...func(*route53.Options)) (*route53.ListHostedZonesOutput, error) {
out := route53.ListHostedZonesOutput{
HostedZones: []types.HostedZone{},
}
for _, p := range mockParams {
out.HostedZones = append(out.HostedZones, types.HostedZone{
Id: aws.String(strings.Replace(p.RecordSet.HostedDomainName, ".", "-", -1)),
Name: aws.String(p.RecordSet.HostedDomainName + "."),
Config: &types.HostedZoneConfig{
PrivateZone: p.IsPrivateHostedZone,
},
})
}
return &out, nil
})
}
// NewMockListResourceRecordSetsAPI returns MockListResourceRecordSetsAPI.
func NewMockListResourceRecordSetsAPI(mockParams []MockRoute53Params) MockListResourceRecordSetsAPI {
return MockListResourceRecordSetsAPI(func(ctx context.Context, params *route53.ListResourceRecordSetsInput, optFns ...func(*route53.Options)) (*route53.ListResourceRecordSetsOutput, error) {
out := route53.ListResourceRecordSetsOutput{}
available := map[string]*types.ResourceRecordSet{}
for _, p := range mockParams {
available[p.RecordSet.Name] = &types.ResourceRecordSet{
Name: aws.String(p.RecordSet.Name),
TTL: aws.Int64(p.RecordSet.TTL),
Type: types.RRTypeCname,
ResourceRecords: []types.ResourceRecord{
{
Value: aws.String(p.RecordSet.Value),
},
},
}
}
rrs := available[aws.ToString(params.StartRecordName)]
if rrs == nil {
return nil, fmt.Errorf("resource record sets not exists name: %s", *params.StartRecordName)
}
out.ResourceRecordSets = append(out.ResourceRecordSets, *rrs)
return &out, nil
})
}
// NewMockChangeResourceRecordSetsAPI returns MockChangeResourceRecordSetsAPI.
func NewMockChangeResourceRecordSetsAPI(mockParams []MockRoute53Params) MockChangeResourceRecordSetsAPI {
return MockChangeResourceRecordSetsAPI(func(ctx context.Context, params *route53.ChangeResourceRecordSetsInput, optFns ...func(*route53.Options)) (*route53.ChangeResourceRecordSetsOutput, error) {
out := route53.ChangeResourceRecordSetsOutput{}
available := map[string]*types.ChangeBatch{}
for _, p := range mockParams {
hostedZoneID := strings.Replace(p.RecordSet.HostedDomainName, ".", "-", -1)
available[hostedZoneID] = &types.ChangeBatch{
Changes: []types.Change{
{
Action: p.ChangeAction,
ResourceRecordSet: &types.ResourceRecordSet{
Name: aws.String(p.RecordSet.Name),
Type: types.RRType(p.RecordSet.Type),
ResourceRecords: []types.ResourceRecord{
{
Value: aws.String(p.RecordSet.Value),
},
},
},
},
},
}
}
cb := available[*params.HostedZoneId]
if cb == nil {
return nil, fmt.Errorf("hosted zone id not exists hostedzoneid: %s", *params.HostedZoneId)
}
if cb.Changes[0].Action != params.ChangeBatch.Changes[0].Action {
return nil, fmt.Errorf("change action not matches: expected %v but %v", cb.Changes[0].Action, params.ChangeBatch.Changes[0].Action)
}
if *cb.Changes[0].ResourceRecordSet.Name != *params.ChangeBatch.Changes[0].ResourceRecordSet.Name {
return nil, fmt.Errorf("record set name not mathces: expected %v but %v",
*cb.Changes[0].ResourceRecordSet.Name,
*params.ChangeBatch.Changes[0].ResourceRecordSet.Name)
}
if cb.Changes[0].ResourceRecordSet.Type != params.ChangeBatch.Changes[0].ResourceRecordSet.Type {
return nil, fmt.Errorf("record set type not mathces: expected %v but %v",
cb.Changes[0].ResourceRecordSet.Type,
params.ChangeBatch.Changes[0].ResourceRecordSet.Type)
}
if *cb.Changes[0].ResourceRecordSet.ResourceRecords[0].Value != *params.ChangeBatch.Changes[0].ResourceRecordSet.ResourceRecords[0].Value {
return nil, fmt.Errorf("record set resource record value not mathces: expected %v but %v",
*cb.Changes[0].ResourceRecordSet.ResourceRecords[0].Value,
*params.ChangeBatch.Changes[0].ResourceRecordSet.ResourceRecords[0].Value)
}
return &out, nil
})
}