mneethiraj commented on code in PR #895: URL: https://github.com/apache/ranger/pull/895#discussion_r3003676463
########## tagsync/src/test/java/org/apache/ranger/tagsync/process/TestTrinoResourceMapper.java: ########## @@ -0,0 +1,212 @@ +/* + * 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 testTrinoInstance() throws Exception { Review Comment: Tests for `ENTITY_TYPE_TRINO_INSTANCE` can either be removed or modified, as the mapper doesn't handle this entity type. ########## tagsync/src/main/java/org/apache/ranger/tagsync/source/atlas/AtlasTrinoResourceMapper.java: ########## @@ -0,0 +1,126 @@ +/* + * 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_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.equals(resourceStr, qualifiedName)){ + trinoInstance = resourceStr; + } + + if (StringUtils.isEmpty(trinoInstance)) { + throwExceptionWithMessage("trino-instance not found in attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "': " + qualifiedName); + } + String serviceName = getRangerServiceName(trinoInstance); + + String[] parts = resourceStr.split(QUALIFIED_NAME_DELIMITER); + if (parts.length < 1 || parts.length > 4) { + throwExceptionWithMessage("invalid resource format in attribute '" + ENTITY_ATTRIBUTE_QUALIFIED_NAME + "': " + qualifiedName); + } + + Map<String, RangerPolicyResource> elements = new HashMap<>(); + if (StringUtils.equals(entityType, ENTITY_TYPE_TRINO_INSTANCE) && StringUtils.equals(resourceStr, qualifiedName)) { Review Comment: Lines 92 & 93, and 31 can be removed, since entity type `ENTITY_TYPE_TRINO_INSTANCE` is not handled anymore. -- 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]
