Search in sources :

Example 1 with ResourceAlreadyExistsException

use of org.apache.atlas.catalog.exception.ResourceAlreadyExistsException in project incubator-atlas by apache.

the class DefaultTypeSystem method createType.

private <T extends HierarchicalType> void createType(Collection<AttributeDefinition> attributes, Class<T> type, String name, String description, boolean isTrait) throws ResourceAlreadyExistsException {
    try {
        List<AtlasStructDef.AtlasAttributeDef> attrDefs = new ArrayList<>();
        for (AttributeDefinition attrDefinition : attributes) {
            attrDefs.add(TypeConverterUtil.toAtlasAttributeDef(attrDefinition));
        }
        if (isTrait) {
            AtlasClassificationDef classificationDef = new AtlasClassificationDef(name, description, "1.0", attrDefs, ImmutableSet.of(TaxonomyResourceProvider.TAXONOMY_TERM_TYPE));
            AtlasTypesDef typesDef = new AtlasTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.of(classificationDef), ImmutableList.<AtlasEntityDef>of());
            typeDefStore.createTypesDef(typesDef);
        } else {
            AtlasEntityDef entityDef = new AtlasEntityDef(name, description, "1.0", attrDefs);
            AtlasTypesDef typesDef = new AtlasTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.of(entityDef));
            typeDefStore.createTypesDef(typesDef);
        }
    } catch (AtlasBaseException e) {
        if (e.getAtlasErrorCode() == AtlasErrorCode.TYPE_ALREADY_EXISTS) {
            throw new ResourceAlreadyExistsException(String.format("Type '%s' already exists", name));
        } else {
            throw new CatalogRuntimeException(String.format("Unable to create type '%s' in type system: %s", name, e), e);
        }
    }
}
Also used : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) CatalogRuntimeException(org.apache.atlas.catalog.exception.CatalogRuntimeException) ArrayList(java.util.ArrayList) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) ResourceAlreadyExistsException(org.apache.atlas.catalog.exception.ResourceAlreadyExistsException) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef)

Example 2 with ResourceAlreadyExistsException

use of org.apache.atlas.catalog.exception.ResourceAlreadyExistsException in project incubator-atlas by apache.

the class DefaultTypeSystem method createTraitInstance.

@Override
public void createTraitInstance(String guid, String typeName, Map<String, Object> properties) throws ResourceAlreadyExistsException {
    try {
        // not using the constructor with properties argument because it is marked 'InterfaceAudience.Private'
        Struct struct = new Struct(typeName);
        for (Map.Entry<String, Object> propEntry : properties.entrySet()) {
            struct.set(propEntry.getKey(), propEntry.getValue());
        }
        // add Taxonomy Namespace
        struct.set(TaxonomyResourceProvider.NAMESPACE_ATTRIBUTE_NAME, TaxonomyResourceProvider.TAXONOMY_NS);
        metadataService.addTrait(guid, metadataService.createTraitInstance(struct));
    } catch (IllegalArgumentException e) {
        // todo: unfortunately, IllegalArgumentException can be thrown for other reasons
        if (e.getMessage().contains("is already defined for entity")) {
            throw new ResourceAlreadyExistsException(String.format("Tag '%s' already associated with the entity", typeName));
        } else {
            throw e;
        }
    } catch (AtlasException e) {
        throw new CatalogRuntimeException(String.format("Unable to create trait instance '%s' in type system: %s", typeName, e), e);
    }
}
Also used : CatalogRuntimeException(org.apache.atlas.catalog.exception.CatalogRuntimeException) ResourceAlreadyExistsException(org.apache.atlas.catalog.exception.ResourceAlreadyExistsException) AtlasException(org.apache.atlas.AtlasException) Map(java.util.Map) Struct(org.apache.atlas.typesystem.Struct)

Example 3 with ResourceAlreadyExistsException

use of org.apache.atlas.catalog.exception.ResourceAlreadyExistsException in project incubator-atlas by apache.

the class TermResourceProviderTest method testCreateResource_invalidRequest__alreadyExists.

@Test(expectedExceptions = ResourceAlreadyExistsException.class)
public void testCreateResource_invalidRequest__alreadyExists() throws Exception {
    AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
    QueryFactory queryFactory = createStrictMock(QueryFactory.class);
    AtlasQuery query = createStrictMock(AtlasQuery.class);
    Capture<ResourceDefinition> resourceDefinitionCapture = newCapture();
    ResourceProvider taxonomyResourceProvider = createStrictMock(TaxonomyResourceProvider.class);
    Capture<Request> taxonomyRequestCapture = newCapture();
    Collection<Map<String, Object>> taxonomyQueryResult = new ArrayList<>();
    Map<String, Object> taxonomyQueryResultRow = new HashMap<>();
    taxonomyQueryResult.add(taxonomyQueryResultRow);
    taxonomyQueryResultRow.put("name", "testTaxonomy");
    taxonomyQueryResultRow.put("id", "11-22-33");
    Result taxonomyResult = new Result(taxonomyQueryResult);
    // mock expectations
    expect(taxonomyResourceProvider.getResourceById(capture(taxonomyRequestCapture))).andReturn(taxonomyResult);
    typeSystem.createTraitType(capture(resourceDefinitionCapture), eq("testTaxonomy.termName"), EasyMock.<String>isNull());
    expectLastCall().andThrow(new ResourceAlreadyExistsException(""));
    replay(typeSystem, queryFactory, query, taxonomyResourceProvider);
    TermResourceProvider provider = new TestTermResourceProvider(typeSystem, taxonomyResourceProvider);
    provider.setQueryFactory(queryFactory);
    TermPath termPath = new TermPath("testTaxonomy", "termName");
    Map<String, Object> requestProperties = new HashMap<>();
    requestProperties.put("termPath", termPath);
    Request userRequest = new InstanceRequest(requestProperties);
    provider.createResource(userRequest);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) ResourceDefinition(org.apache.atlas.catalog.definition.ResourceDefinition) ResourceAlreadyExistsException(org.apache.atlas.catalog.exception.ResourceAlreadyExistsException) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Example 4 with ResourceAlreadyExistsException

use of org.apache.atlas.catalog.exception.ResourceAlreadyExistsException in project incubator-atlas by apache.

the class EntityTagResourceProviderTest method testCreateResource_invalidRequest__alreadyExists.

@Test(expectedExceptions = ResourceAlreadyExistsException.class)
public void testCreateResource_invalidRequest__alreadyExists() throws Exception {
    AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
    QueryFactory queryFactory = createStrictMock(QueryFactory.class);
    AtlasQuery query = createStrictMock(AtlasQuery.class);
    ResourceProvider termResourceProvider = createStrictMock(TermResourceProvider.class);
    Capture<Request> termRequestCapture = newCapture();
    Collection<Map<String, Object>> termQueryResult = new ArrayList<>();
    Map<String, Object> termQueryResultRow = new HashMap<>();
    termQueryResult.add(termQueryResultRow);
    termQueryResultRow.put("name", "testTaxonomy.termName");
    termQueryResultRow.put("type", "testTaxonomy.termName");
    termQueryResultRow.put("available_as_tag", true);
    termQueryResultRow.put("description", "term description");
    Result termResult = new Result(termQueryResult);
    // mock expectations
    expect(termResourceProvider.getResourceById(capture(termRequestCapture))).andReturn(termResult);
    Map<String, Object> tagProperties = new HashMap<>();
    tagProperties.put("name", "testTaxonomy.termName");
    tagProperties.put("description", "term description");
    typeSystem.createTraitInstance("11-22-33", "testTaxonomy.termName", tagProperties);
    expectLastCall().andThrow(new ResourceAlreadyExistsException(""));
    replay(typeSystem, queryFactory, query, termResourceProvider);
    EntityTagResourceProvider provider = new TestEntityTagResourceProvider(typeSystem, termResourceProvider);
    provider.setQueryFactory(queryFactory);
    Map<String, Object> requestProperties = new HashMap<>();
    requestProperties.put("name", "testTaxonomy.termName");
    requestProperties.put("id", "11-22-33");
    Request userRequest = new InstanceRequest(requestProperties);
    provider.createResource(userRequest);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) ResourceAlreadyExistsException(org.apache.atlas.catalog.exception.ResourceAlreadyExistsException) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Example 5 with ResourceAlreadyExistsException

use of org.apache.atlas.catalog.exception.ResourceAlreadyExistsException in project incubator-atlas by apache.

the class DefaultTypeSystem method createEntity.

@Override
public String createEntity(ResourceDefinition definition, Request request) throws ResourceAlreadyExistsException {
    String typeName = definition.getTypeName();
    try {
        createClassType(definition, typeName, typeName + " Definition");
    } catch (ResourceAlreadyExistsException e) {
    // ok if type already exists
    }
    try {
        Referenceable entity = new Referenceable(typeName, request.getQueryProperties());
        // add Taxonomy Namespace
        entity.set(TaxonomyResourceProvider.NAMESPACE_ATTRIBUTE_NAME, TaxonomyResourceProvider.TAXONOMY_NS);
        ITypedReferenceableInstance typedInstance = metadataService.getTypedReferenceableInstance(entity);
        ITypedReferenceableInstance[] entitiesToCreate = Collections.singletonList(typedInstance).toArray(new ITypedReferenceableInstance[1]);
        final List<String> entities = metadataService.createEntities(entitiesToCreate).getCreatedEntities();
        return entities != null && entities.size() > 0 ? entities.get(0) : null;
    } catch (EntityExistsException e) {
        throw new ResourceAlreadyExistsException("Attempted to create an entity which already exists: " + request.getQueryProperties());
    } catch (AtlasException e) {
        throw new CatalogRuntimeException("An expected exception occurred creating an entity: " + e, e);
    }
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) CatalogRuntimeException(org.apache.atlas.catalog.exception.CatalogRuntimeException) ResourceAlreadyExistsException(org.apache.atlas.catalog.exception.ResourceAlreadyExistsException) AtlasException(org.apache.atlas.AtlasException) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException)

Aggregations

ResourceAlreadyExistsException (org.apache.atlas.catalog.exception.ResourceAlreadyExistsException)5 CatalogRuntimeException (org.apache.atlas.catalog.exception.CatalogRuntimeException)3 AtlasException (org.apache.atlas.AtlasException)2 AtlasQuery (org.apache.atlas.catalog.query.AtlasQuery)2 QueryFactory (org.apache.atlas.catalog.query.QueryFactory)2 Test (org.testng.annotations.Test)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ResourceDefinition (org.apache.atlas.catalog.definition.ResourceDefinition)1 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)1 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)1 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)1 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)1 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)1 Referenceable (org.apache.atlas.typesystem.Referenceable)1 Struct (org.apache.atlas.typesystem.Struct)1 EntityExistsException (org.apache.atlas.typesystem.exception.EntityExistsException)1 AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)1