-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
142 lines (134 loc) · 5.74 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
using Microsoft.Azure.Management.Media;
using Microsoft.Azure.Management.Media.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
using Microsoft.Rest.Azure;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace DeleteAllAssets
{
class Program
{
private static IAzureMediaServicesClient client = null;
static void Main(string[] args)
{
ConfigWrapper config = new ConfigWrapper(new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build());
try
{
RunApplication(config);
}
catch (Exception exception)
{
if (exception.Source.Contains("ActiveDirectory"))
{
Console.Error.WriteLine("TIP: Make sure that you have filled out the appsettings.json file before running this sample.");
}
Console.Error.WriteLine($"{exception.Message}");
ApiErrorException apiException = exception.GetBaseException() as ApiErrorException;
if (apiException != null)
{
Console.Error.WriteLine(
$"ERROR: API call failed with error code '{apiException.Body.Error.Code}' and message '{apiException.Body.Error.Message}'.");
}
}
}
private static void RunApplication(ConfigWrapper config)
{
client = CreateMediaServicesClient(config);
// Set the polling interval for long running operations to 2 seconds.
// The default value is 30 seconds for the .NET client SDK
client.LongRunningOperationRetryTimeout = 2;
List<string> assetNameList = new List<string>();
Console.WriteLine("Building a list of assets...");
// Get a list of all of the assets and enumerate through them a page at a time.
IPage<Asset> assetPage = client.Assets.List(config.ResourceGroup, config.AccountName);
bool breakout;
do
{
foreach (Asset asset in assetPage)
{
// Add each asset name to a list
assetNameList.Add(asset.Name);
}
if (assetPage.NextPageLink != null)
{
assetPage = client.Assets.ListNext(assetPage.NextPageLink);
breakout = false;
}
else
{
breakout = true;
}
}
while (!breakout);
// We delete in a separate for loop because deleting within the do loop changes the size of the loop itself.
bool always = false;
for (int i = 0; i < assetNameList.Count; i++)
{
if (!always)
{
Console.WriteLine("Delete the asset '{0}'? (y)es, (n)o, (a)ll assets, (q)uit", assetNameList[i]);
ConsoleKeyInfo response = Console.ReadKey();
Console.WriteLine();
string responseChar = response.Key.ToString();
if (responseChar.Equals("N"))
continue;
if (responseChar.Equals("A"))
{
always = true;
}
else if (!(responseChar.Equals("Y")))
{
break; // At this point anything other than a 'yes' should quit the loop/application.
}
}
Console.WriteLine("Deleting {0}", assetNameList[i]);
client.Assets.Delete(config.ResourceGroup, config.AccountName, assetNameList[i]);
}
}
private static IAzureMediaServicesClient CreateMediaServicesClient(ConfigWrapper config)
{
ArmClientCredentials credentials = new ArmClientCredentials(config);
return new AzureMediaServicesClient(config.ArmEndpoint, credentials)
{
SubscriptionId = config.SubscriptionId,
};
}
}
public class ArmClientCredentials : ServiceClientCredentials
{
private readonly AuthenticationContext _authenticationContext;
private readonly Uri _customerArmAadAudience;
private readonly ClientCredential _clientCredential;
public ArmClientCredentials(ConfigWrapper config)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
var authority = config.AadEndpoint.AbsoluteUri + config.AadTenantId;
_authenticationContext = new AuthenticationContext(authority);
_customerArmAadAudience = config.ArmAadAudience;
_clientCredential = new ClientCredential(config.AadClientId, config.AadSecret);
}
public async override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var token = await _authenticationContext.AcquireTokenAsync(_customerArmAadAudience.OriginalString, _clientCredential);
request.Headers.Authorization = new AuthenticationHeaderValue(token.AccessTokenType, token.AccessToken);
await base.ProcessHttpRequestAsync(request, cancellationToken);
}
}
}