mneethiraj commented on code in PR #895: URL: https://github.com/apache/ranger/pull/895#discussion_r3001749267
########## tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasTrinoResourceMapper.java: ########## @@ -0,0 +1,123 @@ +/* + * 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.ranger.tagsync.source.atlas; + +import org.apache.commons.lang3.StringUtils; +import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource; +import org.apache.ranger.plugin.model.RangerServiceResource; +import org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity; + +import java.util.HashMap; +import java.util.Map; + +public class AtlasTrinoResourceMapper extends AtlasResourceMapper { + public static final String ENTITY_TYPE_TRINO_INSTANCE = "trino_instance"; + public static final String ENTITY_TYPE_TRINO_CATALOG = "trino_catalog"; + public static final String ENTITY_TYPE_TRINO_SCHEMA = "trino_schema"; + public static final String ENTITY_TYPE_TRINO_TABLE = "trino_table"; + public static final String ENTITY_TYPE_TRINO_COLUMN = "trino_column"; + public static final String RANGER_TYPE_TRINO_CATALOG = "catalog"; + public static final String RANGER_TYPE_TRINO_SCHEMA = "schema"; + public static final String RANGER_TYPE_TRINO_TABLE = "table"; + public static final String RANGER_TYPE_TRINO_COLUMN = "column"; + + public static final String[] SUPPORTED_ENTITY_TYPES = { + ENTITY_TYPE_TRINO_INSTANCE, + ENTITY_TYPE_TRINO_CATALOG, + ENTITY_TYPE_TRINO_SCHEMA, + ENTITY_TYPE_TRINO_TABLE, + ENTITY_TYPE_TRINO_COLUMN + }; + + public AtlasTrinoResourceMapper() { + super("trino", SUPPORTED_ENTITY_TYPES); + } + + /* + * qualifiedName can be of format, depending upon the entity-type: + * trino_instance: <instanceName> + * trino_catalog: <catalog>@<instanceName> + * trino_schema: <catalog>.<schema>@<instanceName> + * trino_table: <catalog>.<schema>.<table>@<instanceName> + * trino_column: <catalog>.<schema>.<table>.<column>@<instanceName> + */ + @Override + public RangerServiceResource buildResource(RangerAtlasEntity entity) throws Exception { + String qualifiedName = (String) entity.getAttributes().get(AtlasResourceMapper.ENTITY_ATTRIBUTE_QUALIFIED_NAME); + + if (StringUtils.isEmpty(qualifiedName)) { + throw new Exception("attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "' not found in entity"); + } + + String entityType = entity.getTypeName(); + String entityGuid = entity.getGuid(); + String resourceStr = getResourceNameFromQualifiedName(qualifiedName); + + if (StringUtils.isEmpty(resourceStr)) { + throwExceptionWithMessage("resource not found in attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "': " + qualifiedName); + } + + String trinoInstance = getClusterNameFromQualifiedName(qualifiedName); + if (StringUtils.isEmpty(trinoInstance)) { Review Comment: Looking at qualified name for `trino_instance` at line 55 above (and line 90 below), `trinoInstance` here will be empty for entity type `trino_instance`, right? ########## tagsync/src/test/java/org/apache/ranger/tagsync/process/TestTrinoResourceMapper.java: ########## @@ -0,0 +1,185 @@ +/* + * 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.ranger.tagsync.process; + +import org.apache.ranger.plugin.model.RangerServiceResource; +import org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper; +import org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +import static org.apache.ranger.tagsync.source.atlas.AtlasResourceMapper.ENTITY_ATTRIBUTE_QUALIFIED_NAME; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_CATALOG; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_COLUMN; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_INSTANCE; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_SCHEMA; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_TABLE; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_CATALOG; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_COLUMN; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_SCHEMA; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_TABLE; + +public class TestTrinoResourceMapper { + private static final String INSTANCE_QUALIFIED_NAME = "dev"; + private static final String CATALOG_QUALIFIED_NAME = "sales@dev"; + private static final String SCHEMA_QUALIFIED_NAME = "sales.reporting@dev"; + private static final String TABLE_QUALIFIED_NAME = "sales.reporting.orders@dev"; + private static final String COLUMN_QUALIFIED_NAME = "sales.reporting.orders.customer_id@dev"; + private static final String INVALID_RESOURCE_QUALIFIED_NAME = "sales.reporting.orders.customer_id.extra@dev"; + + private static final String SERVICE_NAME = "dev_trino"; + private static final String RANGER_CATALOG = "sales"; + private static final String RANGER_SCHEMA = "reporting"; + private static final String RANGER_TABLE = "orders"; + private static final String RANGER_COLUMN = "customer_id"; + + AtlasTrinoResourceMapper resourceMapper = new AtlasTrinoResourceMapper(); + + @Test Review Comment: Add tests for `trino_instance` type as well - `testTrinoInstance()`. ########## tagsync/src/test/java/org/apache/ranger/tagsync/process/TestTrinoResourceMapper.java: ########## @@ -0,0 +1,185 @@ +/* + * 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.ranger.tagsync.process; + +import org.apache.ranger.plugin.model.RangerServiceResource; +import org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper; +import org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +import static org.apache.ranger.tagsync.source.atlas.AtlasResourceMapper.ENTITY_ATTRIBUTE_QUALIFIED_NAME; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_CATALOG; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_COLUMN; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_INSTANCE; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_SCHEMA; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.ENTITY_TYPE_TRINO_TABLE; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_CATALOG; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_COLUMN; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_SCHEMA; +import static org.apache.ranger.tagsync.source.atlas.AtlasTrinoResourceMapper.RANGER_TYPE_TRINO_TABLE; + +public class TestTrinoResourceMapper { + private static final String INSTANCE_QUALIFIED_NAME = "dev"; + private static final String CATALOG_QUALIFIED_NAME = "sales@dev"; + private static final String SCHEMA_QUALIFIED_NAME = "sales.reporting@dev"; + private static final String TABLE_QUALIFIED_NAME = "sales.reporting.orders@dev"; + private static final String COLUMN_QUALIFIED_NAME = "sales.reporting.orders.customer_id@dev"; + private static final String INVALID_RESOURCE_QUALIFIED_NAME = "sales.reporting.orders.customer_id.extra@dev"; + + private static final String SERVICE_NAME = "dev_trino"; + private static final String RANGER_CATALOG = "sales"; + private static final String RANGER_SCHEMA = "reporting"; + private static final String RANGER_TABLE = "orders"; + private static final String RANGER_COLUMN = "customer_id"; + + AtlasTrinoResourceMapper resourceMapper = new AtlasTrinoResourceMapper(); + + @Test + public void testTrinoCatalog() throws Exception { + RangerAtlasEntity entity = getEntity(ENTITY_TYPE_TRINO_CATALOG, CATALOG_QUALIFIED_NAME); + RangerServiceResource resource = resourceMapper.buildResource(entity); + + Assertions.assertEquals(SERVICE_NAME, resource.getServiceName()); + assertCatalogResource(resource); + } + + @Test + public void testTrinoSchema() throws Exception { + RangerAtlasEntity entity = getEntity(ENTITY_TYPE_TRINO_SCHEMA, SCHEMA_QUALIFIED_NAME); + RangerServiceResource resource = resourceMapper.buildResource(entity); + + Assertions.assertEquals(SERVICE_NAME, resource.getServiceName()); + assertSchemaResource(resource); + } + + @Test + public void testTrinoTable() throws Exception { + RangerAtlasEntity entity = getEntity(ENTITY_TYPE_TRINO_TABLE, TABLE_QUALIFIED_NAME); + RangerServiceResource resource = resourceMapper.buildResource(entity); + + Assertions.assertEquals(SERVICE_NAME, resource.getServiceName()); + assertTableResource(resource); + } + + @Test + public void testTrinoColumn() throws Exception { + RangerAtlasEntity entity = getEntity(ENTITY_TYPE_TRINO_COLUMN, COLUMN_QUALIFIED_NAME); + RangerServiceResource resource = resourceMapper.buildResource(entity); + + Assertions.assertEquals(SERVICE_NAME, resource.getServiceName()); + assertColumnResource(resource); + } + + @Test + public void testInvalidCatalogEntity() { + assertException(getEntity(ENTITY_TYPE_TRINO_CATALOG, null), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_CATALOG, ""), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_CATALOG, "sales"), "trino-instance not found"); + } + + @Test + public void testInvalidSchemaEntity() { + assertException(getEntity(ENTITY_TYPE_TRINO_SCHEMA, null), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_SCHEMA, ""), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_SCHEMA, "sales.reporting"), "trino-instance not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_SCHEMA, CATALOG_QUALIFIED_NAME), "invalid qualifiedName"); + } + + @Test + public void testInvalidTableEntity() { + assertException(getEntity(ENTITY_TYPE_TRINO_TABLE, null), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_TABLE, ""), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_TABLE, "sales.reporting.orders"), "trino-instance not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_TABLE, SCHEMA_QUALIFIED_NAME), "invalid qualifiedName"); + } + + @Test + public void testInvalidColumnEntity() { + assertException(getEntity(ENTITY_TYPE_TRINO_COLUMN, null), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_COLUMN, ""), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_COLUMN, "sales.reporting.orders.customer_id"), "trino-instance not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_COLUMN, TABLE_QUALIFIED_NAME), "invalid qualifiedName"); + assertException(getEntity(ENTITY_TYPE_TRINO_COLUMN, INVALID_RESOURCE_QUALIFIED_NAME), "invalid resource format"); + } + + @Test + public void testInvalidInstanceEntity() { + assertException(getEntity(ENTITY_TYPE_TRINO_INSTANCE, null), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_INSTANCE, ""), "attribute 'qualifiedName' not found"); + assertException(getEntity(ENTITY_TYPE_TRINO_INSTANCE, INSTANCE_QUALIFIED_NAME), "trino-instance not found"); + } + + private RangerAtlasEntity getEntity(String entityType, String qualifiedName) { + return new RangerAtlasEntity(entityType, "guid-" + entityType, Collections.singletonMap(ENTITY_ATTRIBUTE_QUALIFIED_NAME, qualifiedName)); + } + + private void assertResourceElementCount(RangerServiceResource resource, int count) { + Assertions.assertNotNull(resource); + Assertions.assertEquals(SERVICE_NAME, resource.getServiceName()); + Assertions.assertNotNull(resource.getResourceElements()); + Assertions.assertEquals(count, resource.getResourceElements().size()); + } + + private void assertCatalogResource(RangerServiceResource resource) { + assertResourceElementCount(resource, 1); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_CATALOG, RANGER_CATALOG); + } + + private void assertSchemaResource(RangerServiceResource resource) { + assertResourceElementCount(resource, 2); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_CATALOG, RANGER_CATALOG); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_SCHEMA, RANGER_SCHEMA); + } + + private void assertTableResource(RangerServiceResource resource) { + assertResourceElementCount(resource, 3); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_CATALOG, RANGER_CATALOG); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_SCHEMA, RANGER_SCHEMA); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_TABLE, RANGER_TABLE); + } + + private void assertColumnResource(RangerServiceResource resource) { + assertResourceElementCount(resource, 4); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_CATALOG, RANGER_CATALOG); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_SCHEMA, RANGER_SCHEMA); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_TABLE, RANGER_TABLE); + assertResourceElementValue(resource, RANGER_TYPE_TRINO_COLUMN, RANGER_COLUMN); + } + + private void assertResourceElementValue(RangerServiceResource resource, String resourceName, String value) { + Assertions.assertTrue(resource.getResourceElements().containsKey(resourceName)); + Assertions.assertNotNull(resource.getResourceElements().get(resourceName).getValues()); + Assertions.assertEquals(1, resource.getResourceElements().get(resourceName).getValues().size()); + Assertions.assertEquals(value, resource.getResourceElements().get(resourceName).getValues().get(0)); + } + + private void assertException(RangerAtlasEntity entity, String exceptionMessage) { + try { + RangerServiceResource resource = resourceMapper.buildResource(entity); + + Assertions.assertFalse(true, "Expected buildResource() to fail. But it returned " + resource); Review Comment: Replace `Assertions.assertFalse(true, ` with `Assertions.fail(` -- 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]
