Repository: microsoft/onefuzz
File: src/ApiService/ApiService/onefuzzlib/ImageReference.cs (C#)

Code before:

    public static OneFuzzResult<ImageReference> TryParse(string image) {
        ResourceIdentifier identifier;
        ImageReference result;
        try {
            // see if it is a valid ARM resource identifier:
            identifier = new ResourceIdentifier(image);
            if (identifier.ResourceType == SharedGalleryImageResource.ResourceType) {
                result = new LatestSharedGalleryImage(identifier);
            } else if (identifier.ResourceType == SharedGalleryImageVersionResource.ResourceType) {
                result = new SharedGalleryImage(identifier);
            } else if (identifier.ResourceType == GalleryImageVersionResource.ResourceType) {
                result = new GalleryImage(identifier);
            } else if (identifier.ResourceType == GalleryImageResource.ResourceType) {
                result = new LatestGalleryImage(identifier);
            } else if (identifier.ResourceType == ImageResource.ResourceType) {
                result = new Image(identifier);
            } else {
                return Error.Create(
                    ErrorCode.INVALID_IMAGE,
                    $"Unknown image resource type: {identifier.ResourceType}");
            }
        } catch (FormatException) {
            // not an ARM identifier, try to parse a marketplace image:
            var imageParts = image.Split(":");
            // The python code would throw if more than 4 parts are foud in the split
            if (imageParts.Length != 4) {
                return Error.Create(
                    ErrorCode.INVALID_IMAGE, $"Expected 4 ':' separated parts in '{image}'");
            }

            result = new Marketplace(
                    Publisher: imageParts[0],
                    Offer: imageParts[1],
                    Sku: imageParts[2],
                    Version: imageParts[3]);
        }

        return OneFuzzResult.Ok(result);
    }

    // region is not part of the key as it should not make a difference to the OS type
    // it is only used for marketplace images
    private sealed record CacheKey(string image);
    public Task<OneFuzzResult<Os>> GetOs(IMemoryCache cache, ArmClient armClient, Region region) {
        return cache.GetOrCreateAsync(new CacheKey(ToString()), entry => {
            // this should essentially never change
            // the user would have to delete the image and recreate it with the same name but
            // a different OS, which would be very unusual
            entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1);
            return GetOsUncached(armClient, region);

Review comment (suggestion):
accidental deletion
```suggestion
            // The python code would throw if more than 4 parts are found in the split
```

Code after:

    public static OneFuzzResult<ImageReference> TryParse(string image) {
        ResourceIdentifier identifier;
        ImageReference result;
        try {
            // see if it is a valid ARM resource identifier:
            identifier = new ResourceIdentifier(image);
            if (identifier.ResourceType == SharedGalleryImageResource.ResourceType) {
                result = new LatestSharedGalleryImage(identifier);
            } else if (identifier.ResourceType == SharedGalleryImageVersionResource.ResourceType) {
                result = new SharedGalleryImage(identifier);
            } else if (identifier.ResourceType == GalleryImageVersionResource.ResourceType) {
                result = new GalleryImage(identifier);
            } else if (identifier.ResourceType == GalleryImageResource.ResourceType) {
                result = new LatestGalleryImage(identifier);
            } else if (identifier.ResourceType == ImageResource.ResourceType) {
                result = new Image(identifier);
            } else {
                return Error.Create(
                    ErrorCode.INVALID_IMAGE,
                    $"Unknown image resource type: {identifier.ResourceType}");
            }
        } catch (FormatException) {
            // not an ARM identifier, try to parse a marketplace image:
            var imageParts = image.Split(":");
            // The python code would throw if more than 4 parts are found in the split
            if (imageParts.Length != 4) {
                return Error.Create(
                    ErrorCode.INVALID_IMAGE, $"Expected 4 ':' separated parts in '{image}'");
            }

            result = new Marketplace(
                    Publisher: imageParts[0],
                    Offer: imageParts[1],
                    Sku: imageParts[2],
                    Version: imageParts[3]);
        }

        return OneFuzzResult.Ok(result);
    }

    // region is not part of the key as it should not make a difference to the OS type
    // it is only used for marketplace images
    private sealed record CacheKey(string image);
    public Task<OneFuzzResult<Os>> GetOs(IMemoryCache cache, ArmClient armClient, Region region) {
        return cache.GetOrCreateAsync(new CacheKey(ToString()), entry => {
            // this should essentially never change
            // the user would have to delete the image and recreate it with the same name but
            // a different OS, which would be very unusual
            entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1);
            return GetOsUncached(armClient, region);