-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
226 lines (198 loc) · 10.7 KB
/
Program.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.Network.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core.ResourceActions;
using Microsoft.Azure.Management.Samples.Common;
using Microsoft.Azure.Management.Sql.Fluent;
using Microsoft.Azure.Management.Sql.Fluent.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ManageSqlDatabasesAcrossDifferentDataCenters
{
public class Program
{
private static readonly string administratorLogin = "sqladmin3423";
private static readonly string administratorPassword = Utilities.CreatePassword();
private static readonly string slaveSqlServer1Name = "slave1sql";
private static readonly string slaveSqlServer2Name = "slave2sql";
private static readonly string databaseName = "mydatabase";
private static readonly string networkNamePrefix = "network";
private static readonly string virtualMachineNamePrefix = "samplevm";
/**
* Azure Storage sample for managing SQL Database -
* - Create 3 SQL Servers in different region.
* - Create a master database in master SQL Server.
* - Create 2 more SQL Servers in different azure regions
* - Create secondary read only databases in these server with source as database in server created in step 1.
* - Create 5 virtual networks in different regions.
* - Create one VM in each of the virtual network.
* - Update all three databases to have firewall rules with range of each of the virtual network.
*/
public static void RunSample(IAzure azure)
{
string sqlServerName = SdkContext.RandomResourceName("sqlserver", 20);
string rgName = SdkContext.RandomResourceName("rgRSSDRE", 20);
try
{
// ============================================================
// Create a SQL Server, with 2 firewall rules.
var masterSqlServer = azure.SqlServers.Define(sqlServerName)
.WithRegion(Region.USEast)
.WithNewResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.Create();
Utilities.PrintSqlServer(masterSqlServer);
// ============================================================
// Create a Database in master SQL server created above.
Utilities.Log("Creating a database");
var masterDatabase = masterSqlServer.Databases.Define(databaseName)
.WithEdition(DatabaseEdition.Basic)
.Create();
Utilities.PrintDatabase(masterDatabase);
// Create secondary databases for the master database
var sqlServerInSecondaryLocation = azure.SqlServers
.Define(Utilities.CreateRandomName(slaveSqlServer1Name))
.WithRegion(masterDatabase.DefaultSecondaryLocation)
.WithExistingResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.Create();
Utilities.PrintSqlServer(sqlServerInSecondaryLocation);
Utilities.Log("Creating database in slave SQL Server.");
var secondaryDatabase = sqlServerInSecondaryLocation.Databases.Define(databaseName)
.WithSourceDatabase(masterDatabase)
.WithMode(CreateMode.OnlineSecondary)
.Create();
Utilities.PrintDatabase(secondaryDatabase);
var sqlServerInEurope = azure.SqlServers
.Define(Utilities.CreateRandomName(slaveSqlServer2Name))
.WithRegion(Region.EuropeWest)
.WithExistingResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.Create();
Utilities.PrintSqlServer(sqlServerInEurope);
Utilities.Log("Creating database in second slave SQL Server.");
var secondaryDatabaseInEurope = sqlServerInEurope.Databases.Define(databaseName)
.WithSourceDatabase(masterDatabase)
.WithMode(CreateMode.OnlineSecondary)
.Create();
Utilities.PrintDatabase(secondaryDatabaseInEurope);
// ============================================================
// Create Virtual Networks in different regions
var regions = new List<Region>();
regions.Add(Region.USEast);
regions.Add(Region.USWest);
regions.Add(Region.EuropeNorth);
regions.Add(Region.AsiaSouthEast);
regions.Add(Region.JapanEast);
var creatableNetworks = new List<ICreatable<INetwork>>();
Utilities.Log("Creating virtual networks in different regions.");
foreach (Region region in regions)
{
creatableNetworks.Add(azure.Networks.Define(Utilities.CreateRandomName(networkNamePrefix))
.WithRegion(region)
.WithExistingResourceGroup(rgName));
}
var networks = azure.Networks.Create(creatableNetworks.ToArray());
// ============================================================
// Create virtual machines attached to different virtual networks created above.
var creatableVirtualMachines = new List<ICreatable<IVirtualMachine>>();
foreach (var network in networks)
{
var vmName = Utilities.CreateRandomName(virtualMachineNamePrefix);
var publicIpAddressCreatable = azure.PublicIPAddresses
.Define(vmName)
.WithRegion(network.Region)
.WithExistingResourceGroup(rgName)
.WithLeafDomainLabel(vmName);
creatableVirtualMachines.Add(azure.VirtualMachines.Define(vmName)
.WithRegion(network.Region)
.WithExistingResourceGroup(rgName)
.WithExistingPrimaryNetwork(network)
.WithSubnet(network.Subnets.Values.First().Name)
.WithPrimaryPrivateIPAddressDynamic()
.WithNewPrimaryPublicIPAddress(publicIpAddressCreatable)
.WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012R2Datacenter)
.WithAdminUsername(administratorLogin)
.WithAdminPassword(administratorPassword)
.WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4")));
}
var ipAddresses = new Dictionary<string, string>();
var virtualMachines = azure.VirtualMachines.Create(creatableVirtualMachines.ToArray());
foreach (var virtualMachine in virtualMachines)
{
ipAddresses.Add(virtualMachine.Name, virtualMachine.GetPrimaryPublicIPAddress().IPAddress);
}
Utilities.Log("Adding firewall rule for each of virtual network network");
var sqlServers = new List<ISqlServer>();
sqlServers.Add(sqlServerInSecondaryLocation);
sqlServers.Add(sqlServerInEurope);
sqlServers.Add(masterSqlServer);
foreach (var sqlServer in sqlServers)
{
foreach (var ipAddress in ipAddresses)
{
sqlServer.FirewallRules.Define(ipAddress.Key).WithIPAddress(ipAddress.Value).Create();
}
}
foreach (var sqlServer in sqlServers)
{
Utilities.Log("Print firewall rules in Sql Server in " + sqlServer.RegionName);
var firewallRules = sqlServer.FirewallRules.List();
foreach (var firewallRule in firewallRules)
{
Utilities.PrintFirewallRule(firewallRule);
}
}
// Delete the SQL Server.
Utilities.Log("Deleting all Sql Servers");
foreach (var sqlServer in sqlServers)
{
azure.SqlServers.DeleteById(sqlServer.Id);
}
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.DeleteByName(rgName);
Utilities.Log("Deleted Resource Group: " + rgName);
}
catch
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
}
}
public static void Main(string[] args)
{
try
{
//=================================================================
// Authenticate
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
// Print selected subscription
Utilities.Log("Selected subscription: " + azure.SubscriptionId);
RunSample(azure);
}
catch (Exception e)
{
Utilities.Log(e);
}
}
}
}