0

When using the Microsoft.Azure.Management.ResourceManager.Fluent library to enumerate containers in a resource group:

azure.ContainerGroups.ListByResourceGroup(resouceGroup)

Returns an enumator, but doing any operation (e.g. .ToList()) on the enumator will throw:

Exception thrown: 'Microsoft.Rest.Azure.CloudException' in System.Private.CoreLib.dll The Resource 'Microsoft.ContainerInstance/containerGroups/myResource' under resource group 'myResourceGroup' was not found.

Red Riding Hood
  • 1,932
  • 1
  • 17
  • 36

1 Answers1

2

I solved it by rebuilding the enumerator, as it throws in the MoveNext() operation. However the enumerator iterator still increases, effectively skipping the missing resource.

    private static List<IContainerGroup> getContainerInstances(IAzure azure, string resouceGroup)
    {
        var brokenEnumerator = azure.ContainerGroups.ListByResourceGroup(resouceGroup).GetEnumerator();
        var containerInstances = new List<IContainerGroup>();

        while (true)
        {
            try
            {
                if (!brokenEnumerator.MoveNext())
                {
                    break;
                }
                containerInstances.Add(brokenEnumerator.Current);
            }
            catch (CloudException)
            {
                // noop
            }
        }

        return containerInstances;
    }
Red Riding Hood
  • 1,932
  • 1
  • 17
  • 36