-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
188 lines (150 loc) · 8.26 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
// 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;
namespace ManageSqlFailoverGroups
{
public class Program
{
private static readonly string sqlPrimaryServerName = SdkContext.RandomResourceName("sqlserver", 20);
private static readonly string sqlSecondaryServerName = 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 Failover Groups
* - Create a primary SQL Server with a sample database and a secondary SQL Server.
* - Get a failover group from the primary SQL server to the secondary SQL server.
* - Update a failover group.
* - List all failover groups.
* - Delete a failover group.
* - Delete Sql Server
*/
public static void RunSample(IAzure azure)
{
try
{
// ============================================================
// Create a primary SQL Server with a sample database.
Utilities.Log("Creating a primary SQL Server with a sample database");
var sqlPrimaryServer = azure.SqlServers.Define(sqlPrimaryServerName)
.WithRegion(Region.AsiaSouthEast)
.WithNewResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.DefineDatabase(dbName)
.FromSample(SampleName.AdventureWorksLT)
.WithStandardEdition(SqlDatabaseStandardServiceObjective.S0)
.Attach()
.Create();
Utilities.PrintSqlServer(sqlPrimaryServer);
// ============================================================
// Create a secondary SQL Server with a sample database.
Utilities.Log("Creating a secondary SQL Server with a sample database");
var sqlSecondaryServer = azure.SqlServers.Define(sqlSecondaryServerName)
.WithRegion(Region.USEast2)
.WithExistingResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.Create();
Utilities.PrintSqlServer(sqlSecondaryServer);
// ============================================================
// Create a Failover Group from the primary SQL server to the secondary SQL server.
Utilities.Log("Creating a Failover Group from the primary SQL server to the secondary SQL server");
string failoverGroupName = SdkContext.RandomResourceName("my-other-failover-group", 25);
var failoverGroup = sqlPrimaryServer.FailoverGroups.Define(failoverGroupName)
.WithManualReadWriteEndpointPolicy()
.WithPartnerServerId(sqlSecondaryServer.Id)
.WithReadOnlyEndpointPolicyDisabled()
.Create();
Utilities.PrintSqlFailoverGroup(failoverGroup);
// ============================================================
// Get the Failover Group from the secondary SQL server.
Utilities.Log("Getting the Failover Group from the secondary SQL server");
var failoverGroupOnPartner = sqlSecondaryServer.FailoverGroups.Get(failoverGroup.Name);
Utilities.PrintSqlFailoverGroup(failoverGroup);
// ============================================================
// Update the Failover Group Endpoint policies and tags.
Utilities.Log("Updating the Failover Group Endpoint policies and tags");
failoverGroup.Update()
.WithAutomaticReadWriteEndpointPolicyAndDataLossGracePeriod(120)
.WithReadOnlyEndpointPolicyEnabled()
.WithTag("tag1", "value1")
.Apply();
Utilities.PrintSqlFailoverGroup(failoverGroup);
// ============================================================
// Update the Failover Group to add database and change read-write endpoint's failover policy.
Utilities.Log("Updating the Failover Group to add database and change read-write endpoint's failover policy");
var db = sqlPrimaryServer.Databases.Get(dbName);
Utilities.PrintDatabase(db);
failoverGroup.Update()
.WithManualReadWriteEndpointPolicy()
.WithReadOnlyEndpointPolicyDisabled()
.WithNewDatabaseId(db.Id)
.Apply();
Utilities.PrintSqlFailoverGroup(failoverGroup);
// ============================================================
// List the Failover Group on the secondary server.
Utilities.Log("Listing the Failover Group on the secondary server");
foreach (var item in sqlSecondaryServer.FailoverGroups.List())
{
Utilities.PrintSqlFailoverGroup(item);
}
// ============================================================
// Get the database from the secondary SQL server.
Utilities.Log("Getting the database from the secondary server");
SdkContext.DelayProvider.Delay(3 * 60 * 1000);
db = sqlSecondaryServer.Databases.Get(dbName);
Utilities.PrintDatabase(db);
// ============================================================
// Delete the Failover Group.
Utilities.Log("Deleting the Failover Group");
sqlPrimaryServer.FailoverGroups.Delete(failoverGroup.Name);
// Delete the SQL Servers.
Utilities.Log("Deleting the Sql Servers");
azure.SqlServers.DeleteById(sqlPrimaryServer.Id);
azure.SqlServers.DeleteById(sqlSecondaryServer.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());
}
}
}
}