Exemplo n.º 1
0
        public static StorageAccount CreateStorageAccount(SqlVirtualMachineTestContext context)
        {
            string storageAccountName = "dotnetclient" + context.generateResourceName().Replace("-", "");
            var    stoInput           = new StorageAccountCreateParameters
            {
                Location = context.location,
                Sku      = new Sku(SkuName.StandardLRS, SkuTier.Standard),
                Kind     = "StorageV2"
            };

            context.client.storageClient.StorageAccounts.Create(context.resourceGroup.Name, storageAccountName, stoInput);
            bool created = false;

            while (!created)
            {
                System.Threading.Thread.Sleep(1000);
                var resourceList = context.client.storageClient.StorageAccounts.ListByResourceGroup(context.resourceGroup.Name);
                created =
                    resourceList.Any(
                        t =>
                        StringComparer.OrdinalIgnoreCase.Equals(t.Name, storageAccountName));
            }

            StorageAccount storageAccount = context.client.storageClient.StorageAccounts.GetProperties(context.resourceGroup.Name, storageAccountName);

            storageAccount.Validate();
            return(storageAccount);
        }
Exemplo n.º 2
0
        public static NetworkSecurityGroup CreateNsg(SqlVirtualMachineTestContext context, NetworkSecurityGroup nsg = null)
        {
            if (nsg == null)
            {
                nsg = new NetworkSecurityGroup(name: context.generateResourceName())
                {
                    Location      = context.location,
                    SecurityRules = new List <SecurityRule>(new SecurityRule[]
                    {
                        new SecurityRule()
                        {
                            Name                     = "default-allow-rdp",
                            Priority                 = 1000,
                            Protocol                 = "TCP",
                            Access                   = "Allow",
                            Direction                = "Inbound",
                            SourceAddressPrefix      = "*",
                            SourcePortRange          = "*",
                            DestinationAddressPrefix = "*",
                            DestinationPortRange     = "3389"
                        }
                    })
                };
            }

            context.client.networkClient.NetworkSecurityGroups.CreateOrUpdate(context.resourceGroup.Name, nsg.Name, nsg);
            return(context.client.networkClient.NetworkSecurityGroups.Get(context.resourceGroup.Name, nsg.Name));
        }
        public static SqlVirtualMachineGroup CreateSqlVirtualMachineGroup(SqlVirtualMachineTestContext context, StorageAccount storageAccount, string groupName = null, WsfcDomainProfile profile = null)
        {
            ISqlVirtualMachineGroupsOperations sqlOperations      = context.getSqlClient().SqlVirtualMachineGroups;
            StorageAccountListKeysResult       storageAccountKeys = context.client.storageClient.StorageAccounts.ListKeys(context.resourceGroup.Name, storageAccount.Name);
            IEnumerator <StorageAccountKey>    iter = storageAccountKeys.Keys.GetEnumerator();

            iter.MoveNext();
            string key         = iter.Current.Value;
            string blobAccount = storageAccount.PrimaryEndpoints.Blob;

            if (groupName == null)
            {
                groupName = context.generateResourceName();
            }
            SqlVirtualMachineGroup group = sqlOperations.CreateOrUpdate(context.resourceGroup.Name, groupName, new SqlVirtualMachineGroup
            {
                Location          = context.location,
                SqlImageOffer     = Constants.imageOffer,
                SqlImageSku       = Constants.imageSku,
                WsfcDomainProfile = (profile != null)? profile : new WsfcDomainProfile
                {
                    SqlServiceAccount        = getUsername(Constants.sqlService, Constants.domainName),
                    ClusterOperatorAccount   = getUsername(Constants.adminLogin, Constants.domainName),
                    DomainFqdn               = Constants.domainName + ".com",
                    StorageAccountUrl        = blobAccount,
                    StorageAccountPrimaryKey = key
                }
            });

            group.Validate();
            return(group);
        }
Exemplo n.º 4
0
        public static NetworkInterface CreateNetworkInterface(SqlVirtualMachineTestContext context, VirtualNetwork virtualNetwork = null, NetworkSecurityGroup networkSecurityGroup = null)
        {
            // Create NIC
            MockClient client = context.client;
            ComputeManagementClient computeClient = client.computeClient;

            // Create virtual network
            string subnetName = context.generateResourceName();

            if (virtualNetwork == null)
            {
                virtualNetwork = CreateVirtualNetwork(context, subnetName: subnetName);
            }
            Subnet subnet = virtualNetwork.Subnets[0];

            // Create pbulic IP address
            PublicIPAddress publicIPAddress = CreatePublicIP(context);

            // Create Network security group
            if (networkSecurityGroup == null)
            {
                networkSecurityGroup = CreateNsg(context);
            }

            NetworkInterface nicParameters = new NetworkInterface()
            {
                Location         = context.location,
                IpConfigurations = new List <NetworkInterfaceIPConfiguration>()
                {
                    new NetworkInterfaceIPConfiguration()
                    {
                        Name   = context.generateResourceName(),
                        Subnet = subnet,
                        PrivateIPAllocationMethod = IPAllocationMethod.Dynamic,
                        PublicIPAddress           = publicIPAddress
                    }
                },
                NetworkSecurityGroup = networkSecurityGroup
            };
            string nicName = context.generateResourceName();

            client.networkClient.NetworkInterfaces.CreateOrUpdate(context.resourceGroup.Name, nicName, nicParameters);
            return(client.networkClient.NetworkInterfaces.Get(context.resourceGroup.Name, nicName));
        }
Exemplo n.º 5
0
        public static VirtualNetwork CreateVirtualNetwork(SqlVirtualMachineTestContext context, VirtualNetwork vnet = null, NetworkSecurityGroup networkSecurityGroup = null, string subnetName = null)
        {
            if (subnetName == null)
            {
                subnetName = context.generateResourceName();
            }
            if (networkSecurityGroup == null)
            {
                networkSecurityGroup = CreateNsg(context);
            }
            if (vnet == null)
            {
                vnet = new VirtualNetwork()
                {
                    Location     = context.location,
                    AddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List <string>()
                        {
                            "10.0.0.0/16",
                        }
                    },
                    Subnets = new List <Subnet>()
                    {
                        new Subnet()
                        {
                            Name                 = subnetName,
                            AddressPrefix        = "10.0.0.0/24",
                            NetworkSecurityGroup = networkSecurityGroup
                        }
                    }
                };
            }
            string vnetName = context.generateResourceName();

            context.client.networkClient.VirtualNetworks.CreateOrUpdate(context.resourceGroup.Name, vnetName, vnet);
            return(context.client.networkClient.VirtualNetworks.Get(context.resourceGroup.Name, vnetName));
        }
Exemplo n.º 6
0
        public static PublicIPAddress CreatePublicIP(SqlVirtualMachineTestContext context, PublicIPAddress publicIp = null)
        {
            string publicIpName    = context.generateResourceName();
            string domainNameLabel = context.generateResourceName();

            if (publicIp == null)
            {
                publicIp = new PublicIPAddress()
                {
                    Location = context.location,
                    Tags     = new Dictionary <string, string>()
                    {
                        { "key", "value" }
                    },
                    PublicIPAllocationMethod = IPAllocationMethod.Dynamic,
                    DnsSettings = new PublicIPAddressDnsSettings()
                    {
                        DomainNameLabel = domainNameLabel
                    }
                };
            }
            context.client.networkClient.PublicIPAddresses.CreateOrUpdate(context.resourceGroup.Name, publicIpName, publicIp);
            return(context.client.networkClient.PublicIPAddresses.Get(context.resourceGroup.Name, publicIpName));
        }
Exemplo n.º 7
0
        public static VirtualMachine CreateVM(SqlVirtualMachineTestContext context, string name = null, NetworkInterface nic = null, AvailabilitySet availabilitySet = null)
        {
            MockClient client = context.client;

            if (nic == null)
            {
                nic = CreateNetworkInterface(context);
            }

            // Get VM image
            string publisher = Constants.publisher;
            string offer     = Constants.imageOffer;
            string sku       = Constants.imageSku;
            var    query     = new Microsoft.Rest.Azure.OData.ODataQuery <VirtualMachineImageResource>();

            query.Top = 1;
            var images = client.computeClient.VirtualMachineImages.List(
                location: context.location, publisherName: publisher, offer: offer, skus: sku,
                odataQuery: query);
            var            image    = images.First();
            ImageReference imageRef = new ImageReference
            {
                Publisher = publisher,
                Offer     = offer,
                Sku       = sku,
                Version   = image.Name
            };

            // Create VM
            if (name == null)
            {
                name = context.generateResourceName();
            }
            VirtualMachine vm = client.computeClient.VirtualMachines.CreateOrUpdate(context.resourceGroup.Name, name, new VirtualMachine
            {
                Location        = context.location,
                HardwareProfile = new HardwareProfile
                {
                    VmSize = "Standard_DS13-2_v2"
                },
                NetworkProfile = new NetworkProfile
                {
                    NetworkInterfaces = new List <NetworkInterfaceReference>
                    {
                        new NetworkInterfaceReference
                        {
                            Id = nic.Id
                        }
                    }
                },
                StorageProfile = new StorageProfile
                {
                    ImageReference = imageRef,
                    OsDisk         = new OSDisk
                    {
                        Caching = CachingTypes.None,
                        WriteAcceleratorEnabled = false,
                        CreateOption            = DiskCreateOption.FromImage,
                        ManagedDisk             = new ManagedDiskParameters
                        {
                            StorageAccountType = "Standard_LRS"
                        }
                    },
                    DataDisks = new List <DataDisk>()
                    {
                        new DataDisk()
                        {
                            Caching = CachingTypes.None,
                            WriteAcceleratorEnabled = false,
                            CreateOption            = DiskCreateOptionTypes.Empty,
                            Lun         = 0,
                            DiskSizeGB  = 30,
                            ManagedDisk = new ManagedDiskParameters()
                            {
                                StorageAccountType = "Standard_LRS"
                            }
                        }
                    }
                },
                OsProfile = new OSProfile
                {
                    AdminUsername        = Constants.adminLogin,
                    AdminPassword        = Constants.adminPassword,
                    ComputerName         = name,
                    WindowsConfiguration = new WindowsConfiguration()
                    {
                        ProvisionVMAgent = true
                    }
                },
                AvailabilitySet = availabilitySet == null? null : new Compute.Models.SubResource()
                {
                    Id = availabilitySet.Id
                },
            });

            vm.Validate();
            return(vm);
        }
        public static AvailabilityGroupListener CreateAGListener(SqlVirtualMachineTestContext context, string agListenerName, string groupName)
        {
            MockClient client = context.client;

            string domainName    = Constants.domainName;
            string adminLogin    = Constants.adminLogin;
            string adminPassword = Constants.adminPassword;

            // Create domain
            NetworkSecurityGroup    nsg    = CreateNsg(context);
            VirtualNetwork          vnet   = CreateVirtualNetwork(context, networkSecurityGroup: nsg);
            NetworkInterface        nic    = CreateNetworkInterface(context, virtualNetwork: vnet, networkSecurityGroup: nsg);
            VirtualMachine          vm     = CreateVM(context, nic: nic);
            VirtualMachineExtension domain = CreateDomain(context, vm, domainName, adminLogin, adminPassword);

            Assert.NotNull(domain);

            // Update DNS
            Subnet subnet = vnet.Subnets[0];

            UpdateVnetDNS(context, vnet, nsg, nic, subnet);

            // Create SqlVirtualMachineGroup
            StorageAccount storageAccount = CreateStorageAccount(context);
            StorageAccountListKeysResult    storageAccountKeys = client.storageClient.StorageAccounts.ListKeys(context.resourceGroup.Name, storageAccount.Name);
            IEnumerator <StorageAccountKey> iter = storageAccountKeys.Keys.GetEnumerator();

            iter.MoveNext();
            string            key     = iter.Current.Value;
            WsfcDomainProfile profile = new WsfcDomainProfile()
            {
                ClusterBootstrapAccount  = getUsername(adminLogin, domainName),
                ClusterOperatorAccount   = getUsername(adminLogin, domainName),
                SqlServiceAccount        = getUsername(Constants.sqlService, domainName),
                StorageAccountUrl        = "https://" + storageAccount.Name + ".blob.core.windows.net/",
                StorageAccountPrimaryKey = key,
                DomainFqdn = domainName + ".com",
                OuPath     = ""
            };
            SqlVirtualMachineGroup group = CreateSqlVirtualMachineGroup(context, storageAccount, groupName: groupName, profile: profile);

            // Create availability set
            AvailabilitySet availabilitySet = client.computeClient.AvailabilitySets.CreateOrUpdate(context.resourceGroup.Name, context.generateResourceName(), new AvailabilitySet()
            {
                Location = context.location,
                PlatformFaultDomainCount  = 3,
                PlatformUpdateDomainCount = 2,
                Sku = new Microsoft.Azure.Management.Compute.Models.Sku
                {
                    Name = "Aligned"
                }
            });

            // Create two sql virtual machines
            NetworkInterface nic1 = CreateNetworkInterface(context, virtualNetwork: vnet);
            NetworkInterface nic2 = CreateNetworkInterface(context, virtualNetwork: vnet);

            VirtualMachine vm1 = CreateVM(context, nic: nic1, availabilitySet: availabilitySet);
            VirtualMachine vm2 = CreateVM(context, nic: nic2, availabilitySet: availabilitySet);

            SqlVirtualMachineModel sqlVM1 = prepareMachine(context, group, vm1, domainName, adminLogin, adminPassword);
            SqlVirtualMachineModel sqlVM2 = prepareMachine(context, group, vm2, domainName, adminLogin, adminPassword);

            // Create load balancer
            LoadBalancer loadBalancer = client.networkClient.LoadBalancers.CreateOrUpdate(context.resourceGroup.Name, context.generateResourceName(), new LoadBalancer()
            {
                Location = context.location,
                Sku      = new LoadBalancerSku("Basic"),
                FrontendIPConfigurations = new List <FrontendIPConfiguration> (new FrontendIPConfiguration[]
                {
                    new FrontendIPConfiguration()
                    {
                        Name = "LoadBalancerFrontEnd",
                        PrivateIPAllocationMethod = "Dynamic",
                        Subnet = subnet
                    }
                })
            });

            // Run deployment to create an availability group with the two machines created
            string AgName = "AvGroup";
            VirtualMachineExtension availabilityGroup = context.client.computeClient.VirtualMachineExtensions.CreateOrUpdate(context.resourceGroup.Name, vm1.Name, "agCreation", new VirtualMachineExtension(context.location, name: AgName)
            {
                VirtualMachineExtensionType = "CustomScriptExtension",

                Publisher               = "Microsoft.Compute",
                TypeHandlerVersion      = "1.9",
                AutoUpgradeMinorVersion = true,

                Settings = new CustomScriptExtensionSettings
                {
                    FileUris = new List <string>(new string[] {
                        "https://agtemplatestorage.blob.core.windows.net/templates/Deploy.ps1",
                        "https://agtemplatestorage.blob.core.windows.net/templates/sqlvm6.sql",
                        "https://agtemplatestorage.blob.core.windows.net/templates/sqlvm7.sql",
                        "https://agtemplatestorage.blob.core.windows.net/templates/sqlvm8.sql",
                        "https://agtemplatestorage.blob.core.windows.net/test/sqlvm9.sql",
                        "https://agtemplatestorage.blob.core.windows.net/templates/sqlvm10.sql"
                    }),
                    CommandToExecute = "powershell -ExecutionPolicy Unrestricted -File Deploy.ps1 " + AgName + " " + vm1.Name + " " + vm2.Name + " " + Constants.sqlLogin + " " + adminPassword + " " + domainName + "\\" + Constants.sqlService,
                    ContentVersion   = "1.0.0.0"
                }
            });

            // Create availability group listener
            return(context.getSqlClient().AvailabilityGroupListeners.CreateOrUpdate(context.resourceGroup.Name, group.Name, agListenerName, new AvailabilityGroupListener()
            {
                LoadBalancerConfigurations = new List <LoadBalancerConfiguration>(new LoadBalancerConfiguration[]
                {
                    new LoadBalancerConfiguration()
                    {
                        PrivateIpAddress = new PrivateIPAddress(ipAddress: "10.0.0.11", subnetResourceId: subnet.Id),
                        ProbePort = 59999,
                        LoadBalancerResourceId = loadBalancer.Id,
                        SqlVirtualMachineInstances = new List <string>()
                        {
                            sqlVM1.Id, sqlVM2.Id
                        }
                    }
                }),
                AvailabilityGroupName = AgName,
                Port = 1433
            }));
        }