public override void Execute()
        {
            var client = new AzureResourceManagerClient(Context.SubscriptionId, new DefaultAzureCredential());

            var sub = client.DefaultSubscription;

            if (sub.Data.SubscriptionId != Context.SubscriptionId)
            {
                throw new Exception($"Didn't get back expected subscription.  Got {sub.Data.SubscriptionId} expected {Context.SubscriptionId}");
            }

            Console.WriteLine("Found correct subscription");

            client = new AzureResourceManagerClient();

            sub = client.DefaultSubscription;

            if (sub.Data.SubscriptionId != Context.SubscriptionId)
            {
                throw new Exception($"Didn't get back expected subscription.  Got {sub.Data.SubscriptionId} expected {Context.SubscriptionId}");
            }

            Console.WriteLine("Found correct subscription");
        }
        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 client       = new AzureResourceManagerClient();
            var subscription = client.GetSubscriptionOperations(Context.SubscriptionId);

            // Create Resource Group
            Console.WriteLine($"--------Start create group {Context.RgName}--------");
            var resourceGroup = subscription.GetResourceGroupContainer().Construct(Context.Loc).CreateOrUpdate(Context.RgName).Value;

            CleanUp.Add(resourceGroup.Id);

            // Create AvailabilitySet
            Console.WriteLine("--------Start create AvailabilitySet--------");
            var aset = resourceGroup.GetAvailabilitySetContainer().Construct("Aligned").CreateOrUpdate(Context.VmName + "_aSet").Value;

            if (aset.Data.Location != Context.Loc)
            {
                throw new Exception($"aset was on the wrong location expected {Context.Loc} actual {aset.Data.Location}");
            }

            // Create VNet
            Console.WriteLine("--------Start create VNet--------");
            string vnetName = Context.VmName + "_vnet";
            var    vnet     = resourceGroup.GetVirtualNetworkContainer().Construct("10.0.0.0/16").CreateOrUpdate(vnetName).Value;

            if (vnet.Data.Location != Context.Loc)
            {
                throw new Exception($"vnet was on the wrong location expected {Context.Loc} actual {vnet.Data.Location}");
            }

            //create subnet
            Console.WriteLine("--------Start create Subnet async--------");
            var subnet = vnet.GetSubnetContainer().Construct("10.0.0.0/24").CreateOrUpdateAsync(Context.SubnetName).ConfigureAwait(false).GetAwaiter().GetResult().Value;

            //create network security group
            Console.WriteLine("--------Start create NetworkSecurityGroup--------");
            var nsg = resourceGroup.GetNetworkSecurityGroupContainer().Construct(80).CreateOrUpdate(Context.NsgName).Value;

            if (nsg.Data.Location != Context.Loc)
            {
                throw new Exception($"nsg was on the wrong location expected {Context.Loc} actual {nsg.Data.Location}");
            }

            // Create Network Interface
            Console.WriteLine("--------Start create Network Interface--------");
            var nic = resourceGroup.GetNetworkInterfaceContainer().Construct(subnet.Id).CreateOrUpdate($"{Context.VmName}_nic").Value;

            if (nic.Data.Location != Context.Loc)
            {
                throw new Exception($"nic was on the wrong location expected {Context.Loc} actual {nic.Data.Location}");
            }

            // Create VM
            Console.WriteLine("--------Start create VM--------");
            var vm = resourceGroup.GetVirtualMachineContainer().Construct(Context.Hostname, "admin-user", "!@#$%asdfA", nic.Id, aset.Id).CreateOrUpdate(Context.VmName).Value;

            if (vm.Data.Location != Context.Loc)
            {
                throw new Exception($"vm was on the wrong location expected {Context.Loc} actual {vm.Data.Location}");
            }

            Console.WriteLine("VM ID: " + vm.Id);
            Console.WriteLine("--------Done create VM--------");
        }
Exemple #4
0
        /// <summary>
        /// Gets the NetworkSecurityGroupOperations.
        /// </summary>
        /// <param name="client"> The <see cref="AzureResourceManagerClient" /> instance the method will execute against. </param>
        /// <param name="resourceId"> The ResourceIdentifier of the resource that is the target of operations. </param>
        /// <returns> Returns an object representing the operations that can be performed over a specific <see cref="NetworkSecurityGroup" />. </returns>
        /// <exception cref="ArgumentException"> ResourceIdentifier provided is not for a NetworkSecurityGroup. </exception>
        /// <exception cref="ArgumentNullException"> ResourceIdentifier cannot be null. </exception>
        public static NetworkSecurityGroupOperations GetNetworkSecurityGroupOperations(this AzureResourceManagerClient client, ResourceIdentifier resourceId)
        {
            if (resourceId is null)
            {
                throw new ArgumentNullException(nameof(resourceId), "${nameof(resourceId)} cannot be null.");
            }
            if (resourceId.Type != NetworkSecurityGroupOperations.ResourceType)
            {
                throw new ArgumentException(nameof(resourceId.Type), "${nameof(resourceId.Type)} provided is not for a NetworkSecurityGroup.");
            }

            var subOps = client.GetSubscriptionOperations(resourceId.Subscription);
            var rgOps  = subOps.GetResourceGroupOperations(resourceId.ResourceGroup);

            return(rgOps.GetNetworkSecurityGroupOperations(resourceId.Name));
        }
        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();
        }
Exemple #6
0
        private async System.Threading.Tasks.Task ExecuteAsync()
        {
            var client       = new AzureResourceManagerClient();
            var subscription = client.GetSubscriptionOperations(Context.SubscriptionId);

            // Create Resource Group
            Console.WriteLine($"--------Start create group {Context.RgName}--------");
            var resourceGroup = subscription.GetResourceGroupContainer().Construct(Context.Loc).CreateOrUpdate(Context.RgName).Value;

            CleanUp.Add(resourceGroup.Id);
            var rgOps = subscription.GetResourceGroupOperations(Context.RgName);

            ShouldThrow <ArgumentException>(
                () => rgOps.AddTag("", ""),
                "AddTag with empty string didn't throw",
                "AddTag");

            await ShouldThrowAsync <ArgumentException>(
                async() => await rgOps.AddTagAsync(null, null),
                "AddTagAsync with null string didn't throw",
                "AddTagAsync");

            ShouldThrow <ArgumentException>(
                () => rgOps.StartAddTag("", null),
                "StartAddTag with empty string didn't throw",
                "StartAddTag");

            await ShouldThrowAsync <ArgumentException>(
                async() => await rgOps.StartAddTagAsync(" ", "test"),
                "StartAddTagAsync with whitespaces only string didn't throw",
                "StartAddTagAsync");

            // Create AvailabilitySet
            Console.WriteLine("--------Create AvailabilitySet async--------");
            var aset = (await(await resourceGroup.GetAvailabilitySetContainer().Construct("Aligned").StartCreateOrUpdateAsync(Context.VmName + "_aSet")).WaitForCompletionAsync()).Value;
            var data = aset.Get().Value.Data;

            ShouldThrow <ArgumentException>(
                () => rgOps.CreateResource <AvailabilitySetContainer, AvailabilitySet, AvailabilitySetData>("", data),
                "CreateResource with empty string didn't throw",
                "CreateResource");

            await ShouldThrowAsync <ArgumentException>(
                async() => await rgOps.CreateResourceAsync <AvailabilitySetContainer, AvailabilitySet, AvailabilitySetData>(" ", data),
                "CreateResourceAsync with whitespaces string didn't throw",
                "CreateResourceAsync");

            ShouldThrow <ArgumentNullException>(
                () => rgOps.SetTags(null),
                "SetTags with null didn't throw",
                "SetTags");

            await ShouldThrowAsync <ArgumentNullException>(
                async() => await rgOps.SetTagsAsync(null),
                "SetTagsAsync with null didn't throw",
                "SetTagsAsync");

            ShouldThrow <ArgumentNullException>(
                () => rgOps.StartSetTags(null),
                "StartSetTags with null didn't throw",
                "StartSetTags");

            await ShouldThrowAsync <ArgumentNullException>(
                async() => await rgOps.StartSetTagsAsync(null),
                "StartSetTagsAsync with null didn't throw",
                "StartSetTagsAsync");

            ShouldThrow <ArgumentException>(
                () => rgOps.RemoveTag(""),
                "RemoveTag with empty string didn't throw",
                "RemoveTag");

            await ShouldThrowAsync <ArgumentException>(
                async() => await rgOps.RemoveTagAsync(null),
                "RemoveTagAsync with null didn't throw",
                "RemoveTagAsync");

            ShouldThrow <ArgumentException>(
                () => rgOps.StartRemoveTag(" "),
                "StartRemoveTag with whitespace string didn't throw",
                "StartRemoveTag");

            await ShouldThrowAsync <ArgumentException>(
                async() => await rgOps.StartRemoveTagAsync(null),
                "StartRemoveTagAsync with null didn't throw",
                "StartRemoveTagAsync");

            ShouldThrow <ArgumentNullException>(
                () => rgOps.CreateResource <AvailabilitySetContainer, AvailabilitySet, AvailabilitySetData>("tester", null),
                "CreateResource model exception not thrown",
                "CreateResource");

            await ShouldThrowAsync <ArgumentNullException>(
                async() => await rgOps.CreateResourceAsync <AvailabilitySetContainer, AvailabilitySet, AvailabilitySetData>("tester", null),
                "CreateResourceAsync model exception not thrown",
                "CreateResourceAsync");

            Console.WriteLine("--------Done--------");
        }
        public override void Execute()
        {
            var client       = new AzureResourceManagerClient();
            var subscription = client.GetSubscriptionOperations(Context.SubscriptionId);

            // Create Resource Group
            Console.WriteLine($"--------Start create group {Context.RgName}--------");
            var resourceGroup = subscription.GetResourceGroupContainer().Construct(Context.Loc).CreateOrUpdate(Context.RgName).Value;

            CleanUp.Add(resourceGroup.Id);
            Console.WriteLine("\nResource Group List Available Locations: ");
            var loc = resourceGroup.ListAvailableLocations();

            foreach (var l in loc)
            {
                Console.WriteLine(l.DisplayName);
            }

            // Create AvailabilitySet
            Console.WriteLine("--------Start create AvailabilitySet--------");
            var aset = resourceGroup.GetAvailabilitySetContainer().Construct("Aligned").CreateOrUpdate(Context.VmName + "_aSet").Value;

            Console.WriteLine("\nAvailability Set List Available Locations: ");
            loc = aset.ListAvailableLocations();
            foreach (var l in loc)
            {
                Console.WriteLine(l.DisplayName);
            }

            // Create VNet
            Console.WriteLine("--------Start create VNet--------");
            string vnetName = Context.VmName + "_vnet";
            var    vnet     = resourceGroup.GetVirtualNetworkContainer().Construct("10.0.0.0/16").CreateOrUpdate(vnetName).Value;

            Console.WriteLine("\nVirtual Network List Available Locations: ");
            loc = vnet.ListAvailableLocations();
            foreach (var l in loc)
            {
                Console.WriteLine(l.DisplayName);
            }

            //create subnet
            Console.WriteLine("--------Start create Subnet--------");
            var subnet = vnet.GetSubnetContainer().Construct("10.0.0.0/24").CreateOrUpdate(Context.SubnetName).Value;

            //create network security group
            Console.WriteLine("--------Start create NetworkSecurityGroup--------");
            var nsg = resourceGroup.GetNetworkSecurityGroupContainer().Construct(80).CreateOrUpdate(Context.NsgName).Value;

            Console.WriteLine("\nNetwork Security Group List Available Locations: ");
            loc = nsg.ListAvailableLocations();
            foreach (var l in loc)
            {
                Console.WriteLine(l.DisplayName);
            }

            // Create Network Interface
            Console.WriteLine("--------Start create Network Interface--------");
            var nic = resourceGroup.GetNetworkInterfaceContainer().Construct(subnet.Id).CreateOrUpdate($"{Context.VmName}_nic").Value;

            Console.WriteLine("\nNetwork Interface Container List Available Locations: ");
            loc = nic.ListAvailableLocations();
            foreach (var l in loc)
            {
                Console.WriteLine(l.DisplayName);
            }

            // Create VM
            Console.WriteLine("--------Start create VM--------");
            var vm = resourceGroup.GetVirtualMachineContainer().Construct(Context.Hostname, "admin-user", "!@#$%asdfA", nic.Id, aset.Id).CreateOrUpdate(Context.VmName).Value;

            Console.WriteLine("\nVirtual Machine List Available Locations: ");
            loc = vm.ListAvailableLocations();
            foreach (var l in loc)
            {
                Console.WriteLine(l.DisplayName);
            }

            Console.WriteLine("VM ID: " + vm.Id);
            Console.WriteLine("--------Done create VM--------");
        }
        public override void Execute()
        {
            var client       = new AzureResourceManagerClient();
            var subscription = client.Subscription(Context.SubscriptionId);

            // Create Resource Group
            Console.WriteLine($"--------Start create group {Context.RgName}--------");
            var resourceGroup = subscription.ResourceGroups().Create(Context.RgName, Context.Loc).Value;

            CleanUp.Add(resourceGroup.Id);

            Console.WriteLine("--------Start create Assignment--------");
            var input  = new RoleAssignmentCreateParameters($"/subscriptions/{Context.SubscriptionId}/resourceGroups/{Context.RgName}/providers/Microsoft.Authorization/roleDefinitions/{Context.RoleId}", Context.PrincipalId);
            var assign = resourceGroup.RoleAssignments().Create(Guid.NewGuid().ToString(), input).Value;

            Console.WriteLine("--------Done create Assignment--------");

            assign = assign.Get().Value;

            // Create AvailabilitySet
            Console.WriteLine("--------Start create AvailabilitySet--------");
            var aset = resourceGroup.AvailabilitySets().Construct("Aligned").Create(Context.VmName + "_aSet").Value;

            // Create VNet
            Console.WriteLine("--------Start create VNet--------");
            string vnetName = Context.VmName + "_vnet";
            var    vnet     = resourceGroup.VirtualNetworks().Construct("10.0.0.0/16").Create(vnetName).Value;

            //create subnet
            Console.WriteLine("--------Start create Subnet--------");
            var subnet = vnet.Subnets().Construct(Context.SubnetName, "10.0.0.0/24").Create(Context.SubnetName).Value;

            //create network security group
            Console.WriteLine("--------Start create NetworkSecurityGroup--------");
            _ = resourceGroup.NetworkSecurityGroups().Construct(Context.NsgName, 80).Create(Context.NsgName).Value;

            // Create IP Address
            Console.WriteLine("--------Start create IP Address--------");
            var ipAddress = resourceGroup.PublicIpAddresses().Construct().Create($"{Context.VmName}_ip").Value;

            // Create Network Interface
            Console.WriteLine("--------Start create Network Interface--------");
            var nic = resourceGroup.NetworkInterfaces().Construct(ipAddress.Data, subnet.Id).Create($"{Context.VmName}_nic").Value;

            // Create VM
            Console.WriteLine("--------Start create VM--------");
            var vm = resourceGroup.VirtualMachines().Construct(Context.VmName, "admin-user", "!@#$%asdfA", nic.Id, aset.Data).Create(Context.VmName).Value;

            Console.WriteLine("VM ID: " + vm.Id);
            Console.WriteLine("--------Done create VM--------");


            Console.WriteLine("--------Start create Assignment--------");
            var input2  = new RoleAssignmentCreateParameters($"{vm.Id}/providers/Microsoft.Authorization/roleDefinitions/{Context.RoleId}", Context.PrincipalId);
            var assign2 = vm.RoleAssignments().Create(Guid.NewGuid().ToString(), input2).Value;

            Console.WriteLine("--------Done create Assignment--------");

            assign2 = assign2.Get().Value;
            Console.WriteLine($"Created assignment: '{assign.Data.Id}'");
        }