Skip to content

Commit

Permalink
save progress
Browse files Browse the repository at this point in the history
  • Loading branch information
melindawangmsft committed Aug 4, 2023
1 parent c91e07e commit 32dcbe4
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
34 changes: 34 additions & 0 deletions ResetOptionsBinder.cs
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
};

}
}
70 changes: 70 additions & 0 deletions ams/ResetCommand.cs
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);
}
}
}
}
}
13 changes: 13 additions & 0 deletions contracts/ResetOptions.cs
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
);

}

0 comments on commit 32dcbe4

Please sign in to comment.