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); }
/// <summary> /// Gets the AvailabilitySetOperations. /// </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="AvailabilitySet" />. </returns> /// <exception cref="ArgumentException"> ResourceIdentifier provided is not for an AvailabilitySet. </exception> public static AvailabilitySetOperations GetAvailabilitySetOperations(this AzureResourceManagerClient client, ResourceIdentifier resourceId) { if (resourceId.Type != AvailabilitySetOperations.ResourceType) { throw new ArgumentException("ResourceIdentifier provided is not for an AvailabilitySet."); } return(client.GetSubscriptionOperations(resourceId.Subscription).GetResourceGroupOperations(resourceId.ResourceGroup).GetAvailabilitySetOperations(resourceId.Name)); }
/// <summary> /// Gets the VirtualMachineOperations. /// </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="VirtualMachine" />. </returns> /// <exception cref="ArgumentException"> ResourceIdentifier provided is not for a VirtualMachine. </exception> public static VirtualMachineOperations GetVirtualMachineOperations(this AzureResourceManagerClient client, ResourceIdentifier resourceId) { if (resourceId.Type != VirtualMachineOperations.ResourceType) { throw new ArgumentException("ResourceIdentifier provided is not for a VirtualMachine."); } return(client.GetSubscriptionOperations(resourceId.Subscription).GetResourceGroupOperations(resourceId.ResourceGroup).GetVirtualMachineOperations(resourceId.Name)); }
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}'"); }
/// <summary> /// Gets the VirtualNetworkOperations. /// </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="VirtualNetwork" />. </returns> /// <exception cref="ArgumentException"> ResourceIdentifier provided is not for a VirtualNetwork. </exception> public static VirtualNetworkOperations GetVirtualNetworkOperations(this AzureResourceManagerClient client, ResourceIdentifier resourceId) { if (resourceId.Type != VirtualNetworkOperations.ResourceType) { throw new ArgumentException("ResourceIdentifier provided is not for a VirtualNetwork."); } var subOps = client.GetSubscriptionOperations(resourceId.Subscription); var rgOps = subOps.GetResourceGroupOperations(resourceId.ResourceGroup); return(rgOps.GetVirtualNetworkOperations(resourceId.Parent.Name)); }
/// <summary> /// Gets the PublicIpAddressOperations. /// </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="PublicIpAddress" />. </returns> /// <exception cref="ArgumentException"> ResourceIdentifier provided is not for a PublicIpAddress. </exception> public static PublicIpAddressOperations GetPublicIpAddressOperations(this AzureResourceManagerClient client, ResourceIdentifier resourceId) { if (resourceId.Type != PublicIpAddressOperations.ResourceType) { throw new ArgumentException("ResourceIdentifier provided is not for a PublicIpAddress."); } var subOps = client.GetSubscriptionOperations(resourceId.Subscription); var rgOps = subOps.GetResourceGroupOperations(resourceId.ResourceGroup); return(rgOps.GetPublicIpAddressOperations(resourceId.Name)); }
/// <summary> /// Gets the SubnetOperations. /// </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="Subnet" />. </returns> /// <exception cref="ArgumentException"> ResourceIdentifier provided is not for a Subnet. </exception> /// <exception cref="ArgumentNullException"> ResourceIdentifier cannot be null. </exception> public static SubnetOperations GetSubnetOperations(this AzureResourceManagerClient client, ResourceIdentifier resourceId) { if (resourceId is null) { throw new ArgumentNullException(nameof(resourceId), "${nameof(resourceId)} cannot be null."); } if (resourceId.Type != SubnetOperations.ResourceType) { throw new ArgumentException(nameof(resourceId.Type), "${nameof(resourceId.Type)} provided is not for a Subnet."); } var subOps = client.GetSubscriptionOperations(resourceId.Subscription); var rgOps = subOps.GetResourceGroupOperations(resourceId.ResourceGroup); var vnetOps = rgOps.GetVirtualNetworkOperations(resourceId.Parent.Name); return(vnetOps.GetSubnetOperations(resourceId.Name)); }
/// <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)); } if (resourceId.Type != NetworkSecurityGroupOperations.ResourceType) { throw new ArgumentException($"{nameof(resourceId.Type)} provided is not for a NetworkSecurityGroup.", nameof(resourceId.Type)); } var subOps = client.GetSubscriptionOperations(resourceId.Subscription); var rgOps = subOps.GetResourceGroupOperations(resourceId.ResourceGroup); return(rgOps.GetNetworkSecurityGroupOperations(resourceId.Name)); }
public override void Execute() { var createVm = new CreateSingleVmExample(Context); createVm.Execute(); var client = new AzureResourceManagerClient(); var subscription = client.GetSubscriptionOperations(Context.SubscriptionId); var resourceGroup = subscription.GetResourceGroupOperations(Context.RgName); var vm = resourceGroup.GetVirtualMachineOperations(Context.VmName); Console.WriteLine($"Found VM {Context.VmName}"); Console.WriteLine("--------Stopping VM--------"); vm.PowerOff(); Console.WriteLine("--------Starting VM--------"); vm.PowerOn(); }
public override void Execute() { var createVm = new CreateSingleVmExample(Context); createVm.Execute(); var client = new AzureResourceManagerClient(); var subscription = client.GetSubscriptionOperations(Context.SubscriptionId); var resourceGroup = subscription.GetResourceGroupOperations(Context.RgName).Get().Value; _ = resourceGroup.GetAvailabilitySetOperations(Context.VmName + "_aSet").Get().Value; var vnet = resourceGroup.GetVirtualNetworkOperations(Context.VmName + "_vnet").Get().Value; _ = vnet.GetSubnetOperations(Context.SubnetName).Get().Value; _ = resourceGroup.GetNetworkSecurityGroupOperations(Context.NsgName).Get().Value; _ = resourceGroup.GetPublicIpAddressOperations($"{Context.VmName}_ip").Get().Value; _ = resourceGroup.GetNetworkInterfaceOperations($"{Context.VmName}_nic").Get().Value; _ = resourceGroup.GetVirtualMachineOperations(Context.VmName).Get().Value; }
public override void Execute() { var createVm = new CreateSingleVmExample(Context); createVm.Execute(); var client = new AzureResourceManagerClient(); var subscription = client.GetSubscriptionOperations(Context.SubscriptionId); var resourceGroup = subscription.GetResourceGroupOperations(Context.RgName); var vmId = resourceGroup.GetVirtualMachineOperations(Context.VmName).Id; var vnId = resourceGroup.GetVirtualNetworkOperations(Context.VmName + "_vnet").Id; var subnetId = resourceGroup.GetVirtualNetworkOperations(Context.VmName + "_vnet").GetSubnetOperations(Context.SubnetName).Id; var asId = resourceGroup.GetAvailabilitySetOperations(Context.VmName + "_aSet").Id; var nsgId = resourceGroup.GetNetworkSecurityGroupOperations(Context.NsgName).Id; var niId = resourceGroup.GetNetworkInterfaceOperations(Context.VmName + "_nic").Id; var vmOps = client.GetVirtualMachineOperations(vmId); Console.WriteLine("\nclient.GetVirtualMachineOperations(vmResourceId)"); vmOps.PowerOff(); Console.WriteLine("Option 1 vm is " + vmOps.Get().Value.Data.InstanceView.Statuses.Last().Code); vmOps.PowerOn(); Console.WriteLine("Option 1 vm is " + vmOps.Get().Value.Data.InstanceView.Statuses.Last().Code); var subnetOps = client.GetSubnetOperations(subnetId); Console.WriteLine("Option 1 subnet is " + subnetOps.Id); var vnOps = client.GetVirtualNetworkOperations(vnId); var nsgOps = client.GetNetworkSecurityGroupOperations(nsgId); var niOps = client.GetNetworkInterfaceOperations(niId); var asOps = client.GetAvailabilitySetOperations(asId); Console.WriteLine(vnOps.Id); Console.WriteLine(nsgOps.Id); Console.WriteLine(niOps.Id); Console.WriteLine(asOps.Id); Console.WriteLine("Demo complete"); }
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--------"); }
private Task <VirtualMachine> CreateVmWithBuilderAsync() { 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; // 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 nsg = resourceGroup.GetNetworkSecurityGroupContainer().Construct(80).CreateOrUpdate(Context.NsgName).Value; var subnet = vnet.GetSubnetContainer().Construct("10.0.0.0/24").CreateOrUpdate(Context.SubnetName).Value; // Create Network Interface Console.WriteLine("--------Start create Network Interface--------"); var nic = resourceGroup.GetNetworkInterfaceContainer().Construct(subnet.Id).CreateOrUpdate($"{Context.VmName}_nic").Value; // Options: required parameters on in the constructor VirtualMachineModelBuilder vmBuilder = new VirtualMachineModelBuilder( resourceGroup.GetVirtualMachineContainer(), new VirtualMachineData(new Azure.ResourceManager.Compute.Models.VirtualMachine(Context.Loc)));; var vm = vmBuilder.UseWindowsImage("admin-user", "!@#$%asdfA") .RequiredNetworkInterface(nic.Id) .RequiredAvalabilitySet(aset.Id) .CreateOrUpdate(Context.VmName).Value; return(Task.FromResult(vm)); }
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--------"); }