public void Cleanup(params IDisposable[] disposables) { foreach (var disposable in disposables) { CleanUp.Add(disposable); } }
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; // 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; //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--------"); _ = resourceGroup.GetNetworkSecurityGroupContainer().Construct(80).CreateOrUpdate(Context.NsgName).Value; CreateVms(resourceGroup, aset, subnet); }
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("--------Start create Assignment--------"); var input = new RoleAssignmentCreateParameters($"/subscriptions/{Context.SubscriptionId}/resourceGroups/{Context.RgName}/providers/Microsoft.Authorization/roleDefinitions/{Context.RoleId}", Context.PrincipalId); var assign = resourceGroup.GetRoleAssignmentContainer().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.GetAvailabilitySetContainer().Construct("Aligned").CreateOrUpdate(Context.VmName + "_aSet").Value; // 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; //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--------"); _ = resourceGroup.GetNetworkSecurityGroupContainer().Construct(80).CreateOrUpdate(Context.NsgName).Value; // Create Network Interface Console.WriteLine("--------Start create Network Interface--------"); var nic = resourceGroup.GetNetworkInterfaceContainer().Construct(subnet.Id).CreateOrUpdate($"{Context.VmName}_nic").Value; // 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("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.GetRoleAssignmentContainer().Create(Guid.NewGuid().ToString(), input2).Value; Console.WriteLine("--------Done create Assignment--------"); assign2 = assign2.Get().Value; Console.WriteLine($"Created assignment: '{assign.Data.Id}'"); }
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); // 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--------"); }
private async System.Threading.Tasks.Task ExcuteAsync() { var client = new AzureResourceManagerClient(); var subscription = client.GetSubscriptionOperations(Context.SubscriptionId); // Create Resource Group Console.WriteLine($"--------Start StartCreate group {Context.RgName}--------"); var resourceGroup = (await(subscription.GetResourceGroupContainer().Construct(Context.Loc).StartCreateOrUpdate(Context.RgName)).WaitForCompletionAsync()).Value; CleanUp.Add(resourceGroup.Id); // Create AvailabilitySet Console.WriteLine("--------Start StartCreate AvailabilitySet async--------"); var aset = (await(resourceGroup.GetAvailabilitySetContainer().Construct("Aligned").StartCreateOrUpdate(Context.VmName + "_aSet")).WaitForCompletionAsync()).Value; // Create VNet Console.WriteLine("--------Start StartCreate VNet--------"); string vnetName = Context.VmName + "_vnet"; var vnet = (await(resourceGroup.GetVirtualNetworkContainer().Construct("10.0.0.0/16").StartCreateOrUpdate(vnetName)).WaitForCompletionAsync()).Value; //create subnet Console.WriteLine("--------Start StartCreate Subnet--------"); var subnet = (await(vnet.GetSubnetContainer().Construct("10.0.0.0/24").StartCreateOrUpdate(Context.SubnetName)).WaitForCompletionAsync()).Value; //create network security group Console.WriteLine("--------Start StartCreate NetworkSecurityGroup--------"); _ = (await(resourceGroup.GetNetworkSecurityGroupContainer().Construct(80).StartCreateOrUpdate(Context.NsgName)).WaitForCompletionAsync()).Value; // Create Network Interface Console.WriteLine("--------Start StartCreate Network Interface--------"); var nic = (await(resourceGroup.GetNetworkInterfaceContainer().Construct(subnet.Id).StartCreateOrUpdate($"{Context.VmName}_nic")).WaitForCompletionAsync()).Value; // Create VM Console.WriteLine("--------Start StartCreate VM --------"); var vm = (await(resourceGroup.GetVirtualMachineContainer().Construct(Context.Hostname, "admin-user", "!@#$%asdfA", nic.Id, aset.Id).StartCreateOrUpdate(Context.VmName)).WaitForCompletionAsync()).Value; Console.WriteLine("VM ID: " + vm.Id); Console.WriteLine("--------Done StartCreate VM--------"); }
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; // 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; //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--------"); _ = resourceGroup.GetNetworkSecurityGroupContainer().Construct(80).CreateOrUpdate(Context.NsgName).Value; // Create Network Interface Console.WriteLine("--------Start create Network Interface--------"); var nic = resourceGroup.GetNetworkInterfaceContainer().Construct(subnet.Id).CreateOrUpdate($"{Context.VmName}_nic").Value; // 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("VM ID: " + vm.Id); Console.WriteLine("--------Done create VM--------"); }
public override void Execute() { // Note that we need to create 3 clients from this scenario; customer must know which client contains which resource var rmClient = new ResourcesManagementClient(Context.SubscriptionId, Context.Credential); var computeClient = new ComputeManagementClient(Context.SubscriptionId, Context.Credential); var networkClient = new NetworkManagementClient(Context.SubscriptionId, Context.Credential); // Create Resource Group Console.WriteLine($"--------Start create group {Context.RgName}--------"); // note that some resources use the StartCreate and some use Create - this can make it a bit difficult to handle resource creation generically var resourceGroup = rmClient.ResourceGroups.CreateOrUpdate(Context.RgName, new ResourceGroup(Context.Loc)).Value; CleanUp.Add(resourceGroup.Id); // Create AvailabilitySet Console.WriteLine("--------Start create AvailabilitySet--------"); var aset = new AvailabilitySet(Context.Loc) { PlatformUpdateDomainCount = 5, PlatformFaultDomainCount = 2, Sku = new Azure.ResourceManager.Compute.Models.Sku() { Name = "Aligned" } }; aset = computeClient.AvailabilitySets.CreateOrUpdate(Context.RgName, Context.VmName + "_aSet", aset).Value; // Create VNet Console.WriteLine("--------Start create VNet--------"); string vnetName = Context.VmName + "_vnet"; var vnet = new VirtualNetwork() { Location = Context.Loc, AddressSpace = new AddressSpace() { AddressPrefixes = new List <string>() { "10.0.0.0/16" } } }; vnet = networkClient.VirtualNetworks.StartCreateOrUpdate(Context.RgName, vnetName, vnet).WaitForCompletionAsync().ConfigureAwait(false).GetAwaiter().GetResult().Value; //create subnet Console.WriteLine("--------Start create Subnet--------"); var nsg = new NetworkSecurityGroup { Location = Context.Loc }; nsg.SecurityRules = new List <SecurityRule> { new SecurityRule { Name = "Port80", Priority = 1001, Protocol = SecurityRuleProtocol.Tcp, Access = SecurityRuleAccess.Allow, Direction = SecurityRuleDirection.Inbound, SourcePortRange = "*", SourceAddressPrefix = "*", DestinationPortRange = "80", DestinationAddressPrefix = "*", Description = $"Port_80" } }; nsg = networkClient.NetworkSecurityGroups.StartCreateOrUpdate(Context.RgName, Context.NsgName, nsg).WaitForCompletionAsync().ConfigureAwait(false).GetAwaiter().GetResult().Value; var subnet = new Subnet() { Name = Context.SubnetName, AddressPrefix = "10.0.0.0/24", NetworkSecurityGroup = nsg }; subnet = networkClient.Subnets.StartCreateOrUpdate(Context.RgName, vnetName, Context.SubnetName, subnet).WaitForCompletionAsync().ConfigureAwait(false).GetAwaiter().GetResult().Value; // Create IP Address Console.WriteLine("--------Start create IP Address--------"); var ipAddress = new PublicIPAddress() { PublicIPAddressVersion = Azure.ResourceManager.Network.Models.IPVersion.IPv4.ToString(), PublicIPAllocationMethod = IPAllocationMethod.Dynamic, Location = Context.Loc, }; ipAddress = networkClient.PublicIPAddresses.StartCreateOrUpdate(Context.RgName, $"{Context.VmName}_ip", ipAddress).WaitForCompletionAsync().ConfigureAwait(false).GetAwaiter().GetResult().Value; // Create Network Interface Console.WriteLine("--------Start create Network Interface--------"); var nic = new NetworkInterface() { Location = Context.Loc, IpConfigurations = new List <NetworkInterfaceIPConfiguration>() { new NetworkInterfaceIPConfiguration() { Name = "Primary", Primary = true, Subnet = new Subnet() { Id = subnet.Id }, PrivateIPAllocationMethod = IPAllocationMethod.Dynamic, PublicIPAddress = new PublicIPAddress() { Id = ipAddress.Id } } } }; nic = networkClient.NetworkInterfaces.StartCreateOrUpdate(Context.RgName, $"{Context.VmName}_nic", nic).WaitForCompletionAsync().ConfigureAwait(false).GetAwaiter().GetResult().Value; // Create VM Console.WriteLine("--------Start create VM--------"); var vm = new VirtualMachine(Context.Loc) { NetworkProfile = new Azure.ResourceManager.Compute.Models.NetworkProfile { NetworkInterfaces = new[] { new NetworkInterfaceReference() { Id = nic.Id } } }, OsProfile = new OSProfile { ComputerName = Context.VmName, AdminUsername = "******", AdminPassword = "******", LinuxConfiguration = new LinuxConfiguration { DisablePasswordAuthentication = false, ProvisionVMAgent = true } }, StorageProfile = new StorageProfile() { ImageReference = new ImageReference() { Offer = "UbuntuServer", Publisher = "Canonical", Sku = "18.04-LTS", Version = "latest" }, DataDisks = new List <DataDisk>() }, HardwareProfile = new HardwareProfile() { VmSize = VirtualMachineSizeTypes.StandardB1Ms }, // The namespace-qualified type for SubResource is needed because all 3 libraries define an identical SubResource type. In the proposed model, the common type would be part of the core library AvailabilitySet = new Azure.ResourceManager.Compute.Models.SubResource() { Id = aset.Id } }; vm = computeClient.VirtualMachines.StartCreateOrUpdate(Context.RgName, Context.VmName, vm).WaitForCompletionAsync().ConfigureAwait(false).GetAwaiter().GetResult().Value; Console.WriteLine("VM ID: " + vm.Id); Console.WriteLine("--------Done create VM--------"); }
public override void Execute() { var rmClient = new ResourcesManagementClient(Context.SubscriptionId, Context.Credential); var computeClient = new ComputeManagementClient(Context.SubscriptionId, Context.Credential); var networkClient = new NetworkManagementClient(Context.SubscriptionId, Context.Credential); // Create Resource Group Console.WriteLine($"--------Start create group {Context.RgName}--------"); var resourceGroup = rmClient.ResourceGroups.CreateOrUpdate(Context.RgName, new ResourceGroup(Context.Loc)).Value; CleanUp.Add(resourceGroup.Id); // Create AvailabilitySet Console.WriteLine("--------Start create AvailabilitySet--------"); var aset = new AvailabilitySet(Context.Loc) { PlatformUpdateDomainCount = 5, PlatformFaultDomainCount = 2, Sku = new Azure.ResourceManager.Compute.Models.Sku() { Name = "Aligned" } }; aset = computeClient.AvailabilitySets.CreateOrUpdate(Context.RgName, Context.VmName + "_aSet", aset).Value; // Create VNet Console.WriteLine("--------Start create VNet--------"); string vnetName = Context.VmName + "_vnet"; var vnet = new VirtualNetwork() { Location = Context.Loc, AddressSpace = new AddressSpace() { AddressPrefixes = new List <string>() { "10.0.0.0/16" } } }; vnet = networkClient.VirtualNetworks.StartCreateOrUpdate(Context.RgName, vnetName, vnet).WaitForCompletionAsync().ConfigureAwait(false).GetAwaiter().GetResult().Value; //create subnet Console.WriteLine("--------Start create Subnet--------"); var nsg = new NetworkSecurityGroup { Location = Context.Loc }; nsg.SecurityRules = new List <SecurityRule> { new SecurityRule { Name = "Port80", Priority = 1001, Protocol = SecurityRuleProtocol.Tcp, Access = SecurityRuleAccess.Allow, Direction = SecurityRuleDirection.Inbound, SourcePortRange = "*", SourceAddressPrefix = "*", DestinationPortRange = "80", DestinationAddressPrefix = "*", Description = $"Port_80" } }; nsg = networkClient.NetworkSecurityGroups.StartCreateOrUpdate(Context.RgName, Context.NsgName, nsg).WaitForCompletionAsync().ConfigureAwait(false).GetAwaiter().GetResult().Value; var subnet = new Subnet() { Name = Context.SubnetName, AddressPrefix = "10.0.0.0/24", NetworkSecurityGroup = nsg }; subnet = networkClient.Subnets.StartCreateOrUpdate(Context.RgName, vnetName, Context.SubnetName, subnet).WaitForCompletionAsync().ConfigureAwait(false).GetAwaiter().GetResult().Value; CreateVmsAsync(resourceGroup, aset, subnet).ConfigureAwait(false).GetAwaiter().GetResult(); }
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--------"); }