mchades commented on code in PR #7796:
URL: https://github.com/apache/gravitino/pull/7796#discussion_r2255974102


##########
core/src/main/java/org/apache/gravitino/policy/PolicyManager.java:
##########
@@ -241,13 +302,111 @@ public String[] associatePoliciesForMetadataObject(
       MetadataObject metadataObject,
       String[] policiesToAdd,
       String[] policiesToRemove) {
-    throw new UnsupportedOperationException("Not implemented yet");
+    Preconditions.checkArgument(
+        Policy.SUPPORTS_ALL_OBJECT_TYPES.contains(metadataObject.type()),
+        "Cannot associate policies for unsupported metadata object type %s",
+        metadataObject.type());
+
+    NameIdentifier entityIdent = MetadataObjectUtil.toEntityIdent(metalake, 
metadataObject);
+    Entity.EntityType entityType = 
MetadataObjectUtil.toEntityType(metadataObject);
+
+    MetadataObjectUtil.checkMetadataObject(metalake, metadataObject);
+
+    // Remove all the policies that are both set to add and remove
+    Set<String> policiesToAddSet =
+        policiesToAdd == null ? Sets.newHashSet() : 
Sets.newHashSet(policiesToAdd);
+    Set<String> policiesToRemoveSet =
+        policiesToRemove == null ? Sets.newHashSet() : 
Sets.newHashSet(policiesToRemove);
+    Set<String> common = Sets.intersection(policiesToAddSet, 
policiesToRemoveSet).immutableCopy();
+    policiesToAddSet.removeAll(common);
+    policiesToRemoveSet.removeAll(common);
+
+    NameIdentifier[] policiesToAddIdent =
+        policiesToAddSet.stream()
+            .map(p -> NameIdentifierUtil.ofPolicy(metalake, p))
+            .toArray(NameIdentifier[]::new);
+    NameIdentifier[] policiesToRemoveIdent =
+        policiesToRemoveSet.stream()
+            .map(p -> NameIdentifierUtil.ofPolicy(metalake, p))
+            .toArray(NameIdentifier[]::new);
+
+    checkMetalake(NameIdentifier.of(metalake), entityStore);
+    return TreeLockUtils.doWithTreeLock(
+        entityIdent,
+        LockType.READ,
+        () ->
+            TreeLockUtils.doWithTreeLock(
+                NameIdentifier.of(NamespaceUtil.ofPolicy(metalake).levels()),
+                LockType.WRITE,
+                () -> {
+                  try {
+                    List<PolicyEntity> updatedPolicies =
+                        entityStore
+                            .relationOperations()
+                            .updateEntityRelations(
+                                
SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,
+                                entityIdent,
+                                entityType,
+                                policiesToAddIdent,
+                                policiesToRemoveIdent);
+                    return 
updatedPolicies.stream().map(PolicyEntity::name).toArray(String[]::new);
+                  } catch (NoSuchEntityException e) {
+                    throw new NoSuchMetadataObjectException(
+                        e,
+                        "Failed to associate policies for metadata object %s 
due to not found",
+                        metadataObject);
+                  } catch (EntityAlreadyExistsException e) {
+                    throw new PolicyAlreadyAssociatedException(
+                        e,
+                        "Failed to associate policies for metadata object due 
to some policies %s already "
+                            + "associated to the metadata object %s",
+                        Arrays.toString(policiesToAdd),
+                        metadataObject);
+                  } catch (IOException e) {
+                    LOG.error(
+                        "Failed to associate policies for metadata object {}", 
metadataObject, e);
+                    throw new RuntimeException(e);
+                  }
+                }));
   }
 
   @Override
   public Policy getPolicyForMetadataObject(
       String metalake, MetadataObject metadataObject, String policyName) {
-    throw new UnsupportedOperationException("Not implemented yet");
+    NameIdentifier entityIdent = MetadataObjectUtil.toEntityIdent(metalake, 
metadataObject);
+    Entity.EntityType entityType = 
MetadataObjectUtil.toEntityType(metadataObject);
+    NameIdentifier policyIdent = NameIdentifierUtil.ofPolicy(metalake, 
policyName);
+
+    MetadataObjectUtil.checkMetadataObject(metalake, metadataObject);
+    checkMetalake(NameIdentifier.of(metalake), entityStore);
+
+    return TreeLockUtils.doWithTreeLock(
+        entityIdent,
+        LockType.READ,
+        () -> {
+          try {
+            return entityStore
+                .relationOperations()
+                .getEntityByRelation(
+                    SupportsRelationOperations.Type.POLICY_METADATA_OBJECT_REL,
+                    entityIdent,
+                    entityType,
+                    policyIdent);
+          } catch (NoSuchEntityException e) {
+            if (e.getMessage().contains("No such policy entity")) {
+              throw new NoSuchPolicyException(
+                  e, "Policy %s does not exist for metadata object %s", 
policyName, metadataObject);
+            } else {
+              throw new NoSuchMetadataObjectException(
+                  e,
+                  "Failed to get policy for metadata object %s due to not 
found",
+                  metadataObject);
+            }
+          } catch (IOException e) {
+            LOG.error("Failed to get policy for metadata object {}", 
metadataObject, e);
+            throw new RuntimeException(e);
+          }
+        });

Review Comment:
   in server module



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to