示例#1
0
        public async Task GettingResourceGroupContainer()
        {
            #region Snippet:Managing_Resource_Groups_GetResourceGroupContainer
            ArmClient              armClient    = new ArmClient(new DefaultAzureCredential());
            Subscription           subscription = armClient.DefaultSubscription;
            ResourceGroupContainer rgContainer  = subscription.GetResourceGroups();

            // code omitted for brevity

            string rgName = "myRgName";
#if !SNIPPET
            //Check if "myRgName" exists, if not, create it first or run CreateResourceGroup()
            ResourceGroup rg = await subscription.GetResourceGroups().GetIfExistsAsync(rgName);

            if (rg == null)
            {
                Location          location = Location.WestUS2;
                ResourceGroupData rgData   = new ResourceGroupData(location);
                _ = await rgContainer.CreateOrUpdateAsync(rgName, rgData);
            }
#endif
            ResourceGroup resourceGroup = await rgContainer.GetAsync(rgName);

            #endregion Snippet:Managing_Resource_Groups_GetResourceGroupContainer
        }
示例#2
0
        public async Task UpdateAResourceGroup()
        {
            #region Snippet:Managing_Resource_Groups_UpdateAResourceGroup
            // Note: Resource group named 'myRgName' should exist for this example to work.
            ArmClient    armClient    = new ArmClient(new DefaultAzureCredential());
            Subscription subscription = armClient.DefaultSubscription;
            string       rgName       = "myRgName";
#if !SNIPPET
            //Check if 'myRgName' exists, if not, create it first or run CreateResourceGroup()
            ResourceGroup rg = await subscription.GetResourceGroups().GetIfExistsAsync(rgName);

            if (rg == null)
            {
                Location location = Location.WestUS2;
                ResourceGroupContainer rgContainer = subscription.GetResourceGroups();
                ResourceGroupData      rgData      = new ResourceGroupData(location);
                _ = await rgContainer.CreateOrUpdateAsync(rgName, rgData);
            }
#endif
            ResourceGroup resourceGroup = await subscription.GetResourceGroups().GetAsync(rgName);

            resourceGroup = await resourceGroup.AddTagAsync("key", "value");

            #endregion Snippet:Managing_Resource_Groups_UpdateAResourceGroup
        }
示例#3
0
 public async Task CreateResourceGroup()
 {
     #region Snippet:Readme_GetResourceGroupContainer
     ArmClient armClient = new ArmClient(new DefaultAzureCredential());
     Subscription subscription = armClient.DefaultSubscription;
     ResourceGroupContainer rgContainer = subscription.GetResourceGroups();
     // With the container, we can create a new resource group with an specific name
     string rgName = "myRgName";
     Location location = Location.WestUS2;
     ResourceGroupCreateOrUpdateOperation lro = await rgContainer.CreateOrUpdateAsync(rgName, new ResourceGroupData(location));
     ResourceGroup resourceGroup = lro.Value;
     #endregion
 }
        public async Task CreateResourceGroupAsync()
        {
            #region Snippet:Creating_A_Virtual_Network_CreateResourceGroup
            ArmClient armClient = new ArmClient(new DefaultAzureCredential());
            ResourceGroupContainer rgContainer = armClient.DefaultSubscription.GetResourceGroups();

            string            rgName = "myResourceGroup";
            ResourceGroupData rgData = new ResourceGroupData(Location.WestUS2);
            ResourceGroupCreateOrUpdateOperation operation = await rgContainer.CreateOrUpdateAsync(rgName, rgData);

            ResourceGroup resourceGroup = operation.Value;
            #endregion Snippet:Creating_A_Virtual_Network_CreateResourceGroup
        }
        protected async Task initialize()
        {
            ArmClient    armClient    = new ArmClient(new DefaultAzureCredential());
            Subscription subscription = armClient.DefaultSubscription;

            ResourceGroupContainer rgContainer = subscription.GetResourceGroups();
            // With the container, we can create a new resource group with an specific name
            string   rgName   = "myRgName";
            Location location = Location.WestUS2;
            ResourceGroupCreateOrUpdateOperation lro = await rgContainer.CreateOrUpdateAsync(rgName, new ResourceGroupData(location));

            ResourceGroup resourceGroup = lro.Value;

            this.resourceGroup = resourceGroup;
        }
示例#6
0
        public async Task CreateResourceGroup()
        {
            #region Snippet:Managing_Resource_Groups_CreateAResourceGroup
            // First, initialize the ArmClient and get the default subscription
            ArmClient armClient = new ArmClient(new DefaultAzureCredential());
            // Now we get a ResourceGroup container for that subscription
            Subscription           subscription = armClient.DefaultSubscription;
            ResourceGroupContainer rgContainer  = subscription.GetResourceGroups();

            // With the container, we can create a new resource group with an specific name
            string            rgName   = "myRgName";
            Location          location = Location.WestUS2;
            ResourceGroupData rgData   = new ResourceGroupData(location);
            ResourceGroupCreateOrUpdateOperation operation = await rgContainer.CreateOrUpdateAsync(rgName, rgData);

            ResourceGroup resourceGroup = operation.Value;
            #endregion Snippet:Managing_Resource_Groups_CreateAResourceGroup
        }
示例#7
0
        public async Task CreateAvailabilitySet()
        {
            // First, initialize the ArmClient and get the default subscription
            ArmClient armClient = new ArmClient(new DefaultAzureCredential());
            // Now we get a ResourceGroup container for that subscription
            Subscription           subscription = armClient.DefaultSubscription;
            ResourceGroupContainer rgContainer  = subscription.GetResourceGroups();

            // With the container, we can create a new resource group with an specific name
            string   rgName   = "myRgName";
            Location location = Location.WestUS2;
            ResourceGroupCreateOrUpdateOperation rgLro = await rgContainer.CreateOrUpdateAsync(rgName, new ResourceGroupData(location));

            ResourceGroup resourceGroup = rgLro.Value;

            #region Snippet:Managing_Availability_Set_CreateAnAvailabilitySet
            AvailabilitySetContainer availabilitySetContainer = resourceGroup.GetAvailabilitySets();
            string availabilitySetName = "myAvailabilitySet";
            AvailabilitySetData input  = new AvailabilitySetData(location);
            AvailabilitySetCreateOrUpdateOperation lro = await availabilitySetContainer.CreateOrUpdateAsync(availabilitySetName, input);

            AvailabilitySet availabilitySet = lro.Value;
            #endregion Snippet:Managing_Availability_Set_CreateAnAvailabilitySet
        }
示例#8
0
        public async Task MigrationExample()
        {
            #region Snippet:Construct_Client
            ArmClient armClient = new ArmClient(new DefaultAzureCredential());
            #endregion

            #region Snippet:Create_ResourceGroup
            Subscription           subscription = armClient.DefaultSubscription;
            ResourceGroupContainer rgContainer  = subscription.GetResourceGroups();

            Location location = Location.WestUS2;
            string   rgName   = "QuickStartRG";

            ResourceGroupData rgData = new ResourceGroupData(location);
            ResourceGroupCreateOrUpdateOperation rgCreateLro = await rgContainer.CreateOrUpdateAsync(rgName, rgData);

            ResourceGroup resourceGroup = rgCreateLro.Value;
            #endregion

            #region Snippet:Create_AvailabilitySet
            string vmName = "quickstartvm";
            AvailabilitySetData aSetData = new AvailabilitySetData(location);
            AvailabilitySetCreateOrUpdateOperation asetCreateLro = await resourceGroup.GetAvailabilitySets().CreateOrUpdateAsync(vmName + "_aSet", aSetData);

            AvailabilitySet aset   = asetCreateLro.Value;
            string          asetId = aset.Id;
            #endregion

            #region Snippet:Create_Vnet_and_Subnet
            string       vnetName     = "MYVM" + "_vnet";
            string       subnetName   = "mySubnet";
            AddressSpace addressSpace = new AddressSpace();
            addressSpace.AddressPrefixes.Add("10.0.0.0/16");

            VirtualNetworkData vnetData = new VirtualNetworkData()
            {
                AddressSpace = addressSpace,
                Subnets      =
                {
                    new SubnetData()
                    {
                        Name          = subnetName,
                        AddressPrefix = "10.0.0.0/24"
                    }
                }
            };
            VirtualNetworkCreateOrUpdateOperation vnetCreateLro = await resourceGroup.GetVirtualNetworks().CreateOrUpdateAsync(vnetName, vnetData);

            VirtualNetwork vnet = vnetCreateLro.Value;
            #endregion

            #region Snippet:Create_NetworkSecurityGroup
            string nsgName = vmName + "_nsg";
            NetworkSecurityGroupData nsgData = new NetworkSecurityGroupData()
            {
                Location = location
            };
            NetworkSecurityGroupCreateOrUpdateOperation nsgCreateLro = await resourceGroup.GetNetworkSecurityGroups().CreateOrUpdateAsync(nsgName, nsgData);

            NetworkSecurityGroup nsg = nsgCreateLro.Value;
            #endregion

            #region Snippet:Create_NetworkInterface
            string nicName = vmName + "_nic";
            NetworkInterfaceIPConfiguration nicIPConfig = new NetworkInterfaceIPConfiguration()
            {
                Name    = "Primary",
                Primary = true,
                Subnet  = new SubnetData()
                {
                    Id = vnet.Data.Subnets.First().Id
                },
                PrivateIPAllocationMethod = IPAllocationMethod.Dynamic,
            };

            NetworkInterfaceData nicData = new NetworkInterfaceData();
            nicData.Location = location;
            nicData.IpConfigurations.Add(nicIPConfig);
            NetworkInterfaceCreateOrUpdateOperation nicCreateLro = await resourceGroup.GetNetworkInterfaces().CreateOrUpdateAsync(nicName, nicData);

            NetworkInterface nic = nicCreateLro.Value;
            #endregion

            #region Snippet:Create_VirtualMachine
            VirtualMachineData vmData = new VirtualMachineData(location);
            vmData.OsProfile.AdminUsername = "******";
            vmData.OsProfile.AdminPassword = "******";
            vmData.OsProfile.ComputerName  = "computer-name";
            vmData.AvailabilitySet         = new Compute.Models.SubResource();
            vmData.AvailabilitySet.Id      = aset.Id;
            NetworkInterfaceReference nicReference = new NetworkInterfaceReference();
            nicReference.Id = nic.Id;
            vmData.NetworkProfile.NetworkInterfaces.Add(nicReference);

            VirtualMachine vm = (await resourceGroup.GetVirtualMachines().CreateOrUpdateAsync(vmName, vmData)).Value;
            Console.WriteLine("VM ID: " + vm.Id);
            #endregion
        }
示例#9
0
 public static async Task TryRegisterResourceGroupAsync(ResourceGroupContainer resourceGroupsOperations, string location, string resourceGroupName)
 {
     await resourceGroupsOperations.CreateOrUpdateAsync(resourceGroupName, new ResourceGroupData(location));
 }