wide-crayon-4093
09/07/2021, 5:51 AMpgaConfig := cluster.pgaPassword.Result.ApplyT(func(pgaPassword string) string {
println(fmt.Sprintf("%v", cluster.cfg.Databases))
return cluster.preparePgaConfig(pgaCfg.ApiKey, pgaPassword)
}).(pulumi.StringOutput)
if I have many 'cluster' resources, e.g iterating over an array of clusters and generating resources, then inside ApplyT preparePgaConfig
is called on the last cluster, not each. How do I pass the 'context' in there?bored-table-20691
09/07/2021, 6:08 AMwide-crayon-4093
09/07/2021, 6:25 AMfunc SetupPostgres(ctx *pulumi.Context, args *DataArgs, opts ...pulumi.ResourceOption) error {
var postgresCfg ConfigPostgres
config.New(ctx, "data").RequireObject("postgres", &postgresCfg)
for _, instanceCfg := range postgresCfg.Instances {
err := setupPostgresInstance(ctx, &instanceCfg, args, opts...)
if err != nil {
return err
}
}
return nil
}
func setupPostgresInstance(
ctx *pulumi.Context, cfg *ConfigPostgresInstance, args *DataArgs, opts ...pulumi.ResourceOption,
) error {
instance := &PostgresInstance{
Name: cfg.Name,
InstanceName: fmt.Sprintf("postgres-%s", cfg.Name),
}
instance.pgaPasswordName = fmt.Sprintf("%s-pga", instance.InstanceName)
instance.pgaPassword, err = random.NewRandomPassword(ctx, instance.pgaPasswordName, &random.RandomPasswordArgs{
Length: <http://pulumi.Int|pulumi.Int>(24),
Special: pulumi.Bool(false),
}, opts...)
if err != nil {
return err
}
pgaConfig := instance.pgaPassword.Result.ApplyT(func(pgaPassword string) string {
// instance here is the last element, not the current
return instance.preparePgaConfig(pgaCfg.ApiKey, pgaPassword)
}).(pulumi.StringOutput)
return nil
}
bored-table-20691
09/07/2021, 6:36 AMApplyT
callback function will not be invoked until it’s ready, and at that point, you’re referring to the last iteration of the loop most likelywide-crayon-4093
09/07/2021, 6:37 AM