https://pulumi.com logo
Title
p

proud-spoon-58287

08/07/2020, 9:04 AM
Hi all, dumb question:
export class KsqlDbCluster {

    private readonly _clusterName: string

    constructor(clusterName: string) {
        this._clusterName = clusterName
    }
    private readonly _k8nCluster: gcp.container.Cluster = this.buildCluster(this._clusterName)
I am invoking this class this way
const ksqlDbClusterName = 'ksqldb-cluster'
const ksqlDbCluster = new KsqlDbCluster(ksqlDbClusterName)
Why in the world
this._clusterName
is undefined?
apparently
new
does not invoke the constructor?
b

broad-dog-22463

08/07/2020, 9:09 AM
Morning, the way I usually write classes like this are as follows:
import * as gcp from "@pulumi/gcp";
import * as pulumi from "@pulumi/pulumi";

export class NginxWebserver {
    public readonly instance: gcp.compute.Instance;
    public readonly instanceAddress: pulumi.Output<string>;

    constructor(name: string, machineType: string, imageName: string, network: gcp.compute.Network) {

        //virtual machine running nginx via a [startup script](<https://cloud.google.com/compute/docs/startupscript>)
        const script =
            `#!/bin/bash
apt -y update
apt -y install nginx`;

        const address = new gcp.compute.Address(`${name}`);
        
        this.instance = new gcp.compute.Instance(`${name}`, {
            machineType: `${machineType}`,
            bootDisk: {
                initializeParams: {
                    image: `${imageName}`,
                },
            },
            networkInterfaces: [{
                network: network.id,
                accessConfigs: [{
                    natIp: address.address
                }]
            }],
            metadataStartupScript: script,
        });
        
        this.instanceAddress = address.address;
    }
}
Notice that the resources are built inside the constructor
Then I can use the calling code exactly like you would have
p

proud-spoon-58287

08/07/2020, 9:14 AM
private readonly _clusterName: string

    constructor(clusterName: string) {
        console.log("CONSTRUCTOR:" + clusterName)
        this._clusterName = clusterName
    }
log is empty....
strange ๐Ÿ˜•
so you put everything in constructor?
@broad-dog-22463
b

broad-dog-22463

08/07/2020, 9:16 AM
anything that isn't a method that I need to call directly on the class
so if there was a Build func, then I would have that separate to the constructor and then call that diretly
p

proud-spoon-58287

08/07/2020, 9:19 AM
I see
but why the bloody constructor is not invoked?
bu the way
is there a way to put breakpoints (maybe running pulumi in debug mode? )
by the way, thanks @broad-dog-22463, works now
b

broad-dog-22463

08/07/2020, 9:33 AM
ah good news it worked now
p

proud-spoon-58287

08/07/2020, 10:07 AM
Once you have confirmed the status of the interrupted operations, you can repair your stack
using 'pulumi stack export' to export your stack to a file. For each operation that succeeded,
remove that operation from the "pending_operations" section of the file. Once this is complete,
use 'pulumi stack import' to import the repaired stack.

refusing to proceed
is it possible to force delete everything?
without removing the stack?
b

broad-dog-22463

08/07/2020, 10:10 AM
Iโ€™m not quite sure of the question - you mean for delete of the resources and keep the stack?
p

proud-spoon-58287

08/07/2020, 10:12 AM
yes, how can I re-init the stack?
machine went to sleep while destroying
got that error
I can export the stack and import again
but I'd like to know if there is a faster way
l

limited-rainbow-51650

08/07/2020, 12:02 PM
@proud-spoon-58287 itโ€™s not because you write your read-only property after your constructor, that that is also the evaluation/initialization order. Your property
_k8nCluster
is initialized before the constructor is executed. This StackOverflow answer defines the complete order: https://stackoverflow.com/a/53706727
๐Ÿ‘ 2
p

proud-spoon-58287

08/07/2020, 1:06 PM
Thanks @limited-rainbow-51650
c

clever-sunset-76585

08/07/2020, 1:09 PM
@proud-spoon-58287 As @limited-rainbow-51650 said you cannot use class members before they are initialized in the constructor. To draw a parallel with Java, if you have:
public Class KsqlDbCluster {
  private final String clusterName;
  // This is not legal in Java either regardless of where you position this.
  private final String k8sCluster = this.buildCluster(this.clusterName);

  public KSqlDbCluster(String clusterName) {
    this.clusterName = clusterName;
    // This is fine. But at this point, do you want the class member clusterName at all? :)
    this.k8sCluster = this.buildCluster(this.clusterName);
  }
}
๐Ÿ‘ 1