This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extensions functionality for C# refactor (#2218)
* Add extensions functionality * Small cleanup * Use context, fix typo
- Loading branch information
Showing
8 changed files
with
373 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
src/ApiService/ApiService/onefuzzlib/ImageOperations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System.Threading.Tasks; | ||
using Azure; | ||
using Azure.ResourceManager.Compute; | ||
|
||
namespace Microsoft.OneFuzz.Service; | ||
|
||
public interface IImageOperations { | ||
public Async.Task<OneFuzzResult<Os>> GetOs(string region, string image); | ||
} | ||
|
||
public class ImageOperations : IImageOperations { | ||
private IOnefuzzContext _context; | ||
private ILogTracer _logTracer; | ||
|
||
public ImageOperations(ILogTracer logTracer, IOnefuzzContext context) { | ||
_logTracer = logTracer; | ||
_context = context; | ||
} | ||
public async Task<OneFuzzResult<Os>> GetOs(string region, string image) { | ||
string? name = null; | ||
try { | ||
var parsed = _context.Creds.ParseResourceId(image); | ||
parsed = await _context.Creds.GetData(parsed); | ||
if (string.Equals(parsed.Id.ResourceType, "galleries", StringComparison.OrdinalIgnoreCase)) { | ||
try { | ||
// This is not _exactly_ the same as the python code | ||
// because in C# we don't have access to child_name_1 | ||
var gallery = await _context.Creds.GetResourceGroupResource().GetGalleries().GetAsync( | ||
parsed.Data.Name | ||
); | ||
|
||
var galleryImage = gallery.Value.GetGalleryImages() | ||
.ToEnumerable() | ||
.Where(galleryImage => string.Equals(galleryImage.Id, parsed.Id, StringComparison.OrdinalIgnoreCase)) | ||
.First(); | ||
|
||
galleryImage = await galleryImage.GetAsync(); | ||
|
||
name = galleryImage.Data?.OSType?.ToString().ToLowerInvariant()!; | ||
|
||
} catch (Exception ex) when ( | ||
ex is RequestFailedException || | ||
ex is NullReferenceException | ||
) { | ||
return OneFuzzResult<Os>.Error( | ||
ErrorCode.INVALID_IMAGE, | ||
ex.ToString() | ||
); | ||
} | ||
} else { | ||
try { | ||
name = (await _context.Creds.GetResourceGroupResource().GetImages().GetAsync( | ||
parsed.Data.Name | ||
)).Value.Data.StorageProfile.OSDisk.OSType.ToString().ToLowerInvariant(); | ||
} catch (Exception ex) when ( | ||
ex is RequestFailedException || | ||
ex is NullReferenceException | ||
) { | ||
return OneFuzzResult<Os>.Error( | ||
ErrorCode.INVALID_IMAGE, | ||
ex.ToString() | ||
); | ||
} | ||
} | ||
} catch (FormatException) { | ||
var imageParts = image.Split(":"); | ||
|
||
// The python code would throw if more than 4 parts are found in the split | ||
System.Diagnostics.Trace.Assert(imageParts.Length == 4, $"Expected 4 ':' separated parts in {image}"); | ||
|
||
var publisher = imageParts[0]; | ||
var offer = imageParts[1]; | ||
var sku = imageParts[2]; | ||
var version = imageParts[3]; | ||
|
||
try { | ||
var subscription = await _context.Creds.ArmClient.GetDefaultSubscriptionAsync(); | ||
if (string.Equals(version, "latest", StringComparison.Ordinal)) { | ||
version = (await subscription.GetVirtualMachineImagesAsync( | ||
region, | ||
publisher, | ||
offer, | ||
sku, | ||
top: 1 | ||
).FirstAsync()).Name; | ||
} | ||
|
||
name = (await subscription.GetVirtualMachineImageAsync( | ||
region, | ||
publisher, | ||
offer, | ||
sku | ||
, version | ||
)).Value.OSDiskImageOperatingSystem.ToString().ToLower(); | ||
} catch (RequestFailedException ex) { | ||
return OneFuzzResult<Os>.Error( | ||
ErrorCode.INVALID_IMAGE, | ||
ex.ToString() | ||
); | ||
} | ||
} | ||
|
||
if (name != null) { | ||
name = string.Concat(name[0].ToString().ToUpper(), name.AsSpan(1)); | ||
if (Enum.TryParse(name, out Os os)) { | ||
return OneFuzzResult<Os>.Ok(os); | ||
} | ||
} | ||
|
||
return OneFuzzResult<Os>.Error( | ||
ErrorCode.INVALID_IMAGE, | ||
$"Unexpected image os type: {name}" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/ApiService/ApiService/onefuzzlib/VmExtensionWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Azure.Core; | ||
using Azure.ResourceManager.Compute; | ||
|
||
namespace Microsoft.OneFuzz.Service { | ||
public class VMExtensionWrapper { | ||
public AzureLocation? Location { get; init; } | ||
public string? Name { get; init; } | ||
public string? TypePropertiesType { get; init; } | ||
public string? Publisher { get; init; } | ||
public string? TypeHandlerVersion { get; init; } | ||
public string? ForceUpdateTag { get; init; } | ||
public bool? AutoUpgradeMinorVersion { get; init; } | ||
public bool? EnableAutomaticUpgrade { get; init; } | ||
public BinaryData? Settings { get; init; } | ||
public BinaryData? ProtectedSettings { get; init; } | ||
|
||
public (string, VirtualMachineExtensionData) GetAsVirtualMachineExtension() { | ||
if (Location == null) { // EnsureNotNull does not satisfy the nullability checker | ||
throw new ArgumentNullException("Location required for VirtualMachineExtension"); | ||
} | ||
TypePropertiesType.EnsureNotNull("TypePropertiesType required for VirtualMachineExtension"); | ||
Publisher.EnsureNotNull("Publisher required for VirtualMachineExtension"); | ||
TypeHandlerVersion.EnsureNotNull("TypeHandlerVersion required for VirtualMachineExtension"); | ||
AutoUpgradeMinorVersion.EnsureNotNull("AutoUpgradeMinorVersion required for VirtualMachineExtension"); | ||
Settings.EnsureNotNull("Settings required for VirtualMachineExtension"); | ||
ProtectedSettings.EnsureNotNull("ProtectedSettings required for VirtualMachineExtension"); | ||
|
||
return (Name!, new VirtualMachineExtensionData(Location.Value) { | ||
TypePropertiesType = TypePropertiesType, | ||
Publisher = Publisher, | ||
TypeHandlerVersion = TypeHandlerVersion, | ||
AutoUpgradeMinorVersion = AutoUpgradeMinorVersion, | ||
EnableAutomaticUpgrade = EnableAutomaticUpgrade, | ||
ForceUpdateTag = ForceUpdateTag, | ||
Settings = Settings, | ||
ProtectedSettings = ProtectedSettings | ||
}); | ||
} | ||
|
||
public VirtualMachineScaleSetExtensionData GetAsVirtualMachineScaleSetExtension() { | ||
Name.EnsureNotNull("Name required for VirtualMachineScaleSetExtension"); | ||
TypePropertiesType.EnsureNotNull("TypePropertiesType required for VirtualMachineScaleSetExtension"); | ||
Publisher.EnsureNotNull("Publisher required for VirtualMachineScaleSetExtension"); | ||
TypeHandlerVersion.EnsureNotNull("TypeHandlerVersion required for VirtualMachineScaleSetExtension"); | ||
AutoUpgradeMinorVersion.EnsureNotNull("AutoUpgradeMinorVersion required for VirtualMachineScaleSetExtension"); | ||
Settings.EnsureNotNull("Settings required for VirtualMachineScaleSetExtension"); | ||
ProtectedSettings.EnsureNotNull("ProtectedSettings required for VirtualMachineScaleSetExtension"); | ||
return new VirtualMachineScaleSetExtensionData() { | ||
Name = Name, | ||
TypePropertiesType = TypePropertiesType, | ||
Publisher = Publisher, | ||
TypeHandlerVersion = TypeHandlerVersion, | ||
AutoUpgradeMinorVersion = AutoUpgradeMinorVersion, | ||
EnableAutomaticUpgrade = EnableAutomaticUpgrade, | ||
ForceUpdateTag = ForceUpdateTag, | ||
Settings = Settings, | ||
ProtectedSettings = ProtectedSettings | ||
}; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters