forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
113 lines (100 loc) · 3.38 KB
/
main.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
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.
package main
import (
"fmt"
"strings"
"github.com/pulumi/pulumi-azure-native-sdk/cdn"
"github.com/pulumi/pulumi-azure-native-sdk/resources"
"github.com/pulumi/pulumi-azure-native-sdk/storage"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
resourceGroup, err := resources.NewResourceGroup(ctx, "website-rg", nil)
if err != nil {
return err
}
profile, err := cdn.NewProfile(ctx, "profile", &cdn.ProfileArgs{
ResourceGroupName: resourceGroup.Name,
Sku: &cdn.SkuArgs{
Name: pulumi.String(cdn.SkuName_Standard_Microsoft),
},
})
if err != nil {
return err
}
storageAccount, err := storage.NewStorageAccount(ctx, "sa", &storage.StorageAccountArgs{
ResourceGroupName: resourceGroup.Name,
Kind: pulumi.String(storage.KindStorageV2),
Sku: &storage.SkuArgs{
Name: pulumi.String(storage.SkuName_Standard_LRS),
},
})
if err != nil {
return err
}
endpointOrigin := storageAccount.PrimaryEndpoints.Web().ApplyT(func(endpoint string) string {
endpoint = strings.ReplaceAll(endpoint, "https://", "")
endpoint = strings.ReplaceAll(endpoint, "/", "")
return endpoint
}).(pulumi.StringOutput)
queryStringCachingBehaviorNotSet := cdn.QueryStringCachingBehaviorNotSet
endpoint, err := cdn.NewEndpoint(ctx, "endpoint", &cdn.EndpointArgs{
IsHttpAllowed: pulumi.Bool(false),
IsHttpsAllowed: pulumi.Bool(true),
OriginHostHeader: endpointOrigin,
Origins: cdn.DeepCreatedOriginArray{
&cdn.DeepCreatedOriginArgs{
HostName: endpointOrigin,
HttpsPort: pulumi.Int(443),
Name: pulumi.String("origin-storage-account"),
},
},
ProfileName: profile.Name,
QueryStringCachingBehavior: &queryStringCachingBehaviorNotSet,
ResourceGroupName: resourceGroup.Name,
})
if err != nil {
return err
}
// Enable static website support
staticWebsite, err := storage.NewStorageAccountStaticWebsite(ctx, "staticWebsite", &storage.StorageAccountStaticWebsiteArgs{
AccountName: storageAccount.Name,
ResourceGroupName: resourceGroup.Name,
IndexDocument: pulumi.String("index.html"),
Error404Document: pulumi.String("404.html"),
})
if err != nil {
return err
}
// Upload the files
_, err = storage.NewBlob(ctx, "index.html", &storage.BlobArgs{
ResourceGroupName: resourceGroup.Name,
AccountName: storageAccount.Name,
ContainerName: staticWebsite.ContainerName,
Source: pulumi.NewFileAsset("./wwwroot/index.html"),
ContentType: pulumi.String("text/html"),
})
if err != nil {
return err
}
_, err = storage.NewBlob(ctx, "404.html", &storage.BlobArgs{
ResourceGroupName: resourceGroup.Name,
AccountName: storageAccount.Name,
ContainerName: staticWebsite.ContainerName,
Source: pulumi.NewFileAsset("./wwwroot/404.html"),
ContentType: pulumi.String("text/html"),
})
if err != nil {
return err
}
// Web endpoint to the website
ctx.Export("staticEndpoint", storageAccount.PrimaryEndpoints.Web())
// CDN endpoint to the website.
// Allow it some time after the deployment to get ready.
ctx.Export("cdnEndpoint", endpoint.HostName.ApplyT(func(hostName string) string {
return fmt.Sprintf("%v%v", "https://", hostName)
}))
return nil
})
}