@@ -1001,9 +1001,19 @@ void attachPolicyToTarget(
10011001 PolarisBaseEntity target ,
10021002 List <PolarisEntityCore > policyCatalogPath ,
10031003 PolicyEntity policy ) {
1004+ attachPolicyToTarget (targetCatalogPath , target , policyCatalogPath , policy , null );
1005+ }
1006+
1007+ void attachPolicyToTarget (
1008+ List <PolarisEntityCore > targetCatalogPath ,
1009+ PolarisBaseEntity target ,
1010+ List <PolarisEntityCore > policyCatalogPath ,
1011+ PolicyEntity policy ,
1012+ Map <String , String > parameters ) {
10041013 polarisMetaStoreManager .attachPolicyToEntity (
1005- polarisCallContext , targetCatalogPath , target , policyCatalogPath , policy , null );
1006- ensurePolicyMappingRecordExists (target , policy );
1014+ polarisCallContext , targetCatalogPath , target , policyCatalogPath , policy , parameters );
1015+
1016+ ensurePolicyMappingRecordExists (target , policy , parameters );
10071017 }
10081018
10091019 /** detach a policy from a target */
@@ -1022,8 +1032,10 @@ void detachPolicyFromTarget(
10221032 *
10231033 * @param target the target
10241034 * @param policy the policy
1035+ * @param parameters the parameters
10251036 */
1026- void ensurePolicyMappingRecordExists (PolarisBaseEntity target , PolicyEntity policy ) {
1037+ void ensurePolicyMappingRecordExists (
1038+ PolarisBaseEntity target , PolicyEntity policy , Map <String , String > parameters ) {
10271039 target =
10281040 polarisMetaStoreManager
10291041 .loadEntity (
@@ -1048,15 +1060,15 @@ void ensurePolicyMappingRecordExists(PolarisBaseEntity target, PolicyEntity poli
10481060 validateLoadedPolicyMappings (loadPolicyMappingsResult );
10491061
10501062 checkPolicyMappingRecordExists (
1051- loadPolicyMappingsResult .getPolicyMappingRecords (), target , policy );
1063+ loadPolicyMappingsResult .getPolicyMappingRecords (), target , policy , parameters );
10521064
10531065 // also try load by specific type
10541066 LoadPolicyMappingsResult loadPolicyMappingsResultByType =
10551067 polarisMetaStoreManager .loadPoliciesOnEntityByType (
10561068 this .polarisCallContext , target , policy .getPolicyType ());
10571069 validateLoadedPolicyMappings (loadPolicyMappingsResultByType );
10581070 checkPolicyMappingRecordExists (
1059- loadPolicyMappingsResultByType .getPolicyMappingRecords (), target , policy );
1071+ loadPolicyMappingsResultByType .getPolicyMappingRecords (), target , policy , parameters );
10601072 }
10611073
10621074 /**
@@ -1140,8 +1152,9 @@ void validateLoadedPolicyMappings(LoadPolicyMappingsResult loadPolicyMappingReco
11401152 void checkPolicyMappingRecordExists (
11411153 List <PolarisPolicyMappingRecord > policyMappingRecords ,
11421154 PolarisBaseEntity target ,
1143- PolicyEntity policy ) {
1144- boolean exists = isPolicyMappingRecordExists (policyMappingRecords , target , policy );
1155+ PolicyEntity policy ,
1156+ Map <String , String > parameters ) {
1157+ boolean exists = isPolicyMappingRecordExists (policyMappingRecords , target , policy , parameters );
11451158 Assertions .assertThat (exists ).isTrue ();
11461159 }
11471160
@@ -1184,6 +1197,32 @@ record ->
11841197 return policyMappingCount == 1 ;
11851198 }
11861199
1200+ /**
1201+ * Check if the policy mapping record exists and verify if the parameters also equals
1202+ *
1203+ * @param policyMappingRecords list of policy mapping records
1204+ * @param target the target
1205+ * @param policy the policy
1206+ * @param parameters the parameters
1207+ */
1208+ boolean isPolicyMappingRecordExists (
1209+ List <PolarisPolicyMappingRecord > policyMappingRecords ,
1210+ PolarisBaseEntity target ,
1211+ PolicyEntity policy ,
1212+ Map <String , String > parameters ) {
1213+ PolarisPolicyMappingRecord expected =
1214+ new PolarisPolicyMappingRecord (
1215+ target .getCatalogId (),
1216+ target .getId (),
1217+ policy .getCatalogId (),
1218+ policy .getId (),
1219+ policy .getPolicyTypeCode (),
1220+ parameters );
1221+ long policyMappingCount =
1222+ policyMappingRecords .stream ().filter (record -> expected .equals (record )).count ();
1223+ return policyMappingCount == 1 ;
1224+ }
1225+
11871226 /**
11881227 * Create a test catalog. This is a new catalog which will have the following objects (N is for a
11891228 * namespace, T for a table, V for a view, R for a role, P for a principal, POL for a policy):
@@ -2781,6 +2820,10 @@ void testPolicyMapping() {
27812820 Assertions .assertThat (policyAttachmentResult .getReturnStatus ())
27822821 .isEqualTo (BaseResult .ReturnStatus .POLICY_MAPPING_OF_SAME_TYPE_ALREADY_EXISTS );
27832822
2823+ // Attach the same policy to same target again should succeed and replace the existing one
2824+ attachPolicyToTarget (
2825+ List .of (catalog , N1 , N1_N2 ), N1_N2_T1 , List .of (catalog , N1 ), N1_P1 , Map .of ("test" , "test" ));
2826+
27842827 LoadPolicyMappingsResult loadPolicyMappingsResult =
27852828 polarisMetaStoreManager .loadPoliciesOnEntityByType (
27862829 polarisCallContext , N1_N2_T1 , PredefinedPolicyTypes .DATA_COMPACTION );
0 commit comments