示例#1
0
        static async Task <SkillResponse> HandleCreateResourceIntent(IntentRequest request, Session session,
                                                                     IAsyncCollector <AzureResourceToDeploy> deployQueue, ILogger log)
        {
            SkillResponse response;

            if (request.Intent.Name == Intents.YesIntent)
            {
                var azureResourceType = JsonConvert.DeserializeObject <AzureResourceType>(session.Attributes[Slots.AzureResourceType].ToString());
                var arName            = session.Attributes.ContainsKey(Slots.AzureResourceName) ? (string)session.Attributes[Slots.AzureResourceName] : null;

                var reprompt = new Reprompt {
                    OutputSpeech = new PlainTextOutputSpeech {
                        Text = "Vuoi creare un altro servizio?"
                    }
                };

                session.Attributes.Remove(Slots.AzureResourceType);
                session.Attributes.Remove(Slots.AzureResourceName);
                session.Attributes["state"] = States.AskForAnotherDeploy;

                response = ResponseBuilder.Ask(arName is null ? $"<p>OK, creo la risorsa <s>{azureResourceType.Name}</s></p> <p>Ti avviserò con una notifica appena terminato!</p>".ToSpeech() :
                                               $"<p>OK, creo la risorsa <s>{azureResourceType.Name}</s> che si chiama <s>{arName}</s></p> <p>Ti avviserò con una notifica appena terminato!</p>".ToSpeech(), reprompt, session);

                var azureResourceToDeploy = new AzureResourceToDeploy
                {
                    AzureResource = new AzureResource
                    {
                        Type = azureResourceType,
                        Name = arName
                    },
                    RequestedByUser = session.User.UserId
                };

                await deployQueue.AddAsync(azureResourceToDeploy);
            }
            else
            {
                var reprompt = new Reprompt
                {
                    OutputSpeech = new PlainTextOutputSpeech
                    {
                        Text = "Che tipo di risorsa desideri creare?"
                    }
                };

                session.Attributes.Clear();
                response = ResponseBuilder.Ask("Ah ok, forse allora ho capito male. <p>Cosa desidere creare?</p>".ToSpeech(), reprompt, session);
            }

            return(response);
        }
示例#2
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);
            }
        }