Skip to content

Commit

Permalink
Merge pull request #3498 from atlanhq/mesh-184-link-beta
Browse files Browse the repository at this point in the history
MESH-184 Validations for domainGUIDs de-norm attribute
  • Loading branch information
PRATHAM2002-DS committed Sep 10, 2024
2 parents 129b6fa + 20b1c87 commit 50359b9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class AssetPreProcessor implements PreProcessor {
private AtlasTypeRegistry typeRegistry;
private EntityGraphRetriever entityRetriever;

private static final Set<String> excludedTypes = new HashSet<>(Arrays.asList(ATLAS_GLOSSARY_ENTITY_TYPE, ATLAS_GLOSSARY_TERM_ENTITY_TYPE, ATLAS_GLOSSARY_CATEGORY_ENTITY_TYPE, DATA_PRODUCT_ENTITY_TYPE, DATA_DOMAIN_ENTITY_TYPE));

public AssetPreProcessor(AtlasTypeRegistry typeRegistry, EntityGraphRetriever entityRetriever) {
this.typeRegistry = typeRegistry;
this.entityRetriever = entityRetriever;
Expand Down Expand Up @@ -82,15 +84,14 @@ private void processDomainLinkAttribute(AtlasEntity entity) throws AtlasBaseExce

private void validateDomainAssetLinks(AtlasEntity entity) throws AtlasBaseException {
List<String> domainGuids = ( List<String>) entity.getAttribute(DOMAIN_GUIDS);
Set<String> excludedTypes = new HashSet<>(Arrays.asList(ATLAS_GLOSSARY_ENTITY_TYPE, ATLAS_GLOSSARY_TERM_ENTITY_TYPE, ATLAS_GLOSSARY_CATEGORY_ENTITY_TYPE, DATA_PRODUCT_ENTITY_TYPE, DATA_DOMAIN_ENTITY_TYPE));

if(CollectionUtils.isNotEmpty(domainGuids)){
if(domainGuids.size() > 1) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "Asset can be linked to only one domain");
}

if (excludedTypes.contains(entity.getTypeName())) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "Asset Type {} is not allowed to link with Domain", entity.getTypeName());
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, " This AssetType is not allowed to link with Domain");
}

for(String domainGuid : domainGuids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.atlas.authorize.AtlasPrivilege;
import org.apache.atlas.discovery.EntityDiscoveryService;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.discovery.IndexSearchParams;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasEntityHeader;
import org.apache.atlas.model.instance.AtlasObjectId;
Expand Down Expand Up @@ -70,6 +71,7 @@ public abstract class AbstractDomainPreProcessor implements PreProcessor {

private static final Set<String> POLICY_ATTRIBUTES_FOR_SEARCH = new HashSet<>(Arrays.asList(ATTR_POLICY_RESOURCES));
private static final Set<String> STAKEHOLDER_ATTRIBUTES_FOR_SEARCH = new HashSet<>(Arrays.asList(ATTR_DOMAIN_QUALIFIED_NAMES, ATTR_DOMAIN_QUALIFIED_NAME));
private static final Set<String> DOMAIN_GUID_ATTR = new HashSet<>(Arrays.asList(DOMAIN_GUIDS));

static final Set<String> PARENT_ATTRIBUTES = new HashSet<>(Arrays.asList(SUPER_DOMAIN_QN_ATTR, PreProcessorUtils.PARENT_DOMAIN_QN_ATTR));

Expand Down Expand Up @@ -286,6 +288,58 @@ protected List<AtlasEntityHeader> getPolicies(Set<String> resources) throws Atla
}
}

protected Boolean isAssetLinked(String domainGuid) throws AtlasBaseException {
AtlasPerfMetrics.MetricRecorder metricRecorder = RequestContext.get().startMetricRecord("isAssetLinked");
boolean exists = false;
try {
List<Map<String, Object>> mustClauseList = new ArrayList<>();
mustClauseList.add(mapOf("term", mapOf("__state", "ACTIVE")));
mustClauseList.add(mapOf("terms", mapOf(DOMAIN_GUIDS, domainGuid)));

Map<String, Object> bool = new HashMap<>();
bool.put("must", mustClauseList);

Map<String, Object> dsl = mapOf("query", mapOf("bool", bool));

List<AtlasEntityHeader> assets = fetchLinkedAssets(dsl, DOMAIN_GUID_ATTR, this.discovery);
if (CollectionUtils.isNotEmpty(assets)) {
exists = true;
}

return exists;

} finally {
RequestContext.get().endMetricRecord(metricRecorder);
}
}

protected static List<AtlasEntityHeader> fetchLinkedAssets(Map<String, Object> dsl, Set<String> attributes, EntityDiscoveryService discovery) throws AtlasBaseException {
IndexSearchParams searchParams = new IndexSearchParams();
List<AtlasEntityHeader> ret = new ArrayList<>();

if (CollectionUtils.isNotEmpty(attributes)) {
searchParams.setAttributes(attributes);
}

List<Map> sortList = new ArrayList<>(0);
sortList.add(mapOf("__timestamp", mapOf("order", "asc")));
sortList.add(mapOf("__guid", mapOf("order", "asc")));
dsl.put("sort", sortList);

int from = 0;
int size = 1;
dsl.put("from", from);
dsl.put("size", size);
searchParams.setDsl(dsl);

List<AtlasEntityHeader> headers = discovery.directIndexSearch(searchParams).getEntities();

if (CollectionUtils.isNotEmpty(headers)) {
ret.addAll(headers);
}
return ret;
}

protected List<AtlasEntityHeader> getStakeholderTitlesAndStakeholders(Set<String> qualifiedNames) throws AtlasBaseException {
AtlasPerfMetrics.MetricRecorder metricRecorder = RequestContext.get().startMetricRecord("getStakeholderTitlesAndStakeholders");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ public void processDelete(AtlasVertex vertex) throws AtlasBaseException {

try{
List<String> stakeHolderGuids = new ArrayList<>();
String domainGUID = GraphHelper.getGuid(vertex);

if(isAssetLinked(domainGUID)) {
throw new AtlasBaseException(AtlasErrorCode.OPERATION_NOT_SUPPORTED, "Domain cannot be deleted because some assets are linked to this domain");
}

// active childrens exists?
Iterator<AtlasVertex> childrens = getActiveChildrenVertices(vertex,
Expand Down

0 comments on commit 50359b9

Please sign in to comment.