Skip to content

Commit

Permalink
remove unneeded usings and rename classes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbonaby committed Mar 20, 2024
1 parent ccccf31 commit f1be31d
Show file tree
Hide file tree
Showing 27 changed files with 41 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static IServiceCollection AddHyperVExtensionServices(this IServiceCollect
services.AddSingleton<IPowerShellService>(psService =>
ActivatorUtilities.CreateInstance<PowerShellService>(psService, new PowerShellSession()));
services.AddSingleton<HyperVVirtualMachineFactory>(serviceProvider => psObject => ActivatorUtilities.CreateInstance<HyperVVirtualMachine>(serviceProvider, psObject));
services.AddSingleton<QuickCreateOperationFactory>(serviceProvider => parameters => ActivatorUtilities.CreateInstance<VMGalleryVMCreationOperation>(serviceProvider, parameters));
services.AddSingleton<VmGalleryCreationOperationFactory>(serviceProvider => parameters => ActivatorUtilities.CreateInstance<VMGalleryVMCreationOperation>(serviceProvider, parameters));

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using System.Text.Json.Serialization;
using HyperVExtension.Models.VirtualMachineCreation;

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Used to generate the source code for the classes that we deserialize Json to objects for the QuickCreateConfigfeature.
/// Used to generate the source code for the classes when we deserialize the Json recieved from the VM gallery
/// and any associated json.
/// .Net 8 requires a JsonSerializerContext to be used with the JsonSerializerOptions when
/// deserializing JSON into objects.
/// See : https://learn.microsoft.com/dotnet/standard/serialization/system-text-json/source-generation?pivots=dotnet-8-0
Expand All @@ -23,7 +24,7 @@ namespace HyperVExtension.Models.QuickCreateJsonToClasses;
[JsonSerializable(typeof(VMGalleryRequirements))]
[JsonSerializable(typeof(VMGallerySymbol))]
[JsonSerializable(typeof(VMGalleryThumbnail))]
[JsonSerializable(typeof(QuickCreateUserInput))]
[JsonSerializable(typeof(VMGalleryCreationUserInput))]
public sealed partial class JsonSourceGenerationContext : JsonSerializerContext
{
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents the 'config' json object of an image in the VM Gallery. See Gallery Json "https://go.microsoft.com/fwlink/?linkid=851584"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents the 'detail' json object of an image in the VM Gallery. See Gallery Json "https://go.microsoft.com/fwlink/?linkid=851584"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents the 'disk' json object of an image in the VM Gallery. See Gallery Json "https://go.microsoft.com/fwlink/?linkid=851584"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents the an image json object in the VM Gallery. See Gallery Json "https://go.microsoft.com/fwlink/?linkid=851584"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Text.Json;

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents a list of image json objects in the VM Gallery. See Gallery Json "https://go.microsoft.com/fwlink/?linkid=851584"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents a VM gallery item that contains a uri and a hash.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents the 'Logo' json object of an image in the VM Gallery. See Gallery Json "https://go.microsoft.com/fwlink/?linkid=851584"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents the 'requirements' json object of an image in the VM Gallery. See Gallery Json "https://go.microsoft.com/fwlink/?linkid=851584"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents the 'symbol' json object of an image in the VM Gallery. See Gallery Json "https://go.microsoft.com/fwlink/?linkid=851584"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace HyperVExtension.Models.QuickCreateJsonToClasses;
namespace HyperVExtension.Models.VMGalleryJsonToClasses;

/// <summary>
/// Represents the 'thumbnail' json object of an image in the VM Gallery. See Gallery Json "https://go.microsoft.com/fwlink/?linkid=851584"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Windows.Networking.BackgroundTransfer;

namespace HyperVExtension.Models.VirtualMachineCreation;

public class DownloadOperation : IOperationReport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
namespace HyperVExtension.Models.VirtualMachineCreation;

/// <summary>
/// Represents the user input for a VM gallery operation.
/// Represents the user input for a VM gallery creation operation.
/// </summary>
public sealed class QuickCreateUserInput
public sealed class VMGalleryCreationUserInput
{
public string NewVirtualMachineName { get; set; } = string.Empty;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
using HyperVExtension.Common;
using HyperVExtension.Exceptions;
using HyperVExtension.Helpers;
using HyperVExtension.Models.QuickCreateJsonToClasses;
using HyperVExtension.Models.VMGalleryJsonToClasses;
using HyperVExtension.Providers;
using HyperVExtension.Services;
using Microsoft.Windows.DevHome.SDK;
using Windows.Foundation;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;

namespace HyperVExtension.Models.VirtualMachineCreation;

public delegate VMGalleryVMCreationOperation QuickCreateOperationFactory(QuickCreateUserInput parameters);
public delegate VMGalleryVMCreationOperation VmGalleryCreationOperationFactory(VMGalleryCreationUserInput parameters);

/// <summary>
/// Class that represents the VM gallery VM creation operation.
Expand All @@ -35,7 +34,7 @@ public sealed class VMGalleryVMCreationOperation : IVMGalleryVMCreationOperation

private readonly IVMGalleryService _vmGalleryService;

private readonly QuickCreateUserInput _userInputParameters;
private readonly VMGalleryCreationUserInput _userInputParameters;

public CancellationTokenSource CancellationTokenSource { get; private set; } = new();

Expand All @@ -61,7 +60,7 @@ public VMGalleryVMCreationOperation(
IDownloaderService downloaderService,
IArchiveProviderFactory archiveProviderFactory,
IHyperVManager hyperVManager,
QuickCreateUserInput parameters)
VMGalleryCreationUserInput parameters)
{
_stringResource = stringResource;
_vmGalleryService = vmGalleryService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HyperVExtension.Helpers;

namespace HyperVExtension.Models;
Expand All @@ -14,7 +9,7 @@ public class VirtualMachineCreationParameters
{
public string Name { get; private set; } = string.Empty;

// 4Gb in bytes. We use the same default value as Hyper-V Quick Create feature.
// 4Gb in bytes. We use the same default value as the Hyper-V Managers Quick Create feature.
public long MemoryStartupBytes { get; private set; } = 4096L << 20;

public string VHDPath { get; private set; } = string.Empty;
Expand Down
14 changes: 6 additions & 8 deletions HyperVExtension/src/HyperVExtension/Providers/HyperVProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

using System.Text.Json;
using HyperVExtension.Common;
using HyperVExtension.Exceptions;
using HyperVExtension.Helpers;
using HyperVExtension.Models.QuickCreateJsonToClasses;
using HyperVExtension.Models.VirtualMachineCreation;
using HyperVExtension.Services;
using Microsoft.Windows.DevHome.SDK;
Expand All @@ -22,16 +20,16 @@ public class HyperVProvider : IComputeSystemProvider

private readonly IHyperVManager _hyperVManager;

private readonly QuickCreateOperationFactory _quickCreateOperationFactory;
private readonly VmGalleryCreationOperationFactory _vmGalleryCreationOperationFactory;

// Temporary will need to add more error strings for different operations.
public string OperationErrorString => _stringResource.GetLocalized(errorResourceKey);

public HyperVProvider(IHyperVManager hyperVManager, IStringResource stringResource, QuickCreateOperationFactory quickCreateOperationFactory)
public HyperVProvider(IHyperVManager hyperVManager, IStringResource stringResource, VmGalleryCreationOperationFactory vmGalleryCreationOperationFactory)
{
_hyperVManager = hyperVManager;
_stringResource = stringResource;
_quickCreateOperationFactory = quickCreateOperationFactory;
_vmGalleryCreationOperationFactory = vmGalleryCreationOperationFactory;
}

/// <summary> Gets or sets the default compute system properties. </summary>
Expand Down Expand Up @@ -91,12 +89,12 @@ public ComputeSystemAdaptiveCardResult CreateAdaptiveCardSessionForComputeSystem
{
try
{
if (JsonSerializer.Deserialize(inputJson, typeof(QuickCreateUserInput)) is not QuickCreateUserInput quickCreateInput)
if (JsonSerializer.Deserialize(inputJson, typeof(VMGalleryCreationUserInput)) is not VMGalleryCreationUserInput userInput)
{
throw new InvalidOperationException($"Failed to deserialize the input json {inputJson} to {nameof(QuickCreateUserInput)} object.");
throw new InvalidOperationException($"Failed to deserialize the input json {inputJson} to {nameof(VMGalleryCreationUserInput)} object.");
}

return _quickCreateOperationFactory(quickCreateInput);
return _vmGalleryCreationOperationFactory(userInput);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HyperVExtension.Common;
using HyperVExtension.Models.VirtualMachineCreation;
using Windows.Storage;

namespace HyperVExtension.Services;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Threading;
using HyperVExtension.Extensions;
using HyperVExtension.Models.VirtualMachineCreation;
using Windows.Storage;

namespace HyperVExtension.Services;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HyperVExtension.Models.VirtualMachineCreation;
using Windows.Foundation;
using Windows.Networking.BackgroundTransfer;
using Windows.Storage;

namespace HyperVExtension.Services;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using HyperVExtension.Models.QuickCreateJsonToClasses;
using HyperVExtension.Models.VMGalleryJsonToClasses;
using Windows.Storage;

namespace HyperVExtension.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Security.Cryptography;
using System.Text.Json;
using HyperVExtension.Models.QuickCreateJsonToClasses;
using HyperVExtension.Models.VMGalleryJsonToClasses;
using HyperVExtension.Providers;
using Windows.Storage;

Expand Down Expand Up @@ -31,7 +31,7 @@ public VMGalleryService(IDownloaderService downloaderService)
_downloaderService = downloaderService;
}

private static readonly Uri HyperVQuickCreateUrl = new("https://go.microsoft.com/fwlink/?linkid=851584");
private static readonly Uri VmGalleryUrl = new("https://go.microsoft.com/fwlink/?linkid=851584");

private VMGalleryImageList _imageList = new();

Expand All @@ -53,7 +53,7 @@ public async Task<VMGalleryImageList> GetGalleryImagesAsync()
cancellationTokenSource.CancelAfter(TimeSpan.FromMinutes(5));

// get the JSON data
var resultJson = await _downloaderService.DownloadStringAsync(HyperVQuickCreateUrl.AbsoluteUri, cancellationTokenSource.Token);
var resultJson = await _downloaderService.DownloadStringAsync(VmGalleryUrl.AbsoluteUri, cancellationTokenSource.Token);
_imageList = JsonSerializer.Deserialize(resultJson, typeof(VMGalleryImageList), _jsonOptions) as VMGalleryImageList ?? emptyList;

// Now we need to download the base64 images for the symbols (icons). So they can be used within an adaptive card.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
using HyperVExtension.Common.Extensions;
using HyperVExtension.Helpers;
using HyperVExtension.Models;
using HyperVExtension.Models.QuickCreateJsonToClasses;
using HyperVExtension.Models.VirtualMachineCreation;
using HyperVExtension.Models.VMGalleryJsonToClasses;
using HyperVExtension.Providers;
using HyperVExtension.Services;
using HyperVExtension.UnitTest.Mocks;
Expand Down Expand Up @@ -98,7 +98,7 @@ private IHost CreateTestHost()
services.AddSingleton<IPowerShellService>(psService =>
ActivatorUtilities.CreateInstance<PowerShellService>(psService, new PowerShellSession()));
services.AddSingleton<HyperVVirtualMachineFactory>(serviceProvider => psObject => ActivatorUtilities.CreateInstance<HyperVVirtualMachine>(serviceProvider, psObject));
services.AddSingleton<QuickCreateOperationFactory>(serviceProvider => parameters => ActivatorUtilities.CreateInstance<VMGalleryVMCreationOperation>(serviceProvider, parameters));
services.AddSingleton<VmGalleryCreationOperationFactory>(serviceProvider => parameters => ActivatorUtilities.CreateInstance<VMGalleryVMCreationOperation>(serviceProvider, parameters));
services.AddTransient<IWindowsServiceController, WindowsServiceController>();
}).Build();
Expand Down Expand Up @@ -385,22 +385,22 @@ public void TestDevSetupAgentDeployment()
/// that have Hyper-V enabled and running.
/// </summary>
[TestMethod]
public async Task TestQuickCreateVirtualMachineCreation()
public async Task TestVirtualMachineCreationForVmGallery()
{
var vmGalleryService = TestHost!.GetService<IVMGalleryService>();
var hyperVProvider = TestHost!.GetService<IComputeSystemProvider>();
var expectedVMName = "New Windows 11 VM for Integration test";
var imageList = await vmGalleryService.GetGalleryImagesAsync();
var smallestImageIndex = await GetIndexOfImageWithSmallestRequiredSpace(imageList);
var quickCreateInputJson = JsonSerializer.Serialize(new QuickCreateUserInput()
var inputJson = JsonSerializer.Serialize(new VMGalleryCreationUserInput()
{
NewVirtualMachineName = expectedVMName,

// Get Image with the smallest size from gallery, we'll use it to create a VM.
SelectedImageListIndex = smallestImageIndex,
});

var createComputeSystemOperation = hyperVProvider.CreateCreateComputeSystemOperation(null, quickCreateInputJson);
var createComputeSystemOperation = hyperVProvider.CreateCreateComputeSystemOperation(null, inputJson);
createComputeSystemOperation!.Progress += OnProgressReceived;

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Moq;
using Moq.Language;
using Moq.Protected;
using Windows.Networking.BackgroundTransfer;

namespace HyperVExtension.UnitTest.HyperVExtensionTests.Services;

Expand Down Expand Up @@ -177,7 +176,7 @@ private IHost CreateTestHost()
ActivatorUtilities.CreateInstance<WindowsServiceControllerMock>(controller, VirtualMachineManagementServiceStatus));
services.AddSingleton<HyperVVirtualMachineFactory>(serviceProvider => psObject => ActivatorUtilities.CreateInstance<HyperVVirtualMachine>(serviceProvider, psObject));
services.AddSingleton<QuickCreateOperationFactory>(serviceProvider => parameters => ActivatorUtilities.CreateInstance<VMGalleryVMCreationOperation>(serviceProvider, parameters));
services.AddSingleton<VmGalleryCreationOperationFactory>(serviceProvider => parameters => ActivatorUtilities.CreateInstance<VMGalleryVMCreationOperation>(serviceProvider, parameters));
}).Build();
}

Expand Down
Loading

0 comments on commit f1be31d

Please sign in to comment.