forked from Azure-Samples/redis-cache-dotnet-manage-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
221 lines (201 loc) · 10.9 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Samples.Common;
using Azure.ResourceManager.Redis;
using Azure.ResourceManager.Redis.Models;
namespace ManageRedis
{
public class Program
{
/**
* Azure Redis sample for managing Redis Cache:
* - Create a Redis Cache and print out hostname.
* - Get access keys.
* - Regenerate access keys.
* - Create another 2 Redis Caches with Premium Sku.
* - List all Redis Caches in a resource group – for each cache with Premium Sku:
* - set Redis patch schedule to Monday at 5 am.
* - update shard count.
* - enable non-SSL port.
* - modify max memory policy and reserved settings.
* - restart it.
* - Clean up all resources.
*/
private static ResourceIdentifier? _resourceGroupId = null;
public static async Task RunSample(ArmClient client)
{
try
{
// ============================================================
// Get default subscription
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
// Create a resource group in the USCentral region
var rgName = Utilities.CreateRandomName("RedisRG");
Utilities.Log($"creating resource group with name:{rgName}");
var rgLro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(AzureLocation.CentralUS));
var resourceGroup = rgLro.Value;
_resourceGroupId = resourceGroup.Id;
Utilities.Log("Created a resource group with name: " + resourceGroup.Data.Name);
// Create a Redis cache
Utilities.Log("Creating the 1st Cache...");
var redisCacheName1 = Utilities.CreateRandomName("rc1");
var redisCacheCollection = resourceGroup.GetAllRedis();
var parameter = new RedisCreateOrUpdateContent(AzureLocation.CentralUS, new RedisSku(RedisSkuName.Basic, RedisSkuFamily.BasicOrStandard, 0));
var task1 = redisCacheCollection.CreateOrUpdateAsync(WaitUntil.Completed, redisCacheName1, parameter);
// ============================================================
// Create another two Redis Caches
// Create the 2nd Redis Caches
Utilities.Log("Creating the 2nd Redis Caches...");
var redisCacheName2 = Utilities.CreateRandomName("rc2");
var parameter2 = new RedisCreateOrUpdateContent(AzureLocation.CentralUS, new RedisSku(RedisSkuName.Premium, RedisSkuFamily.Premium, 1))
{
ShardCount = 3
};
var task2 = redisCacheCollection.CreateOrUpdateAsync(WaitUntil.Completed, redisCacheName2, parameter2);
// Create the 3rd Redis cache
Utilities.Log("Creating the 3rd Redis cache...");
var redisCacheName3 = Utilities.CreateRandomName("rc3");
var parameter3 = new RedisCreateOrUpdateContent(AzureLocation.CentralUS, new RedisSku(RedisSkuName.Premium, RedisSkuFamily.Premium, 2))
{
ShardCount = 3
};
var task3 = redisCacheCollection.CreateOrUpdateAsync(WaitUntil.Completed, redisCacheName3, parameter3);
await Task.WhenAll(task1, task2, task3);
Utilities.Log($"Created all");
// ============================================================
// Get | regenerate Redis Cache access keys
Utilities.Log("Getting Redis Cache access keys");
var redisAccessKeys = task1.Result.Value.GetKeys();
Utilities.Log("Got Redis Cache access keys");
Utilities.Log("Regenerating secondary Redis Cache access key");
var content = new RedisRegenerateKeyContent(RedisRegenerateKeyType.Secondary);
_ = task1.Result.Value.RegenerateKey(content);
Utilities.Log("Regenerated secondary Redis Cache access key");
// ============================================================
// List Redis Caches inside the resource group
// Create a Patch Schedules
Utilities.Log("Creating a Patch Schedules...");
var scheduleCollection2 = task2.Result.Value.GetRedisPatchSchedules();
var data2 = new RedisPatchScheduleData(new RedisPatchScheduleSetting[]
{
new RedisPatchScheduleSetting(RedisDayOfWeek.Tuesday, 11)
{
MaintenanceWindow = TimeSpan.FromHours(11)
}
});
_ = (await scheduleCollection2.CreateOrUpdateAsync(WaitUntil.Completed, RedisPatchScheduleDefaultName.Default, data2)).Value;
Utilities.Log("Created a Patch Schedules");
// Create another Patch Schedules
Utilities.Log("Creating another Patch Schedules...");
var scheduleCollection3 = task3.Result.Value.GetRedisPatchSchedules();
var data3 = new RedisPatchScheduleData(new RedisPatchScheduleSetting[]
{
new RedisPatchScheduleSetting(RedisDayOfWeek.Tuesday, 11)
{
MaintenanceWindow = TimeSpan.FromHours(11)
}
});
_ = (await scheduleCollection3.CreateOrUpdateAsync(WaitUntil.Completed, RedisPatchScheduleDefaultName.Default, data3)).Value;
Utilities.Log("Created another Patch Schedules");
Utilities.Log("Listing Redis Caches");
await foreach (var caches in redisCacheCollection.GetAllAsync())
{
Utilities.Log("==================");
var premium = caches.Data.Sku.Name.Equals("Premium");
Utilities.Log(caches.Data.Sku.Name);
if (premium)
{
// Restart Redis Cache
Utilities.Log("Restarting updated Redis Cache");
var ForceRebootcontent = new RedisRebootContent()
{
RebootType = RedisRebootType.AllNodes,
ShardId = 1
};
_ = caches.ForceReboot(ForceRebootcontent);
Utilities.Log("Redis Cache restart scheduled");
// Update each Premium Sku Redis Cache instance
Utilities.Log("Updating Premium Redis Cache");
var patch = new RedisPatch()
{
ShardCount = 4,
EnableNonSslPort = true,
RedisConfiguration = new RedisCommonConfiguration()
{
MaxMemoryPolicy = "allkeys-random",
MaxMemoryReserved = "20"
}
};
_ = await caches.UpdateAsync(patch);
Utilities.Log("Updated Premium Redis Cache");
// Updating Redis Patch Schedule Setting
Utilities.Log("Updating Redis Patch Schedule Setting");
var scheduleEntries = new RedisPatchScheduleSetting[]
{
new RedisPatchScheduleSetting(RedisDayOfWeek.Monday, 5)
{
MaintenanceWindow = TimeSpan.FromHours(5)
}
};
var scheduleData = new RedisPatchScheduleData(scheduleEntries);
foreach(var schedule in caches.GetRedisPatchSchedules())
{
_ = await schedule.UpdateAsync(WaitUntil.Completed, scheduleData);
}
Utilities.Log("Updated Redis Patch Schedule Setting");
Utilities.Log("Deleting a Redis Cache - " + task1.Result.Value.Data.Name);
_ =caches.DeleteAsync(WaitUntil.Completed);
Utilities.Log("Deleted Redis Cache");
}
}
// ============================================================
// Delete a Redis Cache
Utilities.Log("Deleting a Redis Cache - " + task1.Result.Value.Data.Name);
_ = task1.Result.Value.DeleteAsync(WaitUntil.Completed);
Utilities.Log("Deleted Redis Cache");
}
finally
{
try
{
if (_resourceGroupId is not null)
{
Utilities.Log($"Deleting Resource Group: {_resourceGroupId}");
await client.GetResourceGroupResource(_resourceGroupId).DeleteAsync(WaitUntil.Completed);
Utilities.Log($"Deleted Resource Group: {_resourceGroupId}");
}
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception g)
{
Utilities.Log(g);
}
}
}
public static async Task Main(string[] args)
{
try
{
var clientId = Environment.GetEnvironmentVariable("CLIENT_ID");
var clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET");
var tenantId = Environment.GetEnvironmentVariable("TENANT_ID");
var subscription = Environment.GetEnvironmentVariable("SUBSCRIPTION_ID");
ClientSecretCredential credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
ArmClient client = new ArmClient(credential, subscription);
await RunSample(client);
}
catch (Exception e)
{
Utilities.Log(e);
}
}
}
}