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

Code before:
        }
        return entry;
    }

    public async Async.Task<AutoScale?> GetSettingsForScaleset(Guid scalesetId) {
        try {
            var autoscale = await GetEntityAsync(scalesetId.ToString(), scalesetId.ToString());
            return autoscale;
        } catch (Exception ex) {
            _logTracer.Exception(ex, "Failed to get auto-scale entity");
            return null;
        }
    }

    public async Async.Task<OneFuzzResult<AutoscaleProfile>> GetAutoScaleProfile(Guid scalesetId) {
        _logTracer.Info($"getting scaleset for existing auto-scale resources {scalesetId}");
        var settings = _context.Creds.GetResourceGroupResource().GetAutoscaleSettings();
        if (settings is null) {
            return OneFuzzResult<AutoscaleProfile>.Error(ErrorCode.INVALID_CONFIGURATION, $"could not find any auto-scale settings for the resource group");
        } else {
            await foreach (var setting in settings.GetAllAsync()) {

                if (setting.Data.TargetResourceId.EndsWith(scalesetId.ToString())) {
                    if (setting.Data.Profiles.Count != 1) {
                        return OneFuzzResult<AutoscaleProfile>.Error(ErrorCode.INVALID_CONFIGURATION, $"found {setting.Data.Profiles.Count} auto-scale profiles for {scalesetId}");
                    } else {
                        return OneFuzzResult<AutoscaleProfile>.Ok(setting.Data.Profiles.First());
                    }
                }
            }
        }
        return OneFuzzResult<AutoscaleProfile>.Error(ErrorCode.INVALID_CONFIGURATION, $"could not find auto-scale settings for scaleset {scalesetId}");
    }

    public async Async.Task<OneFuzzResultVoid> AddAutoScaleToVmss(Guid vmss, AutoscaleProfile autoScaleProfile) {
        _logTracer.Info($"Checking scaleset {vmss} for existing auto scale resource");

        var existingAutoScaleResource = GetAutoscaleSettings(vmss);

        if (!existingAutoScaleResource.IsOk) {
            return OneFuzzResultVoid.Error(existingAutoScaleResource.ErrorV);
        }

        if (existingAutoScaleResource.OkV != null) {
            return OneFuzzResultVoid.Ok;
        }

        var autoScaleResource = await CreateAutoScaleResourceFor(vmss, await _context.Creds.GetBaseRegion(), autoScaleProfile);
        if (!autoScaleResource.IsOk) {
            return OneFuzzResultVoid.Error(autoScaleResource.ErrorV);
        }

Review comment (suggestion):
In the python code, it's not an error for there to be more than 1 auto scale profile
https://github.com/microsoft/onefuzz/blob/3f86fc8689c5b1785ce9048bc6fded629dbb68d0/src/api-service/__app__/onefuzzlib/azure/auto_scale.py#L245-L253

Code after:
            _logTracer.Error($"Failed to save auto-scale record for scaleset ID: {scalesetId}, minAmount: {minAmount}, maxAmount: {maxAmount}, defaultAmount: {defaultAmount}, scaleOutAmount: {scaleOutAmount}, scaleOutCooldown: {scaleOutCooldown}, scaleInAmount: {scaleInAmount}, scaleInCooldown: {scaleInCooldown}");
        }
        return entry;
    }

    public async Async.Task<AutoScale?> GetSettingsForScaleset(Guid scalesetId) {
        try {
            var autoscale = await GetEntityAsync(scalesetId.ToString(), scalesetId.ToString());
            return autoscale;
        } catch (Exception ex) {
            _logTracer.Exception(ex, "Failed to get auto-scale entity");
            return null;
        }
    }

    public async Async.Task<OneFuzzResult<AutoscaleProfile>> GetAutoScaleProfile(Guid scalesetId) {
        _logTracer.Info($"getting scaleset for existing auto-scale resources {scalesetId}");
        var settings = _context.Creds.GetResourceGroupResource().GetAutoscaleSettings();
        if (settings is null) {
            return OneFuzzResult<AutoscaleProfile>.Error(ErrorCode.INVALID_CONFIGURATION, $"could not find any auto-scale settings for the resource group");
        } else {
            await foreach (var setting in settings.GetAllAsync()) {

                if (setting.Data.TargetResourceId.EndsWith(scalesetId.ToString())) {
                    if (setting.Data.Profiles.IsNullOrEmpty()) {
                        return OneFuzzResult<AutoscaleProfile>.Error(ErrorCode.INVALID_CONFIGURATION, $"found {setting.Data.Profiles.Count} auto-scale profiles for {scalesetId}");
                    } else {
                        if (setting.Data.Profiles.Count != 1) {
                            _logTracer.Warning($"Found more than one autoscaling profile for scaleset {scalesetId}");
                        }
                        return OneFuzzResult<AutoscaleProfile>.Ok(setting.Data.Profiles.First());
                    }
                }
            }
        }
        return OneFuzzResult<AutoscaleProfile>.Error(ErrorCode.INVALID_CONFIGURATION, $"could not find auto-scale settings for scaleset {scalesetId}");
    }

    public async Async.Task<OneFuzzResultVoid> AddAutoScaleToVmss(Guid vmss, AutoscaleProfile autoScaleProfile) {
        _logTracer.Info($"Checking scaleset {vmss} for existing auto scale resource");

        var existingAutoScaleResource = GetAutoscaleSettings(vmss);

        if (!existingAutoScaleResource.IsOk) {
            return OneFuzzResultVoid.Error(existingAutoScaleResource.ErrorV);
        }

        if (existingAutoScaleResource.OkV != null) {
            return OneFuzzResultVoid.Ok;
        }