https://pulumi.com logo
Title
w

worried-knife-31967

10/11/2021, 7:22 PM
currently I'm resorting to
FunctionApp lastApp = null!;

            foreach (var app in apps)
            {
                ComponentResourceOptions options = lastApp == null ? null! : new ComponentResourceOptions {
                    DependsOn = {
                        lastApp
                    }
                };

                var cosmosDbRoles = new CosmosDbRoles($"{StackConstants.ApplicationName.ToLower()}-{app.Key}-cosmosdbroles",
                    new CosmosDbRoleArgs()
                    {
                        ResourceGroupName = globalResourceGroup.Name,
                        FunctionIdentity = app.Value.AppIdentity,
                        CosmosDatabaseAccountName = cosmos.CosmosDatabaseAccountName,
                        CosmosDatabaseAccountId = cosmos.CosmosDatabaseAccountId
                    }, options);
                 lastApp = app.Value;
            }
s

steep-toddler-94095

10/11/2021, 7:50 PM
have you tried a
reduce
(or whatever the equivalent is in .Net -
Aggregate
?)
w

worried-knife-31967

10/11/2021, 8:16 PM
the issue is passing the dependson down to the resources in order
s

steep-toddler-94095

10/11/2021, 8:33 PM
actually after looking a bit more at your code i think a simple 
map
 would work (this is pseudocode since I don't know .Net languages)
const cosmosStuff = apps.map((app, index) => {
    return {
        componentResourceOptions: index == 0 ? null! : new ComponentResourceOptions { DependsOn = { apps[index-1].Value }};
        cosmosDbRoles: new CosmosDbRoles($"{StackConstants.ApplicationName.ToLower()}-{app.Key}-cosmosdbroles",
            new CosmosDbRoleArgs() {
                ResourceGroupName = globalResourceGroup.Name,
                FunctionIdentity = app.Value.AppIdentity,
                CosmosDatabaseAccountName = cosmos.CosmosDatabaseAccountName,
                CosmosDatabaseAccountId = cosmos.CosmosDatabaseAccountId
            },
            options
        );
    }
})
w

worried-knife-31967

10/11/2021, 8:55 PM
The only difference is the index, which is admittedly marginally nicer, but still, that doesn't feel like the best option.
s

steep-toddler-94095

10/11/2021, 9:05 PM
it also removes mutability and allows access to the 
new
 resources in the returned array. there might be a better way if given more specifics about the problem you're trying to solve, but without that this will be the simplest it gets
b

bored-oyster-3147

10/12/2021, 1:35 AM
You could do LINQ Aggregate like @steep-toddler-94095 originally suggested. You would set the starting value to null and return the current item in the sequence each time. Then you could do it in a function without the for loop or the indexing. But I also don't know why this makes you feel dirty. It is what it is.
w

worried-knife-31967

10/12/2021, 8:00 PM
I suppose its because I want a nice factory style apply method... l have a go at a wrapper though.