Пример #1
0
        public async Task Run([QueueTrigger("azure-resource-deploy", Connection = "AzureWebJobsStorage")] AzureResourceToDeploy ard,
                              ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {ard.AzureResource.Type.Name}");

            var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage", EnvironmentVariableTarget.Process);
            //var tableName = Environment.GetEnvironmentVariable("deployLog", EnvironmentVariableTarget.Process);
            var serilog = new LoggerConfiguration()
                          .WriteTo.AzureTableStorage(connectionString, storageTableName: "AldoDeployerLog")
                          .CreateLogger();

            try
            {
                var deployOptions = new DeploymentOptions
                {
                    Region                   = Region.EuropeWest,
                    ResourceGroupName        = "TestCodemotionRome19",
                    UseExistingResourceGroup = true,
                    SubscriptionId           = configuration.SubscriptionId
                };

                var azure = await azureService.Authenticate(configuration.ClientId, configuration.ClientSecret, configuration.TenantId);

                var deployResult = await deploymentService.Deploy(azure, deployOptions, ard.AzureResource);

                const string failed  = "è fallito";
                const string success = "è andato a buon fine";

                var notificationMessage = ard.AzureResource.Name.IsNullOrWhiteSpace() ?
                                          $"Aldo. <p>Il deploy della risorsa <s>{ard.AzureResource.Type.Name}</s> {(deployResult.IsSuccess ? success : failed)}</p>" :
                                          $"Aldo. <p>Il deploy della risorsa <s>{ard.AzureResource.Type.Name}</s> <s>{ard.AzureResource.Name}</s> {(deployResult.IsSuccess ? success : failed)}</p>";

                var notificationResult = await notificationService.SendUserNotification(ard.RequestedByUser, notificationMessage);

                if (notificationResult.IsFailure)
                {
                    serilog.Error(notificationResult.Error);
                }
            }
            catch (Exception e)
            {
                var error = $"{e.Message}\n\r{e.StackTrace}";
                log.LogError(error);
                serilog.Error(error);
            }
        }