-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support RunPlugin for Maven and Gradle plugins (#1608)
This PR implements RunPlugin for the Java provider host. It's implemented for both Maven and Gradle but not other executors. For now, I assume we don't want folks to author plugins with less popular package managers, but we can also add those later. To test RunPlugin, I creates a very basic provider gRPC service that implements only GetSchema. My test provider returns a hard-coded schema, and then tests validate the it goes smoothly through the CLI. Later on we can enhance those tests to be real resource providers. All SDK classes are in the `internal` module. Resolve #1606
- Loading branch information
1 parent
2c7e72d
commit 8f3cc40
Showing
20 changed files
with
539 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
sdk/java/pulumi/src/main/java/com/pulumi/provider/internal/Provider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.pulumi.provider.internal; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import com.pulumi.provider.internal.models.*; | ||
|
||
public interface Provider { | ||
/** | ||
* Returns the schema for this provider's package. | ||
* @param request The schema request | ||
* @return A future containing the schema response | ||
*/ | ||
CompletableFuture<GetSchemaResponse> getSchema(GetSchemaRequest request); | ||
} |
105 changes: 105 additions & 0 deletions
105
sdk/java/pulumi/src/main/java/com/pulumi/provider/internal/ResourceProviderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.pulumi.provider.internal; | ||
|
||
import io.grpc.Server; | ||
import io.grpc.ServerBuilder; | ||
import io.grpc.stub.StreamObserver; | ||
import com.google.protobuf.Empty; | ||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.logging.Logger; | ||
import java.util.function.Function; | ||
|
||
public class ResourceProviderService { | ||
|
||
private static final Logger logger = Logger.getLogger(ResourceProviderService.class.getName()); | ||
|
||
private Server server; | ||
private final Provider implementation; | ||
|
||
public ResourceProviderService(Provider implementation) { | ||
this.implementation = implementation; | ||
} | ||
|
||
public void startAndBlockUntilShutdown() throws IOException, InterruptedException { | ||
start(); | ||
blockUntilShutdown(); | ||
} | ||
|
||
private void start() throws IOException { | ||
server = ServerBuilder.forPort(0) // Use port 0 to let system assign a free port | ||
.addService(new ResourceProviderImpl(this.implementation)) | ||
.build() | ||
.start(); | ||
|
||
// Print the actual bound port for the parent process to read | ||
System.out.println(server.getPort()); | ||
|
||
Runtime.getRuntime().addShutdownHook(new Thread() { | ||
@Override | ||
public void run() { | ||
try { | ||
ResourceProviderService.this.stop(); | ||
} catch (InterruptedException e) { | ||
logger.severe(e.toString()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void stop() throws InterruptedException { | ||
if (server != null) { | ||
server.shutdown().awaitTermination(30, TimeUnit.SECONDS); | ||
} | ||
} | ||
|
||
private void blockUntilShutdown() throws InterruptedException { | ||
if (server != null) { | ||
server.awaitTermination(); | ||
} | ||
} | ||
|
||
static class ResourceProviderImpl extends pulumirpc.ResourceProviderGrpc.ResourceProviderImplBase { | ||
private final Provider implementation; | ||
|
||
public ResourceProviderImpl(Provider implementation) { | ||
this.implementation = implementation; | ||
} | ||
|
||
@Override | ||
public void getPluginInfo(Empty request, StreamObserver<pulumirpc.Plugin.PluginInfo> responseObserver) { | ||
// Return basic plugin information | ||
pulumirpc.Plugin.PluginInfo info = pulumirpc.Plugin.PluginInfo.newBuilder() | ||
.setVersion("1.0.0") | ||
.build(); | ||
|
||
responseObserver.onNext(info); | ||
responseObserver.onCompleted(); | ||
} | ||
|
||
@Override | ||
public void getSchema(pulumirpc.Provider.GetSchemaRequest request, StreamObserver<pulumirpc.Provider.GetSchemaResponse> responseObserver) { | ||
// protobuf sends an empty string for subpackageName/subpackageVersion, but really | ||
// that means null in the domain model. | ||
Function<String, String> nullIfEmpty = s -> { | ||
if (s == null || s.equals("")) { | ||
return null; | ||
} | ||
return s; | ||
}; | ||
|
||
var domRequest = new com.pulumi.provider.internal.models.GetSchemaRequest( | ||
request.getVersion(), | ||
nullIfEmpty.apply(request.getSubpackageName()), | ||
nullIfEmpty.apply(request.getSubpackageVersion()) | ||
); | ||
|
||
this.implementation.getSchema(domRequest).thenAccept(domResponse -> { | ||
var grpcResponse = pulumirpc.Provider.GetSchemaResponse.newBuilder() | ||
.setSchema(domResponse.getSchema()) | ||
.build(); | ||
responseObserver.onNext(grpcResponse); | ||
responseObserver.onCompleted(); | ||
}); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
sdk/java/pulumi/src/main/java/com/pulumi/provider/internal/models/GetSchemaRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.pulumi.provider.internal.models; | ||
|
||
public class GetSchemaRequest { | ||
private final int version; | ||
private final String subpackageName; | ||
private final String subpackageVersion; | ||
|
||
public GetSchemaRequest(int version, String subpackageName, String subpackageVersion) { | ||
this.version = version; | ||
this.subpackageName = subpackageName; | ||
this.subpackageVersion = subpackageVersion; | ||
} | ||
|
||
public int getVersion() { | ||
return version; | ||
} | ||
|
||
public String getSubpackageName() { | ||
return subpackageName; | ||
} | ||
|
||
public String getSubpackageVersion() { | ||
return subpackageVersion; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
sdk/java/pulumi/src/main/java/com/pulumi/provider/internal/models/GetSchemaResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.pulumi.provider.internal.models; | ||
|
||
public class GetSchemaResponse { | ||
private final String schema; | ||
|
||
public GetSchemaResponse(String schema) { | ||
this.schema = schema; | ||
} | ||
|
||
public String getSchema() { | ||
return schema; | ||
} | ||
} |
Oops, something went wrong.