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


##########
core/src/main/java/org/apache/gravitino/policy/PolicyManager.java:
##########
@@ -241,13 +302,112 @@ 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);
+
+    return TreeLockUtils.doWithTreeLock(
+        entityIdent,
+        LockType.READ,
+        () ->
+            TreeLockUtils.doWithTreeLock(
+                NameIdentifier.of(NamespaceUtil.ofPolicy(metalake).levels()),
+                LockType.WRITE,
+                () -> {
+                  try {
+                    checkMetalake(NameIdentifier.of(metalake), entityStore);
+
+                    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 "

Review Comment:
   Do nothing, just the same as the tag operation.



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