예제 #1
0
 private void ValidateMarketplaceVMPlanField(VirtualMachine vm, VirtualMachineImage img)
 {
     Assert.NotNull(vm.Plan);
     Assert.Equal(img.Plan.Publisher, vm.Plan.Publisher);
     Assert.Equal(img.Plan.Product, vm.Plan.Product);
     Assert.Equal(img.Plan.Name, vm.Plan.Name);
 }
예제 #2
0
 private void ValidateMarketplaceVMPlanField(VirtualMachine vm, VirtualMachineImage img)
 {
     if (vm.Plan != null)
     {
         Assert.True(vm.Plan.Name != null);
         Assert.True(vm.Plan.Product != null);
         Assert.True(vm.Plan.PromotionCode != null);
     }
 }
        public async Task <Response <VirtualMachineImage> > GetAsync(string subscriptionId, string location, string edgeZone, string publisherName, string offer, string skus, string version, CancellationToken cancellationToken = default)
        {
            if (subscriptionId == null)
            {
                throw new ArgumentNullException(nameof(subscriptionId));
            }
            if (location == null)
            {
                throw new ArgumentNullException(nameof(location));
            }
            if (edgeZone == null)
            {
                throw new ArgumentNullException(nameof(edgeZone));
            }
            if (publisherName == null)
            {
                throw new ArgumentNullException(nameof(publisherName));
            }
            if (offer == null)
            {
                throw new ArgumentNullException(nameof(offer));
            }
            if (skus == null)
            {
                throw new ArgumentNullException(nameof(skus));
            }
            if (version == null)
            {
                throw new ArgumentNullException(nameof(version));
            }

            using var message = CreateGetRequest(subscriptionId, location, edgeZone, publisherName, offer, skus, version);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 200:
            {
                VirtualMachineImage value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = VirtualMachineImage.DeserializeVirtualMachineImage(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }
예제 #4
0
        public async Task <Response <VirtualMachineImage> > GetAsync(string subscriptionId, AzureLocation location, string publisherName, string offer, string skus, string version, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(subscriptionId, nameof(subscriptionId));
            Argument.AssertNotNullOrEmpty(publisherName, nameof(publisherName));
            Argument.AssertNotNullOrEmpty(offer, nameof(offer));
            Argument.AssertNotNullOrEmpty(skus, nameof(skus));
            Argument.AssertNotNullOrEmpty(version, nameof(version));

            using var message = CreateGetRequest(subscriptionId, location, publisherName, offer, skus, version);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 200:
            {
                VirtualMachineImage value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = VirtualMachineImage.DeserializeVirtualMachineImage(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }
        /// <inheritdoc/>
        public async Task <IVirtualMachineResource> CreateAsync(
            IResourceGroupResource resourceGroup, string name,
            INetworkResource netres, VirtualMachineImage image,
            string customData)
        {
            if (resourceGroup == null)
            {
                throw new ArgumentNullException(nameof(resourceGroup));
            }

            var client = await CreateClientAsync(resourceGroup);

            name = await client.VirtualMachines.SelectResourceNameAsync(resourceGroup.Name,
                                                                        "vm", name);

            var user = VirtualMachineResource.kDefaultUser;
            var pw   = "Vm$" + name.ToSha1Hash().Substring(0, 3);

            if (image == null)
            {
                image = KnownImages.Ubuntu_16_04_lts;
            }

            var      attempt = 0;
            INetwork network = null;

            if (netres != null)
            {
                network = await client.Networks.GetByResourceGroupAsync(
                    resourceGroup.Name, netres.Name);

                if (network == null)
                {
                    throw new ArgumentException(nameof(netres));
                }
            }

            var originalRegion = await resourceGroup.Subscription.GetRegionAsync();

            while (true)
            {
                var regionAndVmSize = await SelectRegionAndVmSizeAsync(
                    resourceGroup, client);

                if (regionAndVmSize == null)
                {
                    throw new ExternalDependencyException(
                              "No sizes available.");
                }
                var region = regionAndVmSize.Item1;
                var vmSize = regionAndVmSize.Item2;
                try {
                    var nicDefine = client.NetworkInterfaces
                                    .Define(name)
                                    .WithRegion(region)
                                    .WithExistingResourceGroup(resourceGroup.Name);
                    var nic = nicDefine
                              .WithNewPrimaryNetwork(client.Networks
                                                     .Define(name)
                                                     .WithRegion(region)
                                                     .WithExistingResourceGroup(resourceGroup.Name)
                                                     .WithAddressSpace("172.16.0.0/16"))
                              .WithPrimaryPrivateIPAddressDynamic();

                    var ipName = await client.PublicIPAddresses.SelectResourceNameAsync(
                        resourceGroup.Name, "ip");

                    var publicIP = client.PublicIPAddresses
                                   .Define(ipName)
                                   .WithRegion(region)
                                   .WithExistingResourceGroup(resourceGroup.Name);

                    if (network != null && originalRegion == region)
                    {
                        nic = nicDefine
                              .WithExistingPrimaryNetwork(network)
                              .WithSubnet(netres.Subnet)
                              .WithPrimaryPrivateIPAddressDynamic();
                    }

                    nic = nic.WithNewPrimaryPublicIPAddress(
                        client.PublicIPAddresses
                        .Define(name)
                        .WithRegion(region)
                        .WithExistingResourceGroup(resourceGroup.Name));

                    var withOs = client.VirtualMachines
                                 .Define(name)
                                 .WithRegion(region)
                                 .WithExistingResourceGroup(resourceGroup.Name)
                                 .WithNewPrimaryNetworkInterface(nic);

                    IWithFromImageCreateOptionsManaged machine;
                    if (image.IsLinux)
                    {
                        machine = withOs
                                  .WithLatestLinuxImage(image.Publisher,
                                                        image.Offer, image.Sku)
                                  .WithRootUsername(user)
                                  .WithRootPassword(pw);
                    }
                    else
                    {
                        machine = withOs
                                  .WithLatestWindowsImage(image.Publisher,
                                                          image.Offer, image.Sku)
                                  .WithAdminUsername(user)
                                  .WithAdminPassword(pw);
                    }

                    _logger.Info($"Trying to create vm {name} on {vmSize}...");

                    IVirtualMachine vm = null;
                    if (!string.IsNullOrEmpty(customData))
                    {
                        vm = await machine
                             .WithCustomData(customData)
                             .WithSize(vmSize)
                             .CreateAsync();

                        _logger.Info($"Starting vm {name} ...");
                        // Restart for changes to go into effect
                        await vm.RestartAsync();
                    }
                    else
                    {
                        vm = await machine.WithSize(vmSize).CreateAsync();
                    }
                    _logger.Info($"Created vm {name}.");
                    return(new VirtualMachineResource(this, resourceGroup, vm,
                                                      user, pw, _logger));
                }
                catch (Exception ex) {
                    _logger.Info(
                        $"#{attempt} failed creating VM {name} as {vmSize}...",
                        () => ex);
                    await TryDeleteResourcesAsync(resourceGroup, name);

                    if (++attempt == 3)
                    {
                        throw ex;
                    }
                }
            }
        }
예제 #6
0
 private void ValidateMarketplaceVMScaleSetPlanField(VirtualMachineScaleSet vmScaleSet, VirtualMachineImage img)
 {
     Assert.NotNull(vmScaleSet.Plan);
     Assert.AreEqual(img.Plan.Publisher, vmScaleSet.Plan.Publisher);
     Assert.AreEqual(img.Plan.Product, vmScaleSet.Plan.Product);
     Assert.AreEqual(img.Plan.Name, vmScaleSet.Plan.Name);
 }