From 2645817cf5b92747b6ed24f881c1fa7a76288d59 Mon Sep 17 00:00:00 2001 From: KnockOutEZ Date: Tue, 22 Oct 2024 16:38:16 +0600 Subject: [PATCH] Add explicit dependencies between resources using dependsOn --- README.md | 57 +++++++++++++++++++++--- examples/go/main.go | 60 ++++++++++++------------- examples/typescript/index.ts | 86 +++++++++++++++++++++++------------- 3 files changed, 133 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 69fd0ba..4fd37ee 100644 --- a/README.md +++ b/README.md @@ -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", { @@ -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", @@ -173,7 +178,6 @@ const cluster = new pgedge.Cluster("exampleCluster", { // privateSubnets: ["10.3.0.0/24"], }, ], - backupStoreIds: [backupStore.id], firewallRules: [ { name: "postgres", @@ -181,7 +185,9 @@ const cluster = new pgedge.Cluster("exampleCluster", { sources: ["123.456.789.0/32"], }, ], -}, { dependsOn: backupStore }); +}, { + dependsOn: [sshKey, cloudAccount, backupStore] +}); // Create a Database const database = new pgedge.Database("exampleDatabase", { @@ -198,7 +204,7 @@ const database = new pgedge.Database("exampleDatabase", { "postgis", ], }, - nodes:{ + nodes: { n1: { name: "n1", }, @@ -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 diff --git a/examples/go/main.go b/examples/go/main.go index eaa1f93..9746c6a 100644 --- a/examples/go/main.go +++ b/examples/go/main.go @@ -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 } @@ -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"), @@ -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() }) -} +} \ No newline at end of file diff --git a/examples/typescript/index.ts b/examples/typescript/index.ts index 5a351cd..819466c 100644 --- a/examples/typescript/index.ts +++ b/examples/typescript/index.ts @@ -16,13 +16,17 @@ 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", @@ -30,6 +34,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", @@ -73,7 +78,6 @@ const cluster = new pgedge.Cluster("exampleCluster", { // privateSubnets: ["10.3.0.0/24"], }, ], - // backupStoreIds: [backupStore.id], firewallRules: [ { name: "postgres", @@ -81,7 +85,9 @@ const cluster = new pgedge.Cluster("exampleCluster", { sources: ["192.0.2.44/32"], }, ], -}, { dependsOn: backupStore }); +}, { + dependsOn: [sshKey, cloudAccount, backupStore] +}); const database = new pgedge.Database("exampleDatabase", { name: "example", @@ -100,7 +106,7 @@ const database = new pgedge.Database("exampleDatabase", { "vector" ], }, - nodes:{ + nodes: { n1: { name: "n1", }, @@ -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; @@ -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}`)); \ No newline at end of file +// 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}`); +}); \ No newline at end of file