Skip to content

Commit

Permalink
Add an API for StackOptions
Browse files Browse the repository at this point in the history
- add Pulumi.withOptions
  • Loading branch information
pawelprazak committed Jun 9, 2022
1 parent 1e62844 commit 14b4156
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
43 changes: 40 additions & 3 deletions sdk/java/pulumi/src/main/java/com/pulumi/Pulumi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pulumi;

import com.pulumi.internal.PulumiInternal;
import com.pulumi.resources.StackOptions;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
Expand All @@ -25,7 +26,7 @@ public interface Pulumi {
* @see #runAsync(Consumer)
*/
static void run(Consumer<Context> stack) {
System.exit(runAsync(stack).join());
withOptions(StackOptions.Empty).run(stack);
}

/**
Expand All @@ -34,9 +35,45 @@ static void run(Consumer<Context> stack) {
* @param stack the stack to run in Pulumi runtime
* @return a future exit code from Pulumi runtime after running the stack
* @see #run(Consumer)
* @see #withOptions(StackOptions)
*/
static CompletableFuture<Integer> runAsync(Consumer<Context> stack) {
var pulumi = PulumiInternal.fromEnvironment();
return pulumi.runAsync(stack);
return withOptions(StackOptions.Empty).runAsync(stack);
}

/**
* @param options the {@link StackOptions} to use
* @return a Pulumi program entrypoint with given {@link StackOptions}
* @see #run(Consumer)
* @see #runAsync(Consumer)
*/
static Pulumi.API withOptions(StackOptions options) {
return PulumiInternal.fromEnvironment(options);
}

/**
* Pulumi entrypoint operations.
*/
interface API {

/**
* Run a Pulumi stack callback and wait for result.
* <br>
* In case of an error terminates the process with {@link System#exit(int)}
*
* @param stack the stack to run in Pulumi runtime
* @see #runAsync(Consumer)
*/
void run(Consumer<Context> stack);

/**
* Run a Pulumi stack callback asynchronously.
*
* @param stack the stack to run in Pulumi runtime
* @return a future exit code from Pulumi runtime after running the stack
* @see #run(Consumer)
* @see #withOptions(StackOptions)
*/
CompletableFuture<Integer> runAsync(Consumer<Context> stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.pulumi.core.Output;
import com.pulumi.core.internal.Strings;
import com.pulumi.core.internal.annotations.InternalUse;
import com.pulumi.resources.ResourceTransformation;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.Map;

import static com.pulumi.core.internal.Objects.require;
Expand All @@ -24,19 +26,22 @@ public class ContextInternal implements Context {
private final ConfigContextInternal config;
private final OutputContextInternal outputs;
private final ImmutableMap.Builder<String, Output<?>> exports;
private final List<ResourceTransformation> resourceTransformations;

public ContextInternal(
String projectName,
String stackName,
LoggingContextInternal logging,
ConfigContextInternal config,
OutputContextInternal outputs
OutputContextInternal outputs,
List<ResourceTransformation> resourceTransformations
) {
this.projectName = require(Strings::isNonEmptyOrNull, projectName, () -> "expected a project name, got empty string or null");
this.stackName = require(Strings::isNonEmptyOrNull, stackName, () -> "expected a stack name, got empty string or null");
this.logging = requireNonNull(logging);
this.config = requireNonNull(config);
this.outputs = requireNonNull(outputs);
this.resourceTransformations = requireNonNull(resourceTransformations);
this.exports = ImmutableMap.builder();
}

Expand Down Expand Up @@ -78,7 +83,13 @@ public Config config(String name) {
return config.config(name);
}

@InternalUse
public Map<String, Output<?>> exports() {
return this.exports.build();
}

@InternalUse
public List<ResourceTransformation> resourceTransformations() {
return this.resourceTransformations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public CompletableFuture<TestResult> runTestAsync(Consumer<Context> stackCallbac
var context = new ContextInternal(
this.options.getProjectName(),
this.options.getStackName(),
loggingContext, configContext, outputsContext
loggingContext, configContext, outputsContext,
this.options.resourceTransformations()
);
var pulumi = new PulumiTestInternal(this.runner, mockEngine, mockMonitor, context);
return pulumi.runTestAsync(stackCallback);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.pulumi.deployment.internal;

import com.pulumi.resources.ResourceTransformation;

import javax.annotation.Nullable;
import java.util.List;

/**
* Optional settings for tests.
*/
public class TestOptions {

private final String projectName;
private final String stackName;
private final boolean preview;
private final List<ResourceTransformation> resourceTransformations;

public TestOptions() {
this(null, null, true);
Expand All @@ -22,10 +27,16 @@ public TestOptions(@Nullable String projectName, @Nullable String stackName) {
this(projectName, stackName, true);
}

/**
* @param projectName the test project name to use
* @param stackName the test stack name to use
* @param preview is the test a preview or a normal execution
*/
public TestOptions(@Nullable String projectName, @Nullable String stackName, boolean preview) {
this.projectName = projectName != null ? projectName : "project";
this.stackName = stackName != null ? stackName : "stack";
this.preview = preview;
this.resourceTransformations = List.of();
}

/**
Expand All @@ -48,4 +59,14 @@ public String getStackName() {
public boolean isPreview() {
return this.preview;
}

/**
* @return the stack resource transformations
* @see com.pulumi.resources.StackOptions#resourceTransformations()
*/
public List<ResourceTransformation> resourceTransformations() {
return this.resourceTransformations;
}

// TODO: add a builder
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import com.pulumi.deployment.internal.DeploymentImpl;
import com.pulumi.deployment.internal.Runner;
import com.pulumi.deployment.internal.Runner.Result;
import com.pulumi.resources.StackOptions;
import com.pulumi.resources.internal.Stack;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -25,7 +25,7 @@

@InternalUse
@ParametersAreNonnullByDefault
public class PulumiInternal implements Pulumi {
public class PulumiInternal implements Pulumi, Pulumi.API {

protected final Runner runner;
protected final ContextInternal stackContext;
Expand All @@ -37,7 +37,7 @@ public PulumiInternal(Runner runner, ContextInternal stackContext) {
}

@InternalUse
public static PulumiInternal fromEnvironment() {
public static PulumiInternal fromEnvironment(StackOptions options) {
var deployment = DeploymentImpl.fromEnvironment();
var instance = Deployment.getInstance();
var projectName = deployment.getProjectName();
Expand All @@ -51,11 +51,16 @@ public static PulumiInternal fromEnvironment() {
var outputFactory = new OutputFactory(runner);
var outputs = new OutputContextInternal(outputFactory);

var ctx = new ContextInternal(projectName, stackName, logging, config, outputs);
var ctx = new ContextInternal(
projectName, stackName, logging, config, outputs, options.resourceTransformations()
);
return new PulumiInternal(runner, ctx);
}

@InternalUse
public void run(Consumer<Context> stack) {
System.exit(runAsync(stack).join());
}

public CompletableFuture<Integer> runAsync(Consumer<Context> stackCallback) {
return runAsyncResult(stackCallback).thenApply(r -> r.exitCode());
}
Expand All @@ -66,7 +71,7 @@ protected CompletableFuture<Result<Stack>> runAsyncResult(Consumer<Context> stac
() -> Stack.factory(
this.stackContext.projectName(),
this.stackContext.stackName(),
List.of() // TODO: set stack transformations here
this.stackContext.resourceTransformations()
).apply(() -> {
// before user code was executed
stackCallback.accept(this.stackContext); // MUST run before accessing mutable variables
Expand Down

0 comments on commit 14b4156

Please sign in to comment.