예제 #1
0
        protected async Task ImportImageAsync(string destTagName, string destRegistryName, string srcTagName,
                                              string?srcRegistryName = null, string?srcResourceId = null, ImportSourceCredentials?sourceCredentials = null)
        {
            AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
                Options.ServicePrincipal.ClientId,
                Options.ServicePrincipal.Secret,
                Options.ServicePrincipal.Tenant,
                AzureEnvironment.AzureGlobalCloud);
            IAzure azure = AzureManagementFactory.CreateAzureManager(credentials, Options.Subscription);

            ImportImageParametersInner importParams = new ImportImageParametersInner()
            {
                Mode   = "Force",
                Source = new ImportSource(
                    srcTagName,
                    srcResourceId,
                    srcRegistryName,
                    sourceCredentials),
                TargetTags = new string[] { destTagName }
            };

            LoggerService.WriteMessage($"Importing '{destTagName}' from '{srcTagName}'");

            if (!Options.IsDryRun)
            {
                try
                {
                    await RetryHelper.GetWaitAndRetryPolicy <Exception>(LoggerService)
                    .ExecuteAsync(() => azure.ContainerRegistries.Inner.ImportImageAsync(Options.ResourceGroup, destRegistryName, importParams));
                }
                catch (Exception e)
                {
                    string errorMsg = $"Importing Failure: {destTagName}";
                    if (e is CloudException cloudException)
                    {
                        errorMsg += Environment.NewLine + cloudException.Body.Message;
                    }

                    errorMsg += Environment.NewLine + e.ToString();

                    LoggerService.WriteMessage(errorMsg);

                    throw;
                }
            }
        }
        protected async Task ImportImageAsync(string destTagName, string srcTagName, string srcRegistryName = null, string srcResourceId = null)
        {
            AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
                Options.ServicePrincipal.ClientId,
                Options.ServicePrincipal.Secret,
                Options.ServicePrincipal.Tenant,
                AzureEnvironment.AzureGlobalCloud);
            IAzure azure = AzureManagementFactory.CreateAzureManager(credentials, Options.Subscription);

            ImportImageParametersInner importParams = new ImportImageParametersInner()
            {
                Mode   = "Force",
                Source = new ImportSource(
                    srcTagName,
                    srcResourceId,
                    srcRegistryName),
                TargetTags = new string[] { destTagName }
            };

            LoggerService.WriteMessage($"Importing '{destTagName}' from '{srcTagName}'");

            if (!Options.IsDryRun)
            {
                try
                {
                    AsyncPolicy <HttpResponseMessage> policy = HttpPolicyBuilder.Create()
                                                               .WithMeteredRetryPolicy(LoggerService)
                                                               .Build();
                    await policy.ExecuteAsync(async() =>
                    {
                        await azure.ContainerRegistries.Inner.ImportImageAsync(Options.ResourceGroup, RegistryName, importParams);
                        return(null);
                    });
                }
                catch (Exception e)
                {
                    LoggerService.WriteMessage($"Importing Failure: {destTagName}{Environment.NewLine}{e}");
                    throw;
                }
            }
        }