Exemple #1
0
        /// <summary>
        /// Performs the execution of the activity.
        /// </summary>
        /// <param name="context">The execution context under which the activity executes.</param>
        protected override void Execute(CodeActivityContext context)
        {
            var azureSubscription = new AzureSubscription(SettingsPath, SubscriptionId);

            var credentials = new CertificateCloudCredentials(
                SubscriptionId,
                new X509Certificate2(Convert.FromBase64String(azureSubscription.ManagementCertificate)));

            var tasks = VirtualMachines.Get(context).Select(
                async vm =>
            {
                using (var client = new ComputeManagementClient(credentials))
                {
                    switch (vm.Size)
                    {
                    case VirtualMachineSize.Stop:
                        await client.DeallocateVmAsync(vm.Name);
                        break;

                    case VirtualMachineSize.Small:
                    case VirtualMachineSize.ExtraSmall:
                    case VirtualMachineSize.Large:
                    case VirtualMachineSize.Medium:
                    case VirtualMachineSize.ExtraLarge:
                    case VirtualMachineSize.A5:
                    case VirtualMachineSize.A6:
                    case VirtualMachineSize.A7:
                    case VirtualMachineSize.A8:
                    case VirtualMachineSize.A9:
                        await client.ResizeVmAsync(vm.Name, vm.Size.GetEnumDescription());
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(
                            nameof(context),
                            @"Unknown VM Size, this shouldn't happen, but the enumeration value isn't implemented in the acitivity switch");
                    }
                }
            });

            Task.WhenAll(tasks);
        }