coufon commented on code in PR #7412:
URL: https://github.com/apache/iceberg/pull/7412#discussion_r1344513347


##########
gcp/src/main/java/org/apache/iceberg/gcp/biglake/BigLakeClient.java:
##########
@@ -0,0 +1,270 @@
+/*
+ * 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.iceberg.gcp.biglake;
+
+import com.google.api.gax.rpc.PermissionDeniedException;
+import com.google.cloud.bigquery.biglake.v1.Catalog;
+import com.google.cloud.bigquery.biglake.v1.CatalogName;
+import com.google.cloud.bigquery.biglake.v1.CreateCatalogRequest;
+import com.google.cloud.bigquery.biglake.v1.CreateDatabaseRequest;
+import com.google.cloud.bigquery.biglake.v1.CreateTableRequest;
+import com.google.cloud.bigquery.biglake.v1.Database;
+import com.google.cloud.bigquery.biglake.v1.DatabaseName;
+import com.google.cloud.bigquery.biglake.v1.DeleteCatalogRequest;
+import com.google.cloud.bigquery.biglake.v1.DeleteDatabaseRequest;
+import com.google.cloud.bigquery.biglake.v1.DeleteTableRequest;
+import com.google.cloud.bigquery.biglake.v1.GetCatalogRequest;
+import com.google.cloud.bigquery.biglake.v1.GetDatabaseRequest;
+import com.google.cloud.bigquery.biglake.v1.GetTableRequest;
+import com.google.cloud.bigquery.biglake.v1.ListDatabasesRequest;
+import com.google.cloud.bigquery.biglake.v1.ListTablesRequest;
+import com.google.cloud.bigquery.biglake.v1.LocationName;
+import com.google.cloud.bigquery.biglake.v1.MetastoreServiceClient;
+import com.google.cloud.bigquery.biglake.v1.MetastoreServiceSettings;
+import com.google.cloud.bigquery.biglake.v1.RenameTableRequest;
+import com.google.cloud.bigquery.biglake.v1.Table;
+import com.google.cloud.bigquery.biglake.v1.TableName;
+import com.google.cloud.bigquery.biglake.v1.UpdateDatabaseRequest;
+import com.google.cloud.bigquery.biglake.v1.UpdateTableRequest;
+import com.google.protobuf.FieldMask;
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Map;
+import java.util.function.Supplier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+
+/** A client of Google BigLake service. */
+final class BigLakeClient implements Closeable {
+
+  private final MetastoreServiceClient stub;
+
+  /**
+   * Constructs a client of Google BigLake Service.
+   *
+   * @param settings BigLake service settings
+   */
+  BigLakeClient(MetastoreServiceSettings settings) throws IOException {
+    this.stub = MetastoreServiceClient.create(settings);
+  }
+
+  /**
+   * Constructs a client of Google BigLake Service.
+   *
+   * @param biglakeEndpoint BigLake service gRPC endpoint, e.g., 
"biglake.googleapis.com:443"
+   */
+  BigLakeClient(String biglakeEndpoint) throws IOException {
+    
this(MetastoreServiceSettings.newBuilder().setEndpoint(biglakeEndpoint).build());
+  }
+
+  public Catalog createCatalog(CatalogName name, Catalog catalog) {
+    return convertException(
+        () -> {
+          try {
+            return stub.createCatalog(
+                CreateCatalogRequest.newBuilder()
+                    .setParent(LocationName.of(name.getProject(), 
name.getLocation()).toString())
+                    .setCatalogId(name.getCatalog())
+                    .setCatalog(catalog)
+                    .build());
+          } catch (com.google.api.gax.rpc.AlreadyExistsException e) {
+            throw new AlreadyExistsException(e, "Catalog already exists: %s", 
name.getCatalog());
+          }
+        },
+        name.getCatalog());
+  }
+
+  public Catalog catalog(CatalogName name) {
+    try {
+      return 
stub.getCatalog(GetCatalogRequest.newBuilder().setName(name.toString()).build());
+    } catch (PermissionDeniedException e) {
+      throw new NoSuchNamespaceException(
+          e, "Catalog does not exist: %s (or permission denied)", 
name.getCatalog());
+    }
+  }
+
+  public void deleteCatalog(CatalogName name) {
+    try {
+      
stub.deleteCatalog(DeleteCatalogRequest.newBuilder().setName(name.toString()).build());
+    } catch (PermissionDeniedException e) {
+      throw new NoSuchNamespaceException(
+          e, "Catalog does not exist: %s (or permission denied)", 
name.getCatalog());
+    }
+  }
+
+  public Database createDatabase(DatabaseName name, Database db) {
+    return convertException(
+        () -> {
+          try {
+            return stub.createDatabase(
+                CreateDatabaseRequest.newBuilder()
+                    .setParent(
+                        CatalogName.of(name.getProject(), name.getLocation(), 
name.getCatalog())
+                            .toString())
+                    .setDatabaseId(name.getDatabase())
+                    .setDatabase(db)
+                    .build());
+          } catch (com.google.api.gax.rpc.AlreadyExistsException e) {

Review Comment:
   Yes, it is a good point. We should check whether it failed due to parent not 
found or permission denied. It needs parsing the error message. I added a TODO 
to do it in a follow-up PR, to avoid adding more new code to this PR which has 
been around for long.
   
   The error message is like this: "Permission 'biglake.databases.create' 
denied on resource 
'//biglake.googleapis.com/projects/myproj/locations/us/catalogs/mycat' (or it 
may not exist).". We need to determine whether the error is on this resource 
(table) or its parent (database or catalog).



-- 
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.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to