Esempio n. 1
0
        private async Task ProcessAllVirtualMachinesActionAsync(IDialogContext context, LuisResult result,
                                                                Operations operation, ResumeAfter <AllVirtualMachinesFormState> resume)
        {
            EntityRecommendation resourceGroupEntity;

            var accessToken = await context.GetAccessToken(resourceId.Value);

            if (string.IsNullOrEmpty(accessToken))
            {
                return;
            }

            var subscriptionId = context.GetSubscriptionId();
            var availableVMs   = (await new VMDomain().ListVirtualMachinesAsync(accessToken, subscriptionId)).ToList();

            // retrieve the list of VMs that are in the correct power state
            var validPowerStates = VirtualMachineHelper.RetrieveValidPowerStateByOperation(operation);
            IEnumerable <VirtualMachine> candidateVMs = null;

            if (result.TryFindEntity("ResourceGroup", out resourceGroupEntity))
            {
                // obtain the name specified by the user - text in LUIS result is different
                var resourceGroup = resourceGroupEntity.GetEntityOriginalText(result.Query);

                candidateVMs = availableVMs.Where(vm => vm.ResourceGroup.Equals(resourceGroup, StringComparison.InvariantCultureIgnoreCase)).ToList();

                if (candidateVMs == null || !candidateVMs.Any())
                {
                    var operationText = VirtualMachineHelper.RetrieveOperationTextByOperation(operation);
                    await context.PostAsync($"The {resourceGroup} resource group doesn't contain VMs or doesn't exist in the current subscription.");

                    context.Done <string>(null);
                    return;
                }

                candidateVMs = candidateVMs.Where(vm => validPowerStates.Contains(vm.PowerState)).ToList();

                if (candidateVMs == null || !candidateVMs.Any())
                {
                    var operationText = VirtualMachineHelper.RetrieveOperationTextByOperation(operation);
                    await context.PostAsync($"No virtual machines that can be {operationText} were found in the {resourceGroup} resource group of the current subscription.");

                    context.Done <string>(null);
                    return;
                }
            }
            else
            {
                candidateVMs = availableVMs.Where(vm => validPowerStates.Contains(vm.PowerState)).ToList();

                if (!candidateVMs.Any())
                {
                    var operationText = VirtualMachineHelper.RetrieveOperationTextByOperation(operation);
                    await context.PostAsync($"No virtual machines that can be {operationText} were found in the current subscription.");

                    context.Done <string>(null);
                    return;
                }
            }

            // prompt the user to select a VM from the list
            var form = new FormDialog <AllVirtualMachinesFormState>(
                new AllVirtualMachinesFormState(candidateVMs, operation),
                VMForms.BuildAllVirtualMachinesForm,
                FormOptions.PromptInStart,
                null);

            context.Call(form, resume);
        }
Esempio n. 2
0
        private async Task ProcessVirtualMachineActionAsync(IDialogContext context, LuisResult result,
                                                            Operations operation, ResumeAfter <VirtualMachineFormState> resume)
        {
            EntityRecommendation virtualMachineEntity;

            // retrieve the list virtual machines from the subscription
            var accessToken = await context.GetAccessToken(resourceId.Value);

            if (string.IsNullOrEmpty(accessToken))
            {
                return;
            }

            var subscriptionId = context.GetSubscriptionId();
            var availableVMs   = (await new VMDomain().ListVirtualMachinesAsync(accessToken, subscriptionId)).ToList();

            // check if the user specified a virtual machine name in the command
            if (result.TryFindEntity("VirtualMachine", out virtualMachineEntity))
            {
                // obtain the name specified by the user - text in LUIS result is different
                var virtualMachineName = virtualMachineEntity.GetEntityOriginalText(result.Query);

                // ensure that the virtual machine exists in the subscription
                var selectedVM = availableVMs.FirstOrDefault(p => p.Name.Equals(virtualMachineName, StringComparison.InvariantCultureIgnoreCase));
                if (selectedVM == null)
                {
                    await context.PostAsync($"The '{virtualMachineName}' virtual machine was not found in the current subscription.");

                    context.Done <string>(null);
                    return;
                }

                // ensure that the virtual machine is in the correct power state for the requested operation
                if ((operation == Operations.Start && (selectedVM.PowerState == VirtualMachinePowerState.Starting || selectedVM.PowerState == VirtualMachinePowerState.Running)) ||
                    (operation == Operations.Shutdown && (selectedVM.PowerState == VirtualMachinePowerState.Stopping || selectedVM.PowerState == VirtualMachinePowerState.Stopped)) ||
                    (operation == Operations.Stop && (selectedVM.PowerState == VirtualMachinePowerState.Deallocating || selectedVM.PowerState == VirtualMachinePowerState.Deallocated)))
                {
                    var powerState = selectedVM.PowerState.ToString().ToLower();
                    await context.PostAsync($"The '{virtualMachineName}' virtual machine is already {powerState}.");

                    context.Done <string>(null);
                    return;
                }

                virtualMachineEntity.Entity = selectedVM.Name;
            }

            // retrieve the list of VMs that are in the correct power state
            var validPowerStates = VirtualMachineHelper.RetrieveValidPowerStateByOperation(operation);

            var candidateVMs = availableVMs.Where(vm => validPowerStates.Contains(vm.PowerState)).ToList();

            if (candidateVMs.Any())
            {
                // prompt the user to select a VM from the list
                var form = new FormDialog <VirtualMachineFormState>(
                    new VirtualMachineFormState(candidateVMs, operation),
                    VMForms.BuildVirtualMachinesForm,
                    FormOptions.PromptInStart,
                    result.Entities);

                context.Call(form, resume);
            }
            else
            {
                var operationText = VirtualMachineHelper.RetrieveOperationTextByOperation(operation);
                await context.PostAsync($"No virtual machines that can be {operationText} were found in the current subscription.");

                context.Done <string>(null);
                //context.Wait(this.MessageReceived);
            }
        }