getting image from azure compute gallery in c sharp

Dharshan M 0 Reputation points
2023-11-22T12:10:43.5666667+00:00

i need to get the image from the azure compute gallery to create a virtual machine.

so help me with to get the image from the azure compute gallery.

   public void DOCreateVMFromImage()
        {
            string captureimage = "Image1";
            string vmName = "Test-3";
            string resourceGrp = "TESTVMGROUP";
            string resource = "TESTVMGROUP"; 
            string adminUsername = "Student1";
            string adminPassword = "student1Excel";
           // var location = Region.USEast;
            var vNetName = "VNET-Fluent";
            var vNetAddress = "172.16.0.0/16";
            var subnetName = "Subnet-Fluent";
            var subnetAddress = "172.16.0.0/24";
            var nicName = "NIC-CVM";
            var publicIPName = "TestpublicIp";
            var nsgName = "NSG-Fluent";
            var sharedImageGalleryName = "NewImages";  // Replace with your actual gallery name
            var sharedImageDefinitionName = "Image1";  // Replace with your actual image definition name
            var sharedImageVersion = "0.0.1";

            var credentials = SdkContext.AzureCredentialsFactory
                    .FromFile("../../../azure-configuration.json");

            var azure = Azure.Authenticate(credentials).WithDefaultSubscription();

            // Get the captured image
            var capturedImage = azure.VirtualMachineImages.GetImage(resourceGrp, sharedImageGalleryName, sharedImageDefinitionName, sharedImageVersion);

            if (capturedImage != null)
            {
             //  var resourceGroup = azure.ResourceGroups.Define(resource)
             //.WithRegion(capturedImage.Region)
             //.Create();

                Console.WriteLine($"Creating virtual network {vNetName} ...");
                var network = azure.Networks.Define(vNetName)
                    .WithRegion(capturedImage.Region)
                    .WithExistingResourceGroup(resource)
                    .WithAddressSpace(vNetAddress)
                    .WithSubnet(subnetName, subnetAddress)
                    .Create();

                Console.WriteLine($"Creating public IP {publicIPName} ...");
                var publicIP = azure.PublicIPAddresses.Define(publicIPName)
                    .WithRegion(capturedImage.Region)
                    .WithExistingResourceGroup(resource)
                    .Create();
                //You need a network security group for controlling the access to the VM
                Console.WriteLine($"Creating Network Security Group {nsgName} ...");
                var nsg = azure.NetworkSecurityGroups.Define(nsgName)
                    .WithRegion(capturedImage.Region)
                    .WithExistingResourceGroup(resource)
                    .Create();
                //You need a security rule for allowing the
                //Internet
                Console.WriteLine($"Creating a Security Rule for allowing the remote");
                nsg.Update()
                    .DefineRule("Allow-RDP")
                    .AllowInbound()
                    .FromAnyAddress()
                    .FromAnyPort()
                    .ToAnyAddress()
                    .ToPort(3389)
                    .WithProtocol(SecurityRuleProtocol.Tcp)
                    .WithPriority(100)
                    .Attach()
                    .Apply();


                Console.WriteLine($"Creating network interface {nicName} ...");
                var nic = azure.NetworkInterfaces.Define(nicName)
                         .WithRegion(capturedImage.Region)
                         .WithExistingResourceGroup(resource)
                         .WithExistingPrimaryNetwork(network)
                         .WithSubnet(subnetName)
                         .WithPrimaryPrivateIPAddressDynamic()
                         .WithExistingPrimaryPublicIPAddress(publicIP)
                         .WithExistingNetworkSecurityGroup(nsg)
                         .Create();

                Console.WriteLine($"Creating a new VM using {captureimage}...");

                var newVM = azure.VirtualMachines.Define(vmName)
                                                 .WithRegion(capturedImage.Region)
                                                 .WithExistingResourceGroup(resource)
                                                 .WithExistingPrimaryNetworkInterface(nic)
                                                 .WithWindowsCustomImage(capturedImage.Id)
                                                 .WithAdminUsername(adminUsername)
                                                 .WithAdminPassword(adminPassword)
                                                 .WithComputerName(vmName)
                                                 .WithSize(VirtualMachineSizeTypes.StandardDS2V2)
                                                 .Create();
                ///CheckVMStatus(azure, newVM.Id);
               ///ShutDownVM(azure, newVM.Id);

                Console.WriteLine("Successfully created a new VM: {0}!", vmName);
                Console.WriteLine("Press any key to exit...");
                Console.ReadLine();
            }
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,629 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 19,706 Reputation points
    2023-11-22T14:57:06.66+00:00

    The code snippet you've provided includes most of what you need to accomplish this task. I will try to explain it step by step :

    The SdkContext.AzureCredentialsFactory.FromFile will load theAzure credentials from a configuration file for authenticating your application with Azure services.

    The Azure.Authenticate(credentials).WithDefaultSubscription() to authenticate and select the default subscription.

    Then to retrieve the image from the Azure Compute Gallery, you used these variables sharedImageGalleryName, sharedImageDefinitionName, and sharedImageVersion where azure.VirtualMachineImages.GetImage is used to retrieve the image using them.

    Then you defined and created a virtual network azure.Networks.Define, a public IP address azure.PublicIPAddresses.Define, and a network security group azure.NetworkSecurityGroups.Define.

    The azure.NetworkInterfaces.Define is to create a network interface for the VM.

    Then to create the VM, you used the azure.VirtualMachines.Define and specifying the VM's region, resource group, network interface, image, admin credentials, computer name, and size.

    If you were looking to understand the code, here is an explanation.

    0 comments No comments