-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
249 lines (214 loc) · 12.6 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// 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.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using Microsoft.Azure.Management.Sql.Fluent;
using Microsoft.Azure.Management.Sql.Fluent.Models;
using System;
using System.Data.SqlClient;
namespace ManageSqlServerDnsAliases
{
public class Program
{
private static readonly string sqlServerForTestName = SdkContext.RandomResourceName("sqltest", 20);
private static readonly string sqlServerForProdName = SdkContext.RandomResourceName("sqlprod", 20);
private static readonly string sqlServerDnsAlias = SdkContext.RandomResourceName("sqlserver", 20);
private static readonly string rgName = SdkContext.RandomResourceName("rgsql", 20);
private static readonly string dbName = "dbSample";
private static readonly string administratorLogin = "sqladmin3423";
private static readonly string administratorPassword = Utilities.CreatePassword();
/**
* Azure SQL sample for managing SQL Server DNS Aliases.
* - Create two SQL Servers "test" and "production", each with an empty database.
* - Create a new table and insert some expected values into each database.
* - Create a SQL Server DNS Alias to the "test" SQL database.
* - Query the "test" SQL database via the DNS alias and print the result.
* - Use the SQL Server DNS alias to acquire the "production" SQL database.
* - Query the "production" SQL database via the DNS alias and print the result.
* - Delete the SQL Servers
*/
public static void RunSample(IAzure azure)
{
try
{
// ============================================================
// Create a "test" SQL Server.
Utilities.Log("Creating a SQL server for test related activities");
var sqlServerForTest = azure.SqlServers.Define(sqlServerForTestName)
.WithRegion(Region.USSouthCentral)
.WithNewResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.DefineFirewallRule("allowAll")
.WithIPAddressRange("0.0.0.1", "255.255.255.255")
.Attach()
.DefineDatabase(dbName)
.WithBasicEdition()
.Attach()
.Create();
Utilities.PrintSqlServer(sqlServerForTest);
// ============================================================
// Create a connection to the "test" SQL Server.
Utilities.Log("Creating a connection to the \"test\" SQL Server");
var connectionToSqlTestUrl = $"user id={administratorLogin};" +
$"password={administratorPassword};" +
$"server={sqlServerForTest.FullyQualifiedDomainName};" +
$"database={dbName}; " +
"Trusted_Connection=False;" +
"Encrypt=True;" +
"connection timeout=30";
// Create a connection to the SQL Server.
using (SqlConnection conTest = new SqlConnection(connectionToSqlTestUrl))
{
conTest.Open();
// ============================================================
// Create a new table into the "test" SQL Server database and insert one value.
Utilities.Log("Creating a new table into the \"test\" SQL Server database and insert one value");
string sqlCreateTableCommand = "CREATE TABLE [Dns_Alias_Sample_Test] ([Name] [varchar](30) NOT NULL)";
SqlCommand createTable = new SqlCommand(sqlCreateTableCommand, conTest);
createTable.ExecuteNonQuery();
string sqlInsertCommand = "INSERT INTO Dns_Alias_Sample_Test VALUES ('Test')";
SqlCommand insertValue = new SqlCommand(sqlInsertCommand, conTest);
insertValue.ExecuteNonQuery();
// Close the connection to the "test" database
conTest.Close();
}
// ============================================================
// Create a "production" SQL Server.
Utilities.Log("Creating a SQL server for production related activities");
var sqlServerForProd = azure.SqlServers.Define(sqlServerForProdName)
.WithRegion(Region.USNorthCentral)
.WithExistingResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.DefineFirewallRule("allowAll")
.WithIPAddressRange("0.0.0.1", "255.255.255.255")
.Attach()
.DefineDatabase(dbName)
.WithBasicEdition()
.Attach()
.Create();
Utilities.PrintSqlServer(sqlServerForProd);
// ============================================================
// Create a connection to the "production" SQL Server.
Utilities.Log("Creating a connection to the \"production\" SQL Server");
var connectionToSqlProdUrl = $"user id={administratorLogin};" +
$"password={administratorPassword};" +
$"server={sqlServerForProd.FullyQualifiedDomainName};" +
$"database={dbName}; " +
"Trusted_Connection=False;" +
"Encrypt=True;" +
"connection timeout=30";
// Create a connection to the SQL Server.
using (SqlConnection conProd = new SqlConnection(connectionToSqlProdUrl))
{
conProd.Open();
// ============================================================
// Create a new table into the "production" SQL Server database and insert one value.
Utilities.Log("Creating a new table into the \"production\" SQL Server database and insert one value");
string sqlCreateTableCommand = "CREATE TABLE [Dns_Alias_Sample_Prod] ([Name] [varchar](30) NOT NULL)";
SqlCommand createTable = new SqlCommand(sqlCreateTableCommand, conProd);
createTable.ExecuteNonQuery();
string sqlInsertCommand = "INSERT INTO Dns_Alias_Sample_Prod VALUES ('Production')";
SqlCommand insertValue = new SqlCommand(sqlInsertCommand, conProd);
insertValue.ExecuteNonQuery();
// Close the connection to the "prod" database
conProd.Close();
}
// ============================================================
// Create a SQL Server DNS alias and use it to query the "test" database.
Utilities.Log("Creating a SQL Server DNS alias and use it to query the \"test\" database");
var dnsAlias = sqlServerForTest.DnsAliases
.Define(sqlServerDnsAlias)
.Create();
SdkContext.DelayProvider.Delay(5 * 60 * 1000);
var connectionUrl = $"user id={administratorLogin};" +
$"password={administratorPassword};" +
$"server={dnsAlias.AzureDnsRecord};" +
$"database={dbName}; " +
"Trusted_Connection=False;" +
"Encrypt=True;" +
"connection timeout=30";
// Create a connection to the SQL DNS alias.
using (SqlConnection conDnsAlias = new SqlConnection(connectionUrl))
{
conDnsAlias.Open();
// ============================================================
// Create a SQL Server DNS alias and use it to query the "test" database.
Utilities.Log("Creating a SQL Server DNS alias and use it to query the \"test\" database");
string sqlCommand = "SELECT * FROM Dns_Alias_Sample_Test;";
SqlCommand selectCommand = new SqlCommand(sqlCommand, conDnsAlias);
var myReader = selectCommand.ExecuteReader();
while (myReader.Read())
{
Utilities.Log(myReader["Name"].ToString());
}
conDnsAlias.Close();
}
// ============================================================
// Use the "production" SQL Server to acquire the SQL Server DNS Alias and use it to query the "production" database.
Utilities.Log("Using the \"production\" SQL Server to acquire the SQL Server DNS Alias and use it to query the \"production\" database");
sqlServerForProd.DnsAliases
.Acquire(sqlServerDnsAlias, sqlServerForTest.Id);
// It takes some time for the DNS alias to reflect the new Server connection
SdkContext.DelayProvider.Delay(10 * 60 * 1000);
// Re-establish the connection.
using (SqlConnection conDnsAlias = new SqlConnection(connectionUrl))
{
conDnsAlias.Open();
// ============================================================
// Create a SQL Server DNS alias and use it to query the "production" database.
Utilities.Log("Creating a SQL Server DNS alias and use it to query the \"production\" database");
string sqlCommand = "SELECT * FROM Dns_Alias_Sample_Prod;";
SqlCommand selectCommand = new SqlCommand(sqlCommand, conDnsAlias);
var myReader = selectCommand.ExecuteReader();
while (myReader.Read())
{
Utilities.Log(myReader["Name"].ToString());
}
conDnsAlias.Close();
}
// Delete the SQL Servers.
Utilities.Log("Deleting the Sql Servers");
azure.SqlServers.DeleteById(sqlServerForTest.Id);
azure.SqlServers.DeleteById(sqlServerForProd.Id);
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.DeleteByName(rgName);
Utilities.Log("Deleted Resource Group: " + rgName);
}
catch (Exception e)
{
Utilities.Log(e);
}
}
}
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.ToString());
}
}
}
}