Skip to content

Commit

Permalink
Feature flag parameterized provider support (#1438)
Browse files Browse the repository at this point in the history
Support for parameterized providers (introduced originally in #1398) is
now exposed by the engine as a new `parameterized` feature flag. This
commit updates the Java SDK to check for support before attempting to
register a parameterized package, allowing us to throw a better error
message in the event that parameterization is not currently supported.

Part of pulumi/pulumi#16906
  • Loading branch information
lunaris authored Oct 14, 2024
1 parent 10decea commit a34adb2
Show file tree
Hide file tree
Showing 14 changed files with 456 additions and 142 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

- Update pulumi/pulumi to v3.132.0

### Bug Fixes
- Feature flag parameterized provider support

### Bug Fixes
2 changes: 1 addition & 1 deletion pulumi
Submodule pulumi updated 2491 files
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ private CompletableFuture<Boolean> monitorSupportsFeature(String feature) {
CompletableFuture<Boolean> monitorSupportsResourceReferences() {
return monitorSupportsFeature("resourceReferences");
}

@InternalUse
CompletableFuture<Boolean> monitorSupportsParameterization() {
return monitorSupportsFeature("parameterization");
}
}

@ParametersAreNonnullByDefault
Expand Down Expand Up @@ -846,21 +851,27 @@ public CompletableFuture<String> registerPackage(
String packageVersion,
String base64Parameter
) {
var request = RegisterPackageRequest.newBuilder()
.setName(baseProviderName)
.setVersion(baseProviderVersion)
.setDownloadUrl(baseProviderDownloadUrl)
.setParameterization(
Parameterization.newBuilder()
.setName(packageName)
.setVersion(packageVersion)
.setValue(ByteString.copyFrom(Base64.getDecoder().decode(base64Parameter)))
.build()
)
.build();

return this.state.monitor.registerPackageAsync(request)
.thenApply(response -> response.getRef());
return this.featureSupport.monitorSupportsParameterization().thenCompose(supportsParameterization -> {
if (!supportsParameterization) {
throw new UnsupportedOperationException("The Pulumi CLI does not support parameterization. Please update the Pulumi CLI.");
}

var request = RegisterPackageRequest.newBuilder()
.setName(baseProviderName)
.setVersion(baseProviderVersion)
.setDownloadUrl(baseProviderDownloadUrl)
.setParameterization(
Parameterization.newBuilder()
.setName(packageName)
.setVersion(packageVersion)
.setValue(ByteString.copyFrom(Base64.getDecoder().decode(base64Parameter)))
.build()
)
.build();

return this.state.monitor.registerPackageAsync(request)
.thenApply(response -> response.getRef());
});
}

private static final class Prepare {
Expand Down
5 changes: 5 additions & 0 deletions tests/examples/aws-native-java-s3-folder/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
3 changes: 3 additions & 0 deletions tests/examples/aws-native-java-s3-folder/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: aws-java-s3-folder
runtime: java
description: A static website hosted on AWS S3
116 changes: 116 additions & 0 deletions tests/examples/aws-native-java-s3-folder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
[![Deploy this example with Pulumi](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new?template=https://github.com/pulumi/examples/blob/master/aws-native-java-s3-folder/README.md#gh-light-mode-only)
[![Deploy this example with Pulumi](https://get.pulumi.com/new/button-light.svg)](https://app.pulumi.com/new?template=https://github.com/pulumi/examples/blob/master/aws-native-java-s3-folder/README.md#gh-dark-mode-only)

# Host a Static Website on Amazon S3 with the AWS Native Provider

A static website that uses [S3's website support](https://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html).
For a detailed walkthrough of this example, see the tutorial [Static Website on AWS S3](https://www.pulumi.com/docs/tutorials/aws/s3-website/).

Note: Some resources are not yet supported by the Native AWS provider, so we are using both the Native
and Classic provider in this example. The resources will be updated to use native resources as they are
available in AWS's Cloud Control API.

## Deploying and running the program

Note: some values in this example will be different from run to run. These values are indicated
with `***`.

1. Set the AWS region:

Either using an environment variable
```bash
$ export AWS_REGION=us-west-2
```

Or with the stack config
```bash
$ pulumi config set aws:region us-west-2
$ pulumi config set aws-native:region us-west-2
```

1. Run `pulumi up` to preview and deploy changes. After the preview is shown you will be
prompted if you want to continue or not.

```bash
$ pulumi up
Previewing update (dev)
View Live: https://app.pulumi.com/***/aws-java-s3-folder/dev/previews/e251093a-d128-4ed3-a230-4e98888aed84
Type Name Plan Info
+ pulumi:pulumi:Stack aws-java-s3-folder-dev create 6 messages
+ ├─ aws-native:s3:Bucket s3-website-bucket create
+ ├─ aws:s3:BucketPolicy bucketPolicy create
+ ├─ aws:s3:BucketObject index.html create
+ └─ aws:s3:BucketObject favicon.ico create
Diagnostics:
pulumi:pulumi:Stack (aws-java-s3-folder-dev):
> Task :app:compileJava UP-TO-DATE
> Task :app:processResources NO-SOURCE
> Task :app:classes UP-TO-DATE
> Task :app:run
BUILD SUCCESSFUL in 3s
2 actionable tasks: 1 executed, 1 up-to-date
Updating (dev)
View Live: https://app.pulumi.com/***/aws-java-s3-folder/dev/updates/1
Type Name Status Info
+ pulumi:pulumi:Stack aws-java-s3-folder-dev created 6 messages
+ ├─ aws-native:s3:Bucket s3-website-bucket created
+ ├─ aws:s3:BucketPolicy bucketPolicy created
+ ├─ aws:s3:BucketObject index.html created
+ └─ aws:s3:BucketObject favicon.ico created
Diagnostics:
pulumi:pulumi:Stack (aws-java-s3-folder-dev):
> Task :app:compileJava UP-TO-DATE
> Task :app:processResources NO-SOURCE
> Task :app:classes UP-TO-DATE
> Task :app:run
BUILD SUCCESSFUL in 1m 5s
2 actionable tasks: 1 executed, 1 up-to-date
Outputs:
bucketName: "s3-website-bucket-***"
urn : "***"
websiteUrl: "http://s3-website-bucket-***.s3-website-us-west-2.amazonaws.com"
Resources:
+ 5 created
Duration: 1m6s
```

1. To see the resources that were created, run `pulumi stack output`:

```bash
$ pulumi stack output
Current stack outputs (3):
OUTPUT VALUE
bucketName s3-website-bucket-***
urn urn:pulumi:dev::aws-java-s3-folder::pulumi:pulumi:Stack::aws-java-s3-folder-dev
websiteUrl http://s3-website-bucket-***.s3-website-us-west-2.amazonaws.com
```

1. To see that the S3 objects exist, you can either use the AWS Console or the AWS CLI:

```bash
$ aws s3 ls $(pulumi stack output bucketName)
2022-02-17 14:11:54 13731 favicon.ico
2022-02-17 14:11:54 198 index.html
```

1. Open the site URL in a browser to see both the rendered HTML and the favicon:

```bash
$ pulumi stack output websiteUrl
http://s3-website-bucket-***.s3-website-us-west-2.amazonaws.com
```

![Hello S3 example](https://user-images.githubusercontent.com/274700/116912066-9384e300-abfc-11eb-8130-dbcff512a9de.png)

1. To clean up resources, run `pulumi destroy` and answer the confirmation question at the prompt.
51 changes: 51 additions & 0 deletions tests/examples/aws-native-java-s3-folder/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.pulumi</groupId>
<artifactId>aws-native-java-s3-folder</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<encoding>UTF-8</encoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<mainClass>s3site.App</mainClass>
<mainArgs/>
</properties>

<dependencies>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>pulumi</artifactId>
<version>(,1.0]</version>
</dependency>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>aws</artifactId>
<version>(6.0.2,6.99]</version>
</dependency>
<dependency>
<groupId>com.pulumi</groupId>
<artifactId>aws-native</artifactId>
<version>(,1.0]</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>${mainClass}</mainClass>
<commandlineArgs>${mainArgs}</commandlineArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// from https://github.com/pulumi/examples/blob/8cc8b1a4384c8b33f70ba65c701e19daecfa6399/aws-ts-s3-folder/index.ts
package s3site;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.asset.FileAsset;
import com.pulumi.aws.s3.BucketObject;
import com.pulumi.aws.s3.BucketObjectArgs;
import com.pulumi.aws.s3.BucketPolicy;
import com.pulumi.aws.s3.BucketPolicyArgs;
import com.pulumi.awsnative.s3.Bucket;
import com.pulumi.awsnative.s3.BucketArgs;
import com.pulumi.awsnative.s3.inputs.BucketWebsiteConfigurationArgs;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.BiConsumer;

public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}

private static void stack(Context ctx) {
final var siteBucket = new Bucket("s3-website-bucket",
BucketArgs.builder().websiteConfiguration(BucketWebsiteConfigurationArgs.builder()
.indexDocument("index.html")
.build()).build());

final String siteDir = "www/";
forEachFileInTree(siteDir, (path, contentType) -> {
new BucketObject(path.toString().replace(siteDir, ""),
BucketObjectArgs.builder().bucket(siteBucket.getId())
.source(new FileAsset(path.toAbsolutePath().toString()))
.contentType(contentType).build()
);
});

final var bucketPolicy = new BucketPolicy("bucketPolicy",
BucketPolicyArgs.builder().bucket(siteBucket.getId())
.policy(siteBucket.arn()
.applyValue(bucketArn -> """
{
"Version":"2012-10-17",
"Statement":[{
"Effect":"Allow",
"Principal":"*",
"Action":["s3:GetObject"],
"Resource":["%s/*"]
}]
}
""".formatted(bucketArn))
).build());

ctx.export("bucketName", siteBucket.bucketName());
ctx.export("websiteUrl", siteBucket.websiteUrl());
}

private static void forEachFileInTree(String siteDir, BiConsumer<Path, String> consumer) {
try (var paths = Files.walk(Paths.get(siteDir)).filter(Files::isRegularFile)) {
paths.forEach(path -> {
final String contentType;
try {
contentType = Files.probeContentType(path);
} catch (IOException e) {
throw new RuntimeException(String.format("Failed to probeContentType for path: '%s'", path), e);
}
consumer.accept(path, contentType);
});
} catch (IOException e) {
throw new RuntimeException(String.format("Failed to walk a path: '%s'", siteDir), e);
}
}
}
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/examples/aws-native-java-s3-folder/www/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head><meta charset="UTF-8">
<title>Hello, Pulumi!</title></head>
<body>
<p>Hello, S3!</p>
<p>Made with ❤️ with <a href="https://pulumi.com">Pulumi</a></p>
</body></html>
Loading

0 comments on commit a34adb2

Please sign in to comment.