forked from jetvova/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCosmosApp.cs
179 lines (153 loc) · 5.83 KB
/
CosmosApp.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
using System;
using System.Collections.Immutable;
using System.Linq;
using Pulumi;
using Pulumi.Azure.Core;
using Pulumi.Azure.CosmosDB;
using Pulumi.Azure.CosmosDB.Inputs;
using Pulumi.Azure.Network;
using Pulumi.Azure.Network.Inputs;
public class GlobalContext
{
public ResourceGroup ResourceGroup { get; }
public Account CosmosAccount { get; }
public SqlDatabase Database { get; }
public SqlContainer Container { get; }
public CustomResourceOptions Options { get; }
internal GlobalContext(ResourceGroup resourceGroup, Account cosmosAccount, SqlDatabase database, SqlContainer container, CustomResourceOptions options)
{
this.ResourceGroup = resourceGroup;
this.CosmosAccount = cosmosAccount;
this.Database = database;
this.Container = container;
this.Options = options;
}
}
public class RegionalContext
{
public string Location { get; }
internal RegionalContext(string location)
{
this.Location = location;
}
}
public interface IRegionalEndpoint
{
// Type of the endpoint
Input<string> Type { get; }
// Azure resource ID (App Service and Public IP are supported)
Input<string>? Id { get; }
// An arbitrary URL for other resource types
Input<string>? Url { get; }
}
public class AzureEndpoint : IRegionalEndpoint
{
public Input<string> Type => "azureEndpoints";
public Input<string> Id { get; }
public Input<string>? Url => null;
public AzureEndpoint(Input<string> id)
{
this.Id = id;
}
}
public class ExternalEndpoint : IRegionalEndpoint
{
public Input<string> Type => "externalEndpoints";
public Input<string>? Id => null;
public Input<string> Url { get; }
public ExternalEndpoint(Input<string> url)
{
this.Url = url;
}
}
public class CosmosAppArgs
{
public ResourceGroup ResourceGroup { get; set; } = null!;
public string[] Locations { get; set; } = null!;
public Func<GlobalContext, Func<RegionalContext, IRegionalEndpoint>> Factory { get; set; } = null!;
public Input<string> DatabaseName { get; set; } = null!;
public Input<string> ContainerName { get; set; } = null!;
public Input<bool>? EnableMultiMaster { get; set; }
}
public class CosmosApp : ComponentResource
{
public Output<string> Endpoint { get; private set; } = null!;
public CosmosApp(string name, CosmosAppArgs args, ComponentResourceOptions? options = null)
: base("examples:azure:CosmosApp", name, options)
{
var resourceGroup = args.ResourceGroup;
var locations = args.Locations;
var primaryLocation = locations[0];
var parentOptions = new CustomResourceOptions { Parent = this };
// Cosmos DB Account with multiple replicas
var cosmosAccount = new Account($"cosmos-{name}",
new AccountArgs
{
ResourceGroupName = resourceGroup.Name,
Location = primaryLocation,
GeoLocations = locations.Select((l, i) => new AccountGeoLocationArgs { Location = l, FailoverPriority = i }).ToArray(),
OfferType = "Standard",
ConsistencyPolicy = new AccountConsistencyPolicyArgs { ConsistencyLevel = "Session" },
EnableMultipleWriteLocations = args.EnableMultiMaster,
},
parentOptions);
var database = new SqlDatabase($"db-{name}",
new SqlDatabaseArgs
{
ResourceGroupName = resourceGroup.Name,
AccountName = cosmosAccount.Name,
Name = args.DatabaseName,
},
parentOptions);
var container = new SqlContainer($"sql-{name}",
new SqlContainerArgs
{
ResourceGroupName = resourceGroup.Name,
AccountName = cosmosAccount.Name,
DatabaseName = database.Name,
Name = args.ContainerName,
},
parentOptions);
// Traffic Manager as a global HTTP endpoint
var profile = new TrafficManagerProfile($"tm{name}",
new TrafficManagerProfileArgs
{
ResourceGroupName = resourceGroup.Name,
TrafficRoutingMethod = "Performance",
DnsConfig = new TrafficManagerProfileDnsConfigArgs
{
// Subdomain must be globally unique, so we default it with the full resource group name
RelativeName = Output.Format($"{name}{resourceGroup.Name}"),
Ttl = 60,
},
MonitorConfig = new TrafficManagerProfileMonitorConfigArgs
{
Protocol = "HTTP",
Port = 80,
Path = "/api/ping",
}
},
parentOptions);
var globalContext = new GlobalContext(resourceGroup, cosmosAccount, database, container, parentOptions);
var buildLocation = args.Factory(globalContext);
var endpointOptions = new CustomResourceOptions { Parent = profile, DeleteBeforeReplace = true };
var endpoints = locations.Select(location =>
{
var app = buildLocation(new RegionalContext(location));
return new TrafficManagerEndpoint($"tm{name}{location}".Truncate(16),
new TrafficManagerEndpointArgs
{
ResourceGroupName = resourceGroup.Name,
ProfileName = profile.Name,
Type = app.Type,
TargetResourceId = app.Id,
Target = app.Url,
EndpointLocation = location,
},
endpointOptions);
}).ToList();
this.Endpoint = Output.Format($"http://{profile.Fqdn}");
this.RegisterOutputs();
}
}