Skip to content

Commit

Permalink
Add explicit dependencies between resources using dependsOn
Browse files Browse the repository at this point in the history
  • Loading branch information
KnockOutEZ committed Oct 22, 2024
1 parent 10f5b2c commit 2645817
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 70 deletions.
57 changes: 50 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,18 @@ const cloudAccount = new pgedge.CloudAccount("exampleCloudAccount", {
credentials: {
role_arn: "arn:aws:iam::21112529deae39:role/pgedge-135232c",
},
}, { dependsOn: sshKey });
}, {
dependsOn: [sshKey]
});
// Create a Backup Store
const backupStore = new pgedge.BackupStore("exampleBackupStore", {
name: "example",
cloudAccountId: cloudAccount.id,
region: "us-west-2",
}, { dependsOn: cloudAccount });
}, {
dependsOn: [cloudAccount]
});
// Create a Cluster
const cluster = new pgedge.Cluster("exampleCluster", {
Expand All @@ -130,6 +134,7 @@ const cluster = new pgedge.Cluster("exampleCluster", {
regions: ["us-west-2", "us-east-1", "eu-central-1"],
nodeLocation: "public",
sshKeyId: sshKey.id,
backupStoreIds: [backupStore.id],
nodes: [
{
name: "n1",
Expand Down Expand Up @@ -173,15 +178,16 @@ const cluster = new pgedge.Cluster("exampleCluster", {
// privateSubnets: ["10.3.0.0/24"],
},
],
backupStoreIds: [backupStore.id],
firewallRules: [
{
name: "postgres",
port: 5432,
sources: ["123.456.789.0/32"],
},
],
}, { dependsOn: backupStore });
}, {
dependsOn: [sshKey, cloudAccount, backupStore]
});
// Create a Database
const database = new pgedge.Database("exampleDatabase", {
Expand All @@ -198,7 +204,7 @@ const database = new pgedge.Database("exampleDatabase", {
"postgis",
],
},
nodes:{
nodes: {
n1: {
name: "n1",
},
Expand Down Expand Up @@ -229,14 +235,51 @@ const database = new pgedge.Database("exampleDatabase", {
},
]
},
}, { dependsOn: cluster });
}, {
dependsOn: [cluster]
});
// Export the resource IDs
// Export resource IDs
export const sshKeyId = sshKey.id;
export const cloudAccountId = cloudAccount.id;
export const backupStoreId = backupStore.id;
export const clusterId = cluster.id;
export const clusterStatus = cluster.status;
export const clusterCreatedAt = cluster.createdAt;
export const databaseId = database.id;
export const databaseStatus = database.status;
// Optional: Log outputs
pulumi.all([
sshKeyId,
cloudAccountId,
backupStoreId,
clusterId,
clusterStatus,
clusterCreatedAt,
databaseId,
databaseStatus
]).apply(([
sshId,
accountId,
backupId,
cId,
cStatus,
cCreatedAt,
dbId,
dbStatus
]) => {
console.log({
sshKeyId: sshId,
cloudAccountId: accountId,
backupStoreId: backupId,
clusterId: cId,
clusterStatus: cStatus,
clusterCreatedAt: cCreatedAt,
databaseId: dbId,
databaseStatus: dbStatus
});
});
```

### Deploying Your Infrastructure
Expand Down
60 changes: 28 additions & 32 deletions examples/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func main() {
Sources: pulumi.ToStringArray([]string{"192.0.2.44/32"}),
},
},
// backupStoreIds: pulumi.ToStringArray([]string{backupStore.ID()}),
}, pulumi.DependsOn([]pulumi.Resource{backupStore}))
BackupStoreIds: pulumi.ToStringArray([]string{backupStore.ID().ElementType().String()}),
}, pulumi.DependsOn([]pulumi.Resource{sshKey, cloudAccount, backupStore}))
if err != nil {
return err
}
Expand All @@ -132,7 +132,7 @@ func main() {
Name: pulumi.String("n1"),
},
"n2": &pgedge.DatabaseNodesArgs{
Name: pulumi.String("n1"),
Name: pulumi.String("n2"),
},
"n3": &pgedge.DatabaseNodesArgs{
Name: pulumi.String("n3"),
Expand Down Expand Up @@ -168,38 +168,34 @@ func main() {
ctx.Export("clusterId", cluster.ID())
ctx.Export("clusterStatus", cluster.Status)
ctx.Export("clusterCreatedAt", cluster.CreatedAt)
ctx.Export("databaseId", database.ID())
ctx.Export("databaseStatus", database.Status)

// Log outputs
sshKey.ID().ApplyT(func(id string) error {
fmt.Printf("SSH Key ID: %s\n", id)
return nil
})
backupStore.ID().ApplyT(func(id string) error {
fmt.Printf("Backup Store ID: %s\n", id)
return nil
})
cloudAccount.ID().ApplyT(func(id string) error {
fmt.Printf("Cloud Account ID: %s\n", id)
return nil
})
cluster.ID().ApplyT(func(id string) error {
fmt.Printf("Cluster ID: %s\n", id)
logOutputs := func() error {
pulumi.All(
sshKey.ID(),
backupStore.ID(),
cloudAccount.ID(),
cluster.ID(),
cluster.Status,
cluster.CreatedAt,
database.ID(),
database.Status,
).ApplyT(func(args []interface{}) error {
fmt.Printf("SSH Key ID: %s\n", args[0])
fmt.Printf("Backup Store ID: %s\n", args[1])
fmt.Printf("Cloud Account ID: %s\n", args[2])
fmt.Printf("Cluster ID: %s\n", args[3])
fmt.Printf("Cluster Status: %s\n", args[4])
fmt.Printf("Cluster Created At: %s\n", args[5])
fmt.Printf("Database ID: %s\n", args[6])
fmt.Printf("Database Status: %s\n", args[7])
return nil
})
return nil
})
cluster.Status.ApplyT(func(status string) error {
fmt.Printf("Cluster Status: %s\n", status)
return nil
})
cluster.CreatedAt.ApplyT(func(createdAt string) error {
fmt.Printf("Cluster Created At: %s\n", createdAt)
return nil
})
database.Status.ApplyT(func(status string) error {
fmt.Printf("Database Status: %s\n", status)
return nil
})
}

return nil
return logOutputs()
})
}
}
86 changes: 55 additions & 31 deletions examples/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ const cloudAccount = new pgedge.CloudAccount("exampleCloudAccount", {
credentials: {
role_arn: "arn:aws:iam::21112529deae39:role/pgedge-135232c",
},
}, { dependsOn: sshKey });
}, {
dependsOn: [sshKey]
});

const backupStore = new pgedge.BackupStore("exampleBackupStore", {
name: "example",
cloudAccountId: cloudAccount.id,
region: "us-west-2",
}, { dependsOn: cloudAccount });
}, {
dependsOn: [cloudAccount]
});

const cluster = new pgedge.Cluster("exampleCluster", {
name: "example",
cloudAccountId: cloudAccount.id,
regions: ["us-west-2", "us-east-1", "eu-central-1"],
nodeLocation: "public",
sshKeyId: sshKey.id,
backupStoreIds: [backupStore.id],
nodes: [
{
name: "n1",
Expand Down Expand Up @@ -73,15 +78,16 @@ const cluster = new pgedge.Cluster("exampleCluster", {
// privateSubnets: ["10.3.0.0/24"],
},
],
// backupStoreIds: [backupStore.id],
firewallRules: [
{
name: "postgres",
port: 5432,
sources: ["192.0.2.44/32"],
},
],
}, { dependsOn: backupStore });
}, {
dependsOn: [sshKey, cloudAccount, backupStore]
});

const database = new pgedge.Database("exampleDatabase", {
name: "example",
Expand All @@ -100,7 +106,7 @@ const database = new pgedge.Database("exampleDatabase", {
"vector"
],
},
nodes:{
nodes: {
n1: {
name: "n1",
},
Expand All @@ -113,25 +119,25 @@ const database = new pgedge.Database("exampleDatabase", {
},
backups: {
provider: "pgbackrest",
configs: [
{
id: "default",
schedules: [
{
id: "daily-full-backup",
cronExpression: "0 1 * * *",
type: "full",
},
{
id: "hourly-incr-backup",
cronExpression: "0 * * * ?",
type: "incr",
},
]
},
]
configs: [{
id: "default",
schedules: [
{
id: "daily-full-backup",
cronExpression: "0 1 * * *",
type: "full",
},
{
id: "hourly-incr-backup",
cronExpression: "0 * * * ?",
type: "incr",
},
]
}]
},
}, { dependsOn: cluster });
}, {
dependsOn: [cluster]
});

// Outputs
export const sshKeyId = sshKey.id;
Expand All @@ -142,11 +148,29 @@ export const clusterStatus = cluster.status;
export const clusterCreatedAt = cluster.createdAt;
export const databaseId = database.id;

// Log outputs
sshKeyId.apply(id => console.log(`SSH Key ID: ${id}`));
backupStoreId.apply(id => console.log(`Backup Store ID: ${id}`));
cloudAccountId.apply(id => console.log(`Cloud Account ID: ${id}`));
clusterId.apply(id => console.log(`Cluster ID: ${id}`));
clusterStatus.apply(status => console.log(`Cluster Status: ${status}`));
clusterCreatedAt.apply(createdAt => console.log(`Cluster Created At: ${createdAt}`));
databaseId.apply(id => console.log(`Database id: ${id}`));
// Async log outputs
pulumi.all([
sshKeyId,
backupStoreId,
cloudAccountId,
clusterId,
clusterStatus,
clusterCreatedAt,
databaseId
]).apply(([
sshId,
backupId,
accountId,
cId,
status,
createdAt,
dbId
]) => {
console.log(`SSH Key ID: ${sshId}`);
console.log(`Backup Store ID: ${backupId}`);
console.log(`Cloud Account ID: ${accountId}`);
console.log(`Cluster ID: ${cId}`);
console.log(`Cluster Status: ${status}`);
console.log(`Cluster Created At: ${createdAt}`);
console.log(`Database ID: ${dbId}`);
});

0 comments on commit 2645817

Please sign in to comment.