-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.cs
142 lines (123 loc) · 5.98 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
// 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.Models;
using System;
namespace ManageSqlDatabase
{
public class Program
{
private static readonly string AdministratorLogin = "sqladmin3423";
private static readonly string AdministratorPassword = Utilities.CreatePassword();
private static readonly string FirewallRuleIPAddress = "10.0.0.1";
private static readonly string FirewallRuleStartIPAddress = "10.2.0.1";
private static readonly string FirewallRuleEndIPAddress = "10.2.0.10";
private static readonly string DatabaseName = "mydatabase";
/**
* Azure Storage sample for managing SQL Database -
* - Create a SQL Server along with 2 firewalls.
* - Create a database in SQL server
* - Change performance level (SKU) of SQL Database
* - List and delete firewalls.
* - Create another firewall in the SQlServer
* - Delete database, firewall and SQL Server
*/
public static void RunSample(IAzure azure)
{
string sqlServerName = SdkContext.RandomResourceName("sqlserver", 20);
string rgName = SdkContext.RandomResourceName("rgRSDSI", 20);
try
{
// ============================================================
// Create a SQL Server, with 2 firewall rules.
var sqlServer = azure.SqlServers.Define(sqlServerName)
.WithRegion(Region.USEast)
.WithNewResourceGroup(rgName)
.WithAdministratorLogin(AdministratorLogin)
.WithAdministratorPassword(AdministratorPassword)
.WithNewFirewallRule(FirewallRuleIPAddress)
.WithNewFirewallRule(FirewallRuleStartIPAddress, FirewallRuleEndIPAddress)
.Create();
Utilities.PrintSqlServer(sqlServer);
// ============================================================
// Create a Database in SQL server created above.
Utilities.Log("Creating a database");
var database = sqlServer.Databases
.Define(DatabaseName)
.Create();
Utilities.PrintDatabase(database);
// ============================================================
// Update the edition of database.
Utilities.Log("Updating a database");
database = database.Update()
.WithEdition(DatabaseEdition.Standard)
.WithServiceObjective(ServiceObjectiveName.S1)
.WithMaxSizeBytes((long)Microsoft.Azure.Management.Sql.Fluent.SqlDatabaseStandardStorage.Max250Gb)
.Apply();
Utilities.PrintDatabase(database);
// ============================================================
// List and delete all firewall rules.
Utilities.Log("Listing all firewall rules");
var firewallRules = sqlServer.FirewallRules.List();
foreach (var firewallRule in firewallRules)
{
// Print information of the firewall rule.
Utilities.PrintFirewallRule(firewallRule);
// Delete the firewall rule.
Utilities.Log("Deleting a firewall rule");
firewallRule.Delete();
}
// ============================================================
// Add new firewall rules.
Utilities.Log("Creating a firewall rule for SQL Server");
var newFirewallRule = sqlServer.FirewallRules.Define("myFirewallRule")
.WithIPAddress("10.10.10.10")
.Create();
Utilities.PrintFirewallRule(newFirewallRule);
// Delete the database.
Utilities.Log("Deleting a database");
database.Delete();
// Delete the SQL Server.
Utilities.Log("Deleting a Sql Server");
azure.SqlServers.DeleteById(sqlServer.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());
}
}
}
}