public static AzureNic ConstructNic(this AzureResourceGroupBase resourceGroup, AzurePublicIpAddress ip, string subnetId)
        {
            var nic = new NetworkInterface()
            {
                Location         = resourceGroup.Location,
                IpConfigurations = new List <NetworkInterfaceIPConfiguration>()
                {
                    new NetworkInterfaceIPConfiguration()
                    {
                        Name    = "Primary",
                        Primary = true,
                        Subnet  = new Subnet()
                        {
                            Id = subnetId
                        },
                        PrivateIPAllocationMethod = IPAllocationMethod.Dynamic,
                        PublicIPAddress           = new PublicIPAddress()
                        {
                            Id = ip.Id
                        }
                    }
                }
            };

            return(new AzureNic(resourceGroup, new PhNetworkInterface(nic)));
        }
        public static AzurePublicIpAddress ConstructIPAddress(this AzureResourceGroupBase resourceGroup)
        {
            var ipAddress = new PublicIPAddress()
            {
                PublicIPAddressVersion   = Azure.ResourceManager.Network.Models.IPVersion.IPv4.ToString(),
                PublicIPAllocationMethod = IPAllocationMethod.Dynamic,
                Location = resourceGroup.Location,
            };

            return(new AzurePublicIpAddress(resourceGroup, new PhPublicIPAddress(ipAddress)));
        }
        public static AzureAvailabilitySet ConstructAvailabilitySet(this AzureResourceGroupBase resourceGroup, string skuName)
        {
            var availabilitySet = new AvailabilitySet(resourceGroup.Location)
            {
                PlatformUpdateDomainCount = 5,
                PlatformFaultDomainCount  = 2,
                Sku = new Azure.ResourceManager.Compute.Models.Sku()
                {
                    Name = skuName
                },
            };

            return(new AzureAvailabilitySet(resourceGroup, new PhAvailabilitySet(availabilitySet)));
        }
        public static NetworkSecurityGroupCollection Nsgs(this AzureResourceGroupBase resourceGroup)
        {
            NetworkSecurityGroupCollection result;

            lock (nsgLock)
            {
                if (!nsgCollections.TryGetValue(resourceGroup.Id, out result))
                {
                    result = new NetworkSecurityGroupCollection(resourceGroup);
                    nsgCollections.Add(resourceGroup.Id, result);
                }
            }

            return(result);
        }
        public static AzureVnet ConstructVnet(this AzureResourceGroupBase resourceGroup, string vnetCidr)
        {
            var vnet = new VirtualNetwork()
            {
                Location     = resourceGroup.Location,
                AddressSpace = new AddressSpace()
                {
                    AddressPrefixes = new List <string>()
                    {
                        vnetCidr
                    }
                },
            };

            return(new AzureVnet(resourceGroup, new PhVirtualNetwork(vnet)));
        }
        public static AvailabilitySetCollection AvailabilitySets(this AzureResourceGroupBase resourceGroup)
        {
            AvailabilitySetCollection result;

            if (!asetCollections.TryGetValue(resourceGroup.Id, out result))
            {
                lock (asetLock)
                {
                    if (!asetCollections.TryGetValue(resourceGroup.Id, out result))
                    {
                        result = new AvailabilitySetCollection(resourceGroup);
                        asetCollections.Add(resourceGroup.Id, result);
                    }
                }
            }
            return(result);
        }
        public static VmCollection Vms(this AzureResourceGroupBase resourceGroup)
        {
            VmCollection result;

            if (!vmCollections.TryGetValue(resourceGroup.Id, out result))
            {
                lock (vmLock)
                {
                    if (!vmCollections.TryGetValue(resourceGroup.Id, out result))
                    {
                        result = new VmCollection(resourceGroup);
                        vmCollections.Add(resourceGroup.Id, result);
                    }
                }
            }
            return(result);
        }
        public static NicCollection Nics(this AzureResourceGroupBase resourceGroup)
        {
            NicCollection result;

            if (!nicCollections.TryGetValue(resourceGroup.Id, out result))
            {
                lock (nicLock)
                {
                    if (!nicCollections.TryGetValue(resourceGroup.Id, out result))
                    {
                        result = new NicCollection(resourceGroup);
                        nicCollections.Add(resourceGroup.Id, result);
                    }
                }
            }

            return(result);
        }
        public static PublicIpAddressCollection IpAddresses(this AzureResourceGroupBase resourceGroup)
        {
            PublicIpAddressCollection result;

            if (!ipCollections.TryGetValue(resourceGroup.Id, out result))
            {
                lock (ipLock)
                {
                    if (!ipCollections.TryGetValue(resourceGroup.Id, out result))
                    {
                        result = new PublicIpAddressCollection(resourceGroup);
                        ipCollections.Add(resourceGroup.Id, result);
                    }
                }
            }

            return(result);
        }
        public static AzureVm ConstructVm(this AzureResourceGroupBase resourceGroup, string vmName, string adminUser, string adminPw, AzureNic nic, AzureAvailabilitySet aset)
        {
            var vm = new VirtualMachine(resourceGroup.Location)
            {
                NetworkProfile = new NetworkProfile {
                    NetworkInterfaces = new[] { new NetworkInterfaceReference()
                                                {
                                                    Id = nic.Id
                                                } }
                },
                OsProfile = new OSProfile
                {
                    ComputerName       = vmName,
                    AdminUsername      = adminUser,
                    AdminPassword      = adminPw,
                    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
                },
                AvailabilitySet = new SubResource()
                {
                    Id = aset.Id
                }
            };

            return(new AzureVm(resourceGroup, new PhVirtualMachine(vm)));
        }
        /// <summary>
        /// Create an NSG with the given open TCP ports
        /// </summary>
        /// <param name="openPorts">The set of TCP ports to open</param>
        /// <returns>An NSG, with the given TCP ports open</returns>
        public static AzureNetworkSecurityGroup ConstructNsg(this AzureResourceGroupBase resourceGroup, string nsgName, params int[] openPorts)
        {
            var nsg = new NetworkSecurityGroup {
                Location = resourceGroup.Location
            };
            var index = 0;

            nsg.SecurityRules = openPorts.Select(openPort => new SecurityRule
            {
                Name                     = $"Port{openPort}",
                Priority                 = 1000 + (++index),
                Protocol                 = SecurityRuleProtocol.Tcp,
                Access                   = SecurityRuleAccess.Allow,
                Direction                = SecurityRuleDirection.Inbound,
                SourcePortRange          = "*",
                SourceAddressPrefix      = "*",
                DestinationPortRange     = $"{openPort}",
                DestinationAddressPrefix = "*",
                Description              = $"Port_{openPort}"
            }).ToList();
            var result = new AzureNetworkSecurityGroup(resourceGroup, nsg, nsgName);

            return(result);
        }