forked from mweagle/Sparta
/
apigateway_domain.go
125 lines (115 loc) · 4.19 KB
/
apigateway_domain.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
package decorator
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws/session"
sparta "github.com/mweagle/Sparta"
spartaCF "github.com/mweagle/Sparta/aws/cloudformation"
gocf "github.com/mweagle/go-cloudformation"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
// APIGatewayMappingEntry is the keyname used to store the API Gateway mappings
APIGatewayMappingEntry = "APIGatewayMappings"
)
// APIGatewayDomainDecorator returns a ServiceDecoratorHookHandler
// implementation that registers a custom domain for an API Gateway
// service
func APIGatewayDomainDecorator(apiGateway *sparta.API,
acmCertARN gocf.Stringable,
basePath string,
domainName string) sparta.ServiceDecoratorHookHandler {
// Attach the domain decorator to the API GW instance
domainDecorator := func(context map[string]interface{},
serviceName string,
template *gocf.Template,
S3Bucket string,
S3Key string,
buildID string,
awsSession *session.Session,
noop bool,
logger *logrus.Logger) error {
domainParts := strings.Split(domainName, ".")
if len(domainParts) != 3 {
return errors.Errorf("Invalid domain name supplied to APIGatewayDomainDecorator: %s",
domainName)
}
// Add the mapping
template.Mappings = map[string]*gocf.Mapping{
APIGatewayMappingEntry: spartaCF.APIGatewayMapping,
}
// Resource names
domainInfoResourceName := sparta.CloudFormationResourceName(apiGateway.LogicalResourceName(),
"Domain")
basePathMappingResourceName := sparta.CloudFormationResourceName(apiGateway.LogicalResourceName(), "BasePathMapping")
dnsRecordResourceName := sparta.CloudFormationResourceName(apiGateway.LogicalResourceName(),
"CloudFrontDNS")
// Then add all the resources
domainInfo := &gocf.APIGatewayDomainName{
DomainName: gocf.String(domainName),
}
apiGatewayType := ""
apiGWEndpointConfiguration := apiGateway.EndpointConfiguration
if apiGWEndpointConfiguration != nil && apiGWEndpointConfiguration.Types != nil {
typesList := apiGWEndpointConfiguration.Types
if len(typesList.Literal) == 1 {
apiGatewayType = typesList.Literal[0].Literal
} else {
return errors.Errorf("Invalid API GW types provided to decorator: %#v",
apiGWEndpointConfiguration.Types)
}
}
attrName := ""
switch apiGatewayType {
case "REGIONAL":
{
domainInfo.RegionalCertificateArn = acmCertARN.String()
domainInfo.EndpointConfiguration = &gocf.APIGatewayDomainNameEndpointConfiguration{
Types: gocf.StringList(gocf.String("REGIONAL")),
}
attrName = "RegionalDomainName"
}
case "EDGE":
{
domainInfo.CertificateArn = acmCertARN.String()
domainInfo.EndpointConfiguration = &gocf.APIGatewayDomainNameEndpointConfiguration{
Types: gocf.StringList(gocf.String("EDGE")),
}
attrName = "DistributionDomainName"
}
default:
return errors.Errorf("Unsupported API Gateway type: %#v", apiGatewayType)
}
template.AddResource(domainInfoResourceName, domainInfo)
basePathMapping := gocf.APIGatewayBasePathMapping{
BasePath: gocf.String(basePath),
DomainName: gocf.Ref(domainInfoResourceName).String(),
RestAPIID: gocf.Ref(apiGateway.LogicalResourceName()).String(),
}
mappingResource := template.AddResource(basePathMappingResourceName, basePathMapping)
mappingResource.DependsOn = []string{domainInfoResourceName,
apiGateway.LogicalResourceName()}
// Use the HostedZoneName to create the record
domainZone := domainParts[1:]
dnsRecordResource := &gocf.Route53RecordSet{
HostedZoneName: gocf.String(fmt.Sprintf("%s.", strings.Join(domainZone, "."))),
Name: gocf.String(fmt.Sprintf("%s.", domainName)),
Type: gocf.String("A"),
AliasTarget: &gocf.Route53RecordSetAliasTarget{
HostedZoneID: gocf.FindInMap(APIGatewayMappingEntry,
gocf.Ref("AWS::Region"),
gocf.String(spartaCF.HostedZoneID)),
DNSName: gocf.GetAtt(domainInfoResourceName, attrName).String(),
},
}
template.AddResource(dnsRecordResourceName, dnsRecordResource)
// Add an output...
template.Outputs["APIGatewayCustomDomain"] = &gocf.Output{
Description: "Custom API Gateway Domain",
Value: gocf.String(domainName),
}
return nil
}
return sparta.ServiceDecoratorHookFunc(domainDecorator)
}