Skip to content

Instantly share code, notes, and snippets.

@pawelprazak
Last active November 4, 2021 14:05
Show Gist options
  • Save pawelprazak/61dc14021206aeaee2eb323b1996fba0 to your computer and use it in GitHub Desktop.
Save pawelprazak/61dc14021206aeaee2eb323b1996fba0 to your computer and use it in GitHub Desktop.
Pulumi Java prototype API
package org.example;
import io.pulumi.Config;
import io.pulumi.Stack;
import io.pulumi.core.Output;
import io.pulumi.core.internal.annotations.OutputExport;
import io.pulumi.deployment.Deployment;
import org.example.k8s.core.v1.Namespace;
import org.example.k8s.core.v1.Secret;
import org.example.k8s.meta.v1.ObjectMeta;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static class MyStack extends Stack {
@OutputExport(type = String.class)
public final Output<String> theNamespace;
@OutputExport(type = String.class)
public final Output<String> theSecret;
@OutputExport(type = Map.class, parameters = {String.class, String.class})
public final Output<Map<String, String>> theSecretData;
public MyStack() {
var config = Config.of("example-jvm");
var name = config.require("name");
var ns = new Namespace(name, Namespace.args()
.setMetadata(
ObjectMeta.args()
.setName(name)
.setLabels(Map.of("app", name))
.build()
).build()
);
this.theNamespace = Output.allOutputs(
ns.getApiVersion(),
ns.getKind(),
ns.getMetadata().applyOptional(ObjectMeta.Result::getName)
).applyValue(ss -> String.join("/", ss));
var secret = new Secret("my-secret", Secret.args()
.setMetadata(
ObjectMeta.args()
.setNamespace(
ns.getMetadata()
.applyOptional(ObjectMeta.Result::getName)
.toInput())
.build())
.setData(Map.of("key", Base64.getEncoder().encodeToString("value".getBytes(StandardCharsets.UTF_8))))
.build()
);
this.theSecret = secret.getMetadata().applyOptional(ObjectMeta.Result::getName);
this.theSecretData = secret.getData().applyValue(
d -> d.entrySet().stream().peek(System.out::println).collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue
)));
}
}
public static void main(String[] args) {
var code = Deployment.runAsyncStack(MyStack.class).join();
System.out.println("Example ended with: " + code);
System.exit(code);
}
}
config:
example-jvm:name: hello
@pawelprazak
Copy link
Author

$ export PULUMI_CONFIG_PASSPHRASE="example-jvm"; pulumi up -d
Previewing update (example-jvm):
     Type                 Name                 Plan     Info
     pulumi:pulumi:Stack  example-example-jvm           16 messages; 13 debugs

Diagnostics:
  pulumi:pulumi:Stack (example-example-jvm):
    > Task :compileJava
    > Task :processResources UP-TO-DATE
    > Task :classes
    > Task :run
    key=dmFsdWU=
    Example ended with: 0
    BUILD SUCCESSFUL in 2s
    3 actionable tasks: 2 executed, 1 up-to-date

    debug: Preparing resource: t=pulumi:pulumi:Stack, name=example-example-jvm, custom=false, remote=false
    debug: Preparing resource: t=kubernetes:core/v1:Namespace, name=hello, custom=true, remote=false
    debug: Preparing resource: t=kubernetes:core/v1:Secret, name=my-secret, custom=true, remote=false
    debug: Prepared resource: t=pulumi:pulumi:Stack, name=example-example-jvm, custom=false, remote=false
    debug: Registering resource monitor start: t=pulumi:pulumi:Stack, name=example-example-jvm, custom=false, remote=false
    debug: Registering resource monitor end: t=pulumi:pulumi:Stack, name=example-example-jvm, custom=false, remote=false
    debug: Prepared resource: t=kubernetes:core/v1:Namespace, name=hello, custom=true, remote=false
    debug: Registering resource monitor start: t=kubernetes:core/v1:Namespace, name=hello, custom=true, remote=false
    debug: Registering resource monitor end: t=kubernetes:core/v1:Namespace, name=hello, custom=true, remote=false
    debug: Prepared resource: t=kubernetes:core/v1:Secret, name=my-secret, custom=true, remote=false
    debug: Registering resource monitor start: t=kubernetes:core/v1:Secret, name=my-secret, custom=true, remote=false
    debug: Registering resource monitor end: t=kubernetes:core/v1:Secret, name=my-secret, custom=true, remote=false
    debug: RegisterResourceOutputs RPC prepared: urn='urn:pulumi:example-jvm::example::pulumi:pulumi:Stack::example-example-jvm'


Do you want to perform this update? yes
Updating (example-jvm):
     Type                 Name                 Status     Info
     pulumi:pulumi:Stack  example-example-jvm             16 messages; 13 debugs

Diagnostics:
  pulumi:pulumi:Stack (example-example-jvm):
    debug: Preparing resource: t=pulumi:pulumi:Stack, name=example-example-jvm, custom=false, remote=false
    debug: Preparing resource: t=kubernetes:core/v1:Namespace, name=hello, custom=true, remote=false
    debug: Preparing resource: t=kubernetes:core/v1:Secret, name=my-secret, custom=true, remote=false
    debug: Prepared resource: t=pulumi:pulumi:Stack, name=example-example-jvm, custom=false, remote=false
    debug: Registering resource monitor start: t=pulumi:pulumi:Stack, name=example-example-jvm, custom=false, remote=false
    debug: Registering resource monitor end: t=pulumi:pulumi:Stack, name=example-example-jvm, custom=false, remote=false
    debug: Prepared resource: t=kubernetes:core/v1:Namespace, name=hello, custom=true, remote=false
    debug: Registering resource monitor start: t=kubernetes:core/v1:Namespace, name=hello, custom=true, remote=false
    debug: Registering resource monitor end: t=kubernetes:core/v1:Namespace, name=hello, custom=true, remote=false
    debug: Prepared resource: t=kubernetes:core/v1:Secret, name=my-secret, custom=true, remote=false
    debug: Registering resource monitor start: t=kubernetes:core/v1:Secret, name=my-secret, custom=true, remote=false
    debug: Registering resource monitor end: t=kubernetes:core/v1:Secret, name=my-secret, custom=true, remote=false
    debug: RegisterResourceOutputs RPC prepared: urn='urn:pulumi:example-jvm::example::pulumi:pulumi:Stack::example-example-jvm'

    > Task :compileJava UP-TO-DATE
    > Task :processResources UP-TO-DATE
    > Task :classes UP-TO-DATE
    > Task :run
    can't deserialize: 'uid'
    can't deserialize: 'managedFields'
    can't deserialize: 'resourceVersion'
    can't deserialize: 'creationTimestamp'
    can't deserialize: 'uid'
    can't deserialize: 'managedFields'
    can't deserialize: 'resourceVersion'
    can't deserialize: 'creationTimestamp'
    key=dmFsdWU=
    Example ended with: 0
    BUILD SUCCESSFUL in 1s
    3 actionable tasks: 1 executed, 2 up-to-date

Outputs:
    theNamespace : "v1/Namespace/hello"
    theSecret    : "[secret]"
    theSecretData: "[secret]"
    urn          : "urn:pulumi:example-jvm::example::pulumi:pulumi:Stack::example-example-jvm"

Resources:
    3 unchanged

Duration: 2s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment