quarkus-google-cloud-services icon indicating copy to clipboard operation
quarkus-google-cloud-services copied to clipboard

Add cloud tasks support

Open zecit opened this issue 2 years ago • 8 comments

Add a module to support cloud tasks creation.

zecit avatar Jul 04 '22 13:07 zecit

+1

bernardocoferre avatar Nov 03 '22 02:11 bernardocoferre

Any update on this?

kamal-j avatar Feb 10 '23 12:02 kamal-j

No update, we didn't plan to support any Google Cloud services as it may be a huge task. Anyone how is interested should +1 on the issue description so I know that is a wanted feature.

Most Google Cloud Services works in the same way so I plan to create a section on the documentation explaining how someone can integrate them without needing and extension (it's mainly authentication and gRCP integration with Quarkus gRCP).

loicmathieu avatar Feb 10 '23 15:02 loicmathieu

Most Google Cloud Services works in the same way so I plan to create a section on the documentation explaining how someone can integrate them without needing and extension (it's mainly authentication and gRCP integration with Quarkus gRCP).

That would be very helpful. Thanks!

kamal-j avatar Feb 13 '23 06:02 kamal-j

+1

shaonianA avatar May 25 '23 16:05 shaonianA

I've been using Cloud Tasks in my quarkus application for a while. I hope I didn't miss anything because it's been a while and hard to extract needed parts.

To make it work as native image I added following to pom.xml

        <dependency>
            <groupId>io.quarkiverse.googlecloudservices</groupId>
            <artifactId>quarkus-google-cloud-common</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkiverse.googlecloudservices</groupId>
            <artifactId>quarkus-google-cloud-common-grpc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-tasks</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.annotation</groupId>
                    <artifactId>javax.annotation-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.checkerframework</groupId>
                    <artifactId>checker-qual</artifactId>
                </exclusion>
                <!-- We exclude the grpc-netty-shaded library as grpc-netty is included via
                quarkus-google-cloud-common-grpc and Quarkus includes the netty library -->
                <exclusion>
                    <groupId>io.grpc</groupId>
                    <artifactId>grpc-netty-shaded</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.google.api</groupId>
                    <artifactId>gax-grpc</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>animal-sniffer-annotations</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-grpc-common</artifactId>
        </dependency>

And to initialize and authenticate I do following.


@Inject
GoogleCredentials googleCredentials;
    
    
CloudTasksSettings settings = CloudTasksSettings.newBuilder()                            
 .setCredentialsProvider(FixedCredentialsProvider.create(googleCredentials))
                                .build();
try (CloudTasksClient client = CloudTasksClient.create(settings)) {
   
  // Construct the fully qualified queue name.
  String queuePath = QueueName.of(gCloudProjectId, gCloudLocationId, 
  ct.getQueueName()).toString();
  OidcToken.Builder oidcTokenBuilder =
  OidcToken.newBuilder()
     .setServiceAccountEmail(gCloudServiceAccount)
     .setAudience(ct.getAudiance());
                                        
  Task.newBuilder()
     .setHttpRequest(HttpRequest.newBuilder()
                     .setBody(ByteString.copyFrom(ct.getBody(), Charset.defaultCharset()))
                      .putHeaders("Content-Type", "application/json")
                      .setUrl(ct.getUrl())
                       .setHttpMethod(ct.getMethod())
                       .setOidcToken(oidcTokenBuilder)
                       .build())
                       
  Task taskToCreate = taskBuilder.build();

  // Send create task request.
  Task task = client.createTask(queuePath, taskToCreate);
  System.out.println("Task created: " + task.getName());

}
                                

ayhanap avatar Nov 03 '23 01:11 ayhanap

Thanks @ayhanap these steps are the basic steps for supporting any Google Cloud services: declaring the dependency with exclusion, if gRCP is used: excluding the dependency and importing the Quarkuse one, injecting authentication and initiating a client. To make it looks more like an extension you can create a CDI producer for the client.

I still have plan to add a documentation page for it, I may borrow the code for your example ;)

I think you don't need to exclude

                <exclusion>
                    <groupId>com.google.api</groupId>
                    <artifactId>gax-grpc</artifactId>
                </exclusion>

And for checker-qual and animal-sniffer, excluding them should be only needed when the lib is integrated into a Quarkus extension.

loicmathieu avatar Nov 06 '23 11:11 loicmathieu

I got inspired by existing code from other gcloud services, I just copied the storage one I guess :) Might be right about unnecessary exclusions, been a while don't remember if they caused any issues.

Feel free to use it as you like, it's just the sample code from https://cloud.google.com/tasks/docs/creating-http-target-tasks#java and some modifications for authentication.

ayhanap avatar Nov 06 '23 11:11 ayhanap