https://pulumi.com logo
Title
s

silly-smartphone-71988

06/07/2022, 11:46 AM
Hi, I'm trying to keep a List of a CustomResource descendent in a class to be used after to set DependsOn for other objects creation I need to pass the name of the ressource as a Output<string> and return the object from the list corresponding. I'm stuck because I always get null. Or if I use apply I get an Output<ProtectionContainer> which cannot be used for the DependsOn Any help welcomed :) Thank you
public class Sample : ComponentResource
{
	private List<Pulumi.AzureNative.RecoveryServices.ProtectionContainer> protectionContainers;
	
	public Sample(ResourceName resourceName, ComponentResourceOptions? options = null) : base(resourceName.Type, resourceName.Name, options)
	{
		this.protectionContainers = new List<Pulumi.AzureNative.RecoveryServices.ProtectionContainer>();        
	}
	
	public Create(Output<string> name)
	{
		
        var protectionContainer = new RecoveryServices.ProtectionContainer("fooName", new RecoveryServices.ProtectionContainerArgs
        {            
            ContainerName = name,
            FabricName = "Azure",
            Properties = new Pulumi.AzureNative.RecoveryServices.Inputs.AzureWorkloadContainerArgs
            {
                [...]
            },
            ResourceGroupName = this.args.ResourceGroup,
            VaultName = this.recoveryServicesVault.Name.Apply(t => t)
        }
		this.protectionContainers.Add(protectionContainer);
	}
	
	public WithBackupFileShare(Output<string> protectedcontainername)
	{
		var protectionContainer = GetProtectionContainer(protectionContainerName); //always gets null
		var _ = new RecoveryServices.ProtectedItem(args.Name, new RecoveryServices.ProtectedItemArgs
        {
            ContainerName = protectionContainerName,
            [...]
        }, 
        new CustomResourceOptions { DependsOn = {
			protectionContainer  //need a Resource descendent here thus why I need a RecoveryServices.ProtectionContainer
			}, Parent = protectionContainer });
	}
	
	public RecoveryServices.ProtectionContainer? GetProtectionContainer(Output<string> containerName)
    {
		//how to get back the RecoveryServices.ProtectionContainer from this.protectionContainers?
		//when using a for each I have to use item.Name.Apply to compare the name and this returns an Output<> not a RecoveryServices.ProtectionContainer
		//i tried to set a local variable from within the apply but it does not work. the variable stays as null
		RecoveryServices.ProtectionContainer? protectionContainer = null;
		foreach (var item in this.protectionContainers)
        {
			var test = Output.Tuple<string,string,RecoveryServices.ProtectionContainer>(item.Name, containerName,item).Apply(t =>
             {             
                if (t.Item1 == t.Item2) {
                    protectionContainer = t.Item3; //does not work protectionContainer stays null
                    return t.Item3; //
                    }
                else{
                    return null;
                }
            });
			protectionContainer = test; //does not work because var is of type Output<RecoveryServices.ProtectionContainer>
		}
		return protectionContainer; //returns null
	}
}
1
Somehow for each does not work... but a while loop with a counter works
public RecoveryServices.ProtectionContainer? GetProtectionContainer(Output<string> containerName)
{ int idx = -1; int counter = 0; while (idx == -1 && counter < this.protectionContainers.Count) { _ = Output.Tuple(this.protectionContainers[counter].Name, containerName).Apply(t => { if (t.Item1 == t.Item2) { idx = counter; return true; } else{ return false; } }); } if(idx != -1) { return this.protectionContainers[idx]; } else { return null; }
Oh well false joy... I had forgotten to increment the counter in the while loop like a newb... sorry about that. So it does not work. Anybody has an idea why idx is set from within the apply but when it is used after the while loop its still -1?