-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c91e07e
commit 32dcbe4
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace AMSMigrate | ||
{ | ||
internal class ResetOptionsBinder | ||
{ | ||
private readonly Option<string> _sourceAccount = new Option<string>( | ||
aliases: new[] { "--source-account-name", "-n" }, | ||
description: "Azure Media Services Account.") | ||
{ | ||
IsRequired = true, | ||
Arity = ArgumentArity.ExactlyOne | ||
}; | ||
|
||
private readonly Option<bool> _all = new Option<bool>( | ||
aliases: new[] { "--all", "-a" }, | ||
description: "Reset all assets in the account, regardless of their migration status.") | ||
{ | ||
IsRequired = false | ||
}; | ||
private readonly Option<bool> _failed = new Option<bool>( | ||
aliases: new[] { "--failed", "-f" }, | ||
description: "Reset the failed migrated assets in the account, This is the default setting.") | ||
{ | ||
IsRequired = false | ||
}; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using AMSMigrate.Ams; | ||
using AMSMigrate.Contracts; | ||
using Azure; | ||
using Azure.Core; | ||
using Azure.ResourceManager.Media; | ||
using Azure.Storage.Blobs; | ||
using Microsoft.Extensions.Logging; | ||
using Spectre.Console; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace AMSMigrate.ams | ||
{ | ||
internal class ResetCommand : BaseMigrator | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly ResetOptions _options; | ||
private readonly IMigrationTracker<BlobContainerClient, AssetMigrationResult> _tracker; | ||
public ResetCommand(GlobalOptions globalOptions, | ||
ResetOptions resetOptions, | ||
IAnsiConsole console, | ||
TokenCredential credential, | ||
IMigrationTracker<BlobContainerClient, AssetMigrationResult> tracker, | ||
ILogger<ResetCommand> logger) | ||
: base(globalOptions, console, credential) | ||
{ | ||
_options = resetOptions; | ||
_logger = logger; | ||
_tracker = tracker; | ||
} | ||
|
||
public override async Task MigrateAsync(CancellationToken cancellationToken) | ||
{ | ||
var account = await GetMediaAccountAsync(_options.AccountName, cancellationToken); | ||
_logger.LogInformation("Begin cleaning up on account: {name}", account.Data.Name); | ||
|
||
var storage = await _resourceProvider.GetStorageAccountAsync(account, cancellationToken); | ||
var totalAssets = await QueryMetricAsync( | ||
account.Id.ToString(), | ||
"AssetCount", | ||
cancellationToken: cancellationToken); | ||
|
||
_logger.LogInformation("The total asset count of the media account is {count}.", totalAssets); | ||
AsyncPageable<MediaAssetResource> assets = account.GetMediaAssets() | ||
.GetAllAsync(cancellationToken: cancellationToken); | ||
List<MediaAssetResource>? assetList = await assets.ToListAsync(cancellationToken); | ||
|
||
foreach (var asset in assetList) | ||
{ | ||
var container = storage.GetContainer(asset); | ||
if (!await container.ExistsAsync(cancellationToken)) | ||
{ | ||
_logger.LogWarning("Container {name} missing for asset {asset}", container.Name, asset.Data.Name); | ||
return; | ||
} | ||
|
||
// The asset container exists, try to check the metadata list first. | ||
|
||
if (_options.all || (_tracker.GetMigrationStatusAsync(container, cancellationToken).Result.Status == MigrationStatus.Failed)) | ||
{ | ||
container.DeleteBlobAsync(cancellationToken); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
| ||
namespace AMSMigrate.Contracts | ||
{ | ||
/// <summary> | ||
/// It holds the options for cleanup commands. | ||
/// </summary> | ||
public record ResetOptions( | ||
string AccountName, | ||
bool all, | ||
bool failed | ||
); | ||
|
||
} |