You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/03/15 10:00:51 UTC

[GitHub] [camel] oscerd commented on a change in pull request #5205: CAMEL-15963 Create a Google Cloud Functions component

oscerd commented on a change in pull request #5205:
URL: https://github.com/apache/camel/pull/5205#discussion_r594196695



##########
File path: components/camel-google/camel-google-functions/src/main/java/org/apache/camel/component/google/functions/GoogleCloudFunctionsProducer.java
##########
@@ -0,0 +1,330 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.google.functions;
+
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
+import com.google.api.client.util.Lists;
+import com.google.api.gax.rpc.ApiException;
+import com.google.cloud.functions.v1.CallFunctionRequest;
+import com.google.cloud.functions.v1.CallFunctionResponse;
+import com.google.cloud.functions.v1.CloudFunction;
+import com.google.cloud.functions.v1.CloudFunctionName;
+import com.google.cloud.functions.v1.CloudFunctionsServiceClient;
+import com.google.cloud.functions.v1.CloudFunctionsServiceClient.ListFunctionsPagedResponse;
+import com.google.cloud.functions.v1.CreateFunctionRequest;
+import com.google.cloud.functions.v1.DeleteFunctionRequest;
+import com.google.cloud.functions.v1.GenerateDownloadUrlRequest;
+import com.google.cloud.functions.v1.GenerateDownloadUrlResponse;
+import com.google.cloud.functions.v1.GenerateUploadUrlRequest;
+import com.google.cloud.functions.v1.GenerateUploadUrlResponse;
+import com.google.cloud.functions.v1.HttpsTrigger;
+import com.google.cloud.functions.v1.ListFunctionsRequest;
+import com.google.cloud.functions.v1.LocationName;
+import com.google.cloud.functions.v1.UpdateFunctionRequest;
+import com.google.protobuf.Empty;
+import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.Message;
+import org.apache.camel.support.DefaultProducer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The GoogleCloudFunctions producer.
+ */
+public class GoogleCloudFunctionsProducer extends DefaultProducer {
+    private static final Logger LOG = LoggerFactory.getLogger(GoogleCloudFunctionsProducer.class);
+
+    private GoogleCloudFunctionsEndpoint endpoint;
+
+    public GoogleCloudFunctionsProducer(GoogleCloudFunctionsEndpoint endpoint) {
+        super(endpoint);
+        this.endpoint = endpoint;
+    }
+
+    @Override
+    public void process(final Exchange exchange) throws Exception {
+        switch (determineOperation(exchange)) {
+            case listFunctions:
+                listFunctions(endpoint.getClient(), exchange);
+                break;
+            case getFunction:
+                getFunction(endpoint.getClient(), exchange);
+                break;
+            case callFunction:
+                callFunction(endpoint.getClient(), exchange);
+                break;
+            case generateDownloadUrl:
+                generateDownloadUrl(endpoint.getClient(), exchange);
+                break;
+            case generateUploadUrl:
+                generateUploadUrl(endpoint.getClient(), exchange);
+                break;
+            case createFunction:
+                createFunction(endpoint.getClient(), exchange);
+                break;
+            case updateFunction:
+                updateFunction(endpoint.getClient(), exchange);
+                break;
+            case deleteFunction:
+                deleteFunction(endpoint.getClient(), exchange);
+                break;
+
+            default:
+                throw new IllegalArgumentException("Unsupported operation");
+        }
+    }
+
+    private void listFunctions(CloudFunctionsServiceClient client, Exchange exchange) throws InvalidPayloadException {
+        if (getConfiguration().isPojoRequest()) {
+            Object payload = exchange.getIn().getMandatoryBody();
+            if (payload instanceof ListFunctionsRequest) {
+                ListFunctionsPagedResponse pagedListResponse;
+                try {
+                    pagedListResponse = client.listFunctions((ListFunctionsRequest) payload);
+                    List<CloudFunction> response = Lists.newArrayList(pagedListResponse.iterateAll());
+                    Message message = getMessageForResponse(exchange);
+                    message.setBody(response);
+                } catch (ApiException ae) {
+                    LOG.trace("listFunctions command returned the error code {}", ae.getStatusCode());
+                    throw ae;
+                }
+            }
+        } else {
+            ListFunctionsRequest request = ListFunctionsRequest.newBuilder()
+                    .setParent(LocationName.of(getConfiguration().getProject(), getConfiguration().getLocation())
+                            .toString())
+                    .setPageSize(883849137) // TODO check it
+                    .setPageToken("pageToken873572522").build();
+            ListFunctionsPagedResponse pagedListResponse = client.listFunctions(request);
+            List<CloudFunction> response = Lists.newArrayList(pagedListResponse.iterateAll());
+            Message message = getMessageForResponse(exchange);
+            message.setBody(response);
+        }
+    }
+
+    private void getFunction(CloudFunctionsServiceClient client, Exchange exchange) throws InvalidPayloadException {
+        if (getConfiguration().isPojoRequest()) {

Review comment:
       This is equal to what we are doing in AWS components, so we need to refactor there too. I'll take care of that.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org