Beispiel #1
0
        public override void Execute()
        {
            var createMultipleVms = new CreateMultipleVms(Context);

            createMultipleVms.Execute();

            var rg = new AzureResourceManagerClient().ResourceGroup(Context.SubscriptionId, Context.RgName).Get().Value;

            //set tags on random vms
            Random rand = new Random(Environment.TickCount);

            foreach (var generic in rg.VirtualMachines().ListByName(Environment.UserName))
            {
                var vm = VirtualMachineOperations.FromGeneric(generic);
                if (rand.NextDouble() > 0.5)
                {
                    Console.WriteLine("adding tag to {0}", vm.Id.Name);
                    vm.StartAddTag("tagkey", "tagvalue");
                }
            }

            var filteredList = rg.VirtualMachines().List().Where(vm =>
            {
                string value;
                return(vm.Data.Tags.TryGetValue("tagkey", out value) && value == "tagvalue");
            });

            foreach (var vm in filteredList)
            {
                Console.WriteLine("--------Stopping VM {0}--------", vm.Id.Name);
                vm.PowerOff();
                Console.WriteLine("--------Starting VM {0}--------", vm.Id.Name);
                vm.PowerOn();
            }
        }
        public override void Execute()
        {
            var createVm = new CreateSingleVmExample(Context);

            createVm.Execute();

            var rgOp = new AzureResourceManagerClient().ResourceGroup(Context.SubscriptionId, Context.RgName);

            foreach (var genericOp in rgOp.VirtualMachines().ListByName(Context.VmName))
            {
                Console.WriteLine($"Deleting {genericOp.Id}");
                genericOp.Delete();
            }

            try
            {
                var vmOp = rgOp.VirtualMachine(Context.VmName);
                Console.WriteLine($"Trying to get {vmOp.Id}");
                var response = vmOp.Get();
            }
            catch (RequestFailedException e) when(e.Status == 404)
            {
                Console.WriteLine("Got 404 returned as expected");
                return;
            }

            throw new InvalidOperationException("Failed");
        }
Beispiel #3
0
        public override void Execute()
        {
            var createVm = new CreateSingleVmExample(Context);

            createVm.Execute();

            var rgOp = new AzureResourceManagerClient().ResourceGroup(Context.SubscriptionId, Context.RgName);

            foreach (var entity in rgOp.VirtualMachines().List())
            {
                Console.WriteLine($"{entity.Id.Name}");
                entity.StartAddTag("name", "Value");
            }
        }
Beispiel #4
0
        public override void Execute()
        {
            var createMultipleVms = new CreateMultipleVms(Context);

            createMultipleVms.Execute();

            var client = new AzureResourceManagerClient();

            foreach (var sub in client.Subscriptions().List())
            {
                var vmList = sub.ListVirtualMachines();
                foreach (var vm in vmList.Where(armResource => armResource.Data.Name.Contains("-o")))
                {
                    Console.WriteLine($"In subscription list: Stopping {vm.Id}");
                    vm.PowerOff();
                    Console.WriteLine($"In subscription list: Starting {vm.Id}");
                    vm.PowerOn();
                }
            }

            var resourceGroup = new AzureResourceManagerClient().ResourceGroup(Context.SubscriptionId, Context.RgName);

            resourceGroup.VirtualMachines().List().Select(vm =>
            {
                var parts = vm.Id.Name.Split('-');
                var n     = Convert.ToInt32(parts[0].Last());
                return(vm, n);
            })
            .Where(tuple => tuple.n % 2 == 0)
            .ToList()
            .ForEach(tuple =>
            {
                Console.WriteLine($"In resource group list: Stopping {tuple.vm.Id.Name}");
                tuple.vm.PowerOff();
                Console.WriteLine($"In resource group list: Starting {tuple.vm.Id.Name}");
                tuple.vm.PowerOn();
            });
        }
        public override void Execute()
        {
            var createVm = new CreateSingleVmExample(Context);

            createVm.Execute();

            var rgOp = new AzureResourceManagerClient().ResourceGroup(Context.SubscriptionId, Context.RgName);

            foreach (var genericOp in rgOp.VirtualMachines().ListByName(Context.VmName))
            {
                Console.WriteLine($"Adding tag to {genericOp.Id}");
                genericOp.StartAddTag("tagKey", "tagVaue");
            }

            var vmOp = rgOp.VirtualMachine(Context.VmName);

            Console.WriteLine($"Getting {vmOp.Id}");
            var vm = vmOp.Get().Value;

            if (!vm.Data.Tags.ContainsKey("tagKey"))
            {
                throw new InvalidOperationException("Failed");
            }
        }
        public override void Execute()
        {
            var createMultipleVms = new CreateMultipleVms(Context);

            createMultipleVms.Execute();

            var rg = new AzureResourceManagerClient().ResourceGroup(Context.SubscriptionId, Context.RgName).Get().Value;

            foreach (var availabilitySet in rg.AvailabilitySets().ListByName(Environment.UserName))
            {
                Console.WriteLine($"--------AvailabilitySet operation id--------: {availabilitySet.Id}");
            }

            foreach (var availabilitySet in rg.AvailabilitySets().ListByNameExpanded(Environment.UserName))
            {
                Console.WriteLine($"--------AvailabilitySet id--------: {availabilitySet.Data.Id}");
            }

            foreach (var vm in rg.VirtualMachines().ListByName(Environment.UserName))
            {
                Console.WriteLine($"--------VM operation id--------: {vm.Id}");
            }

            foreach (var vm in rg.VirtualMachines().ListByNameExpanded(Environment.UserName))
            {
                Console.WriteLine($"--------VM id--------: {vm.Data.Id}");
            }

            foreach (var networkInterface in rg.NetworkInterfaces().ListByName(Environment.UserName))
            {
                Console.WriteLine($"--------NetworkInterface operation id--------: {networkInterface.Id}");
            }

            foreach (var networkInterface in rg.NetworkInterfaces().ListByNameExpanded(Environment.UserName))
            {
                Console.WriteLine($"--------NetworkInterface id--------: {networkInterface.Data.Id}");
            }

            foreach (var networkSecurityGroup in rg.NetworkSecurityGroups().ListByName(Environment.UserName))
            {
                Console.WriteLine($"--------NetworkSecurityGroup operation id--------: {networkSecurityGroup.Id}");
            }

            foreach (var networkSecurityGroup in rg.NetworkSecurityGroups().ListByNameExpanded(Environment.UserName))
            {
                Console.WriteLine($"--------NetworkSecurityGroup id--------: {networkSecurityGroup.Data.Id}");
            }

            foreach (var publicIpAddress in rg.PublicIpAddresses().ListByName(Environment.UserName))
            {
                Console.WriteLine($"--------PublicIpAddress operation id--------: {publicIpAddress.Id}");
            }

            foreach (var publicIpAddress in rg.NetworkSecurityGroups().ListByNameExpanded(Environment.UserName))
            {
                Console.WriteLine($"--------PublicIpAddress id--------: {publicIpAddress.Data.Id}");
            }

            foreach (var VNet in rg.VirtualNetworks().ListByName(Environment.UserName))
            {
                Console.WriteLine($"--------VNet operation id--------: {VNet.Id}");
            }

            foreach (var VNet in rg.VirtualNetworks().ListByNameExpanded(Environment.UserName))
            {
                Console.WriteLine($"--------VNet id--------: {VNet.Data.Id}");
            }
            ExecuteAsync(rg).GetAwaiter().GetResult();
        }