Search in sources :

Example 6 with NamespaceSummary

use of io.cdap.cdap.api.NamespaceSummary in project cdap by cdapio.

the class DraftServiceTest method testCreateAndDeleteDraft.

private void testCreateAndDeleteDraft(DraftType draftType) throws IOException, TimeoutException, InterruptedException, ExecutionException {
    NamespaceSummary namespace = new NamespaceSummary(NamespaceId.DEFAULT.getNamespace(), "", 0);
    DraftId draftId = new DraftId(namespace, "test-1", "admin");
    Draft expectedDraft;
    if (draftType == DraftType.Batch) {
        expectedDraft = createBatchPipelineDraft(draftId, "TestPipeline1", "This is a test pipeline.");
    } else {
        expectedDraft = createStreamingPipelineDraft(draftId, "TestPipeline1", "This is a test pipeline.");
    }
    Draft fetchedDraft = getDraft(draftId);
    Assert.assertTrue(sameDraft(fetchedDraft, expectedDraft));
    validateMetric(1, Constants.Metrics.DRAFT_COUNT);
    deleteDraftAndCheck(draftId);
    validateMetric(0, Constants.Metrics.DRAFT_COUNT);
}
Also used : Draft(io.cdap.cdap.datapipeline.draft.Draft) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) DraftId(io.cdap.cdap.datapipeline.draft.DraftId)

Example 7 with NamespaceSummary

use of io.cdap.cdap.api.NamespaceSummary in project cdap by cdapio.

the class DraftServiceTest method testDraftAuthorization.

@Test
public void testDraftAuthorization() throws Exception {
    user = ALICE_NAME;
    HttpResponse response;
    // Check Alice can't list requests
    expectedCode = HttpURLConnection.HTTP_FORBIDDEN;
    listDrafts(NamespaceId.DEFAULT.getNamespace(), false, "", "", "");
    // Grant her nesessary privilidges
    Authorizable namespaceAuthorizable = Authorizable.fromEntityId(NamespaceId.DEFAULT);
    getAccessController().grant(namespaceAuthorizable, ALICE_PRINCIPAL, EnumSet.of(StandardPermission.GET));
    // Check Alice can list requests now
    expectedCode = HttpURLConnection.HTTP_OK;
    listDrafts(NamespaceId.DEFAULT.getNamespace(), false, "", "", "");
    // Check Bob still can't do it
    user = "bob";
    expectedCode = HttpURLConnection.HTTP_FORBIDDEN;
    listDrafts(NamespaceId.DEFAULT.getNamespace(), false, "", "", "");
    // Check Alice can't do it for other namespace
    user = ALICE_NAME;
    listDrafts(TEST_NAMESPACE, false, "", "", "");
    // Check Alice can't create drafts
    NamespaceSummary namespace = new NamespaceSummary(NamespaceId.DEFAULT.getNamespace(), "", 0);
    DraftId draftId = new DraftId(namespace, "draft1", ALICE_NAME);
    // She can't delete it as well
    expectedCode = HttpURLConnection.HTTP_FORBIDDEN;
    deleteDraft(draftId);
    createBatchPipelineDraft(draftId, "TestPipeline1", "This is a test pipeline.");
    // Grant Alice create priviledge. Note that we don't differenciate create and update,
    // so update is used in both cases
    getAccessController().grant(namespaceAuthorizable, ALICE_PRINCIPAL, EnumSet.of(StandardPermission.UPDATE));
    // Now Alice should be able to create draft
    expectedCode = HttpURLConnection.HTTP_OK;
    createBatchPipelineDraft(draftId, "TestPipeline1", "This is a test pipeline.");
    // Bob still can't get the draft
    expectedCode = HttpURLConnection.HTTP_FORBIDDEN;
    user = "bob";
    getDraft(draftId);
    // Alice should be able to delete draft
    expectedCode = HttpURLConnection.HTTP_OK;
    user = ALICE_NAME;
    deleteDraft(draftId);
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) Authorizable(io.cdap.cdap.proto.security.Authorizable) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) DraftId(io.cdap.cdap.datapipeline.draft.DraftId) Test(org.junit.Test)

Example 8 with NamespaceSummary

use of io.cdap.cdap.api.NamespaceSummary in project cdap by cdapio.

the class DraftServiceTest method testListDraftFilter.

private void testListDraftFilter(DraftType draftType) throws IOException, TimeoutException, InterruptedException, ExecutionException {
    // Create 2 drafts per namespace, each belonging to a different user
    NamespaceSummary namespace = new NamespaceSummary(NamespaceId.DEFAULT.getNamespace(), "", 0);
    List<DraftId> draftsToCleanUp = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        String id = String.format("draft-%d", i);
        String name = String.format("draft-name-%d", i);
        String owner = String.format("draft-user-%d", i);
        DraftId draftId = new DraftId(namespace, id, owner);
        draftsToCleanUp.add(draftId);
        if (draftType == DraftType.Batch) {
            createBatchPipelineDraft(draftId, name, "This is a test pipeline.");
        } else {
            createStreamingPipelineDraft(draftId, name, "This is a test pipeline.");
        }
    }
    // Test getting drafts for the default namespace
    List<Draft> drafts = listDrafts(NamespaceId.DEFAULT.getNamespace(), false, "", "", "");
    Assert.assertEquals(4, drafts.size());
    drafts.forEach(draft -> Assert.assertNull(draft.getConfig()));
    validateMetric(4, Constants.Metrics.DRAFT_COUNT);
    // Check that the default sorting is correct
    String[] expectedOrder = new String[] { "draft-name-0", "draft-name-1", "draft-name-2", "draft-name-3" };
    Assert.assertArrayEquals(drafts.stream().map(DraftStoreRequest::getName).toArray(), expectedOrder);
    // Test getting drafts for the default namespace and sorting on id column with descending order
    drafts = listDrafts(NamespaceId.DEFAULT.getNamespace(), false, "id", "DESC", "");
    Assert.assertEquals(4, drafts.size());
    String[] expectedReverseOrder = new String[] { "draft-3", "draft-2", "draft-1", "draft-0" };
    Assert.assertArrayEquals(drafts.stream().map(Draft::getId).toArray(), expectedReverseOrder);
    // Test filtering on draft name using a filter with 0 matches
    drafts = listDrafts(NamespaceId.DEFAULT.getNamespace(), false, "", "", "nomatches");
    Assert.assertEquals(drafts.size(), 0);
    // Test filtering on draft name using a filter with 1 match and include the config
    Draft specialDraft;
    DraftId specialDraftId = new DraftId(namespace, "newDraft", "");
    draftsToCleanUp.add(specialDraftId);
    if (draftType == DraftType.Batch) {
        specialDraft = createBatchPipelineDraft(specialDraftId, "SpecialDraft", "This is a special pipeline.");
    } else {
        specialDraft = createStreamingPipelineDraft(specialDraftId, "SpecialDraft", "This is a special pipeline.");
    }
    drafts = listDrafts(NamespaceId.DEFAULT.getNamespace(), true, "", "", "spe");
    Assert.assertEquals(drafts.size(), 1);
    Assert.assertTrue(// This confirms that the config was included
    sameDraft(drafts.get(0), specialDraft));
    // List with all options enabled
    drafts = listDrafts(NamespaceId.DEFAULT.getNamespace(), true, "id", "DESC", "draft");
    // Confirm special draft is not present
    Assert.assertEquals(drafts.size(), 4);
    drafts.forEach(// Confirm that config was included
    draft -> Assert.assertNotNull(draft.getConfig()));
    Assert.assertArrayEquals(drafts.stream().map(Draft::getId).toArray(), // Confirm sorting
    expectedReverseOrder);
    // Cleanup
    for (DraftId draftId : draftsToCleanUp) {
        deleteDraftAndCheck(draftId);
    }
}
Also used : Draft(io.cdap.cdap.datapipeline.draft.Draft) DraftStoreRequest(io.cdap.cdap.datapipeline.draft.DraftStoreRequest) ArrayList(java.util.ArrayList) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) DraftId(io.cdap.cdap.datapipeline.draft.DraftId)

Example 9 with NamespaceSummary

use of io.cdap.cdap.api.NamespaceSummary in project cdap by cdapio.

the class ConnectionStoreTest method testNotFound.

@Test
public void testNotFound() throws Exception {
    ConnectionId connectionId = new ConnectionId(new NamespaceSummary("default", "", 0L), "nonexisting");
    // get non-existing connection
    try {
        connectionStore.getConnection(connectionId);
        Assert.fail();
    } catch (ConnectionNotFoundException e) {
    // expected
    }
    try {
        connectionStore.deleteConnection(connectionId);
        Assert.fail();
    } catch (ConnectionNotFoundException e) {
    // expected
    }
    // put a connection in the store
    NamespaceSummary oldNamespace = new NamespaceSummary("default", "", 0L);
    ConnectionId id = new ConnectionId(oldNamespace, "myconn");
    Connection connection = new Connection("myconn", "GCS", "GCS connection", false, false, 0L, 0L, new PluginInfo("GCS", "connector", "Google Cloud Platform", ImmutableMap.of("project", "abc"), new ArtifactSelectorConfig("SYSTEM", "google-cloud", "1.0.0")));
    connectionStore.saveConnection(id, connection, false);
    // get the connection with a namespace with a higher generation id should fail
    try {
        connectionStore.getConnection(new ConnectionId(new NamespaceSummary("default", "", 1L), "myconn"));
        Assert.fail();
    } catch (ConnectionNotFoundException e) {
    // expected
    }
}
Also used : ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) ConnectionNotFoundException(io.cdap.cdap.etl.proto.connection.ConnectionNotFoundException) ArtifactSelectorConfig(io.cdap.cdap.etl.proto.ArtifactSelectorConfig) Connection(io.cdap.cdap.etl.proto.connection.Connection) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) PluginInfo(io.cdap.cdap.etl.proto.connection.PluginInfo) Test(org.junit.Test)

Example 10 with NamespaceSummary

use of io.cdap.cdap.api.NamespaceSummary in project cdap by caskdata.

the class StudioService method createPreconfiguredConnections.

private void createPreconfiguredConnections(SystemServiceContext context) throws IOException {
    ConnectionStore connectionStore = new ConnectionStore(context);
    for (PreconfiguredConnectionCreationRequest creationRequest : connectionConfig.getConnections()) {
        if (creationRequest.getName() == null || creationRequest.getNamespace() == null) {
            continue;
        }
        NamespaceSummary namespaceSummary = context.getAdmin().getNamespaceSummary(creationRequest.getNamespace());
        if (namespaceSummary == null) {
            LOG.warn("Namespace {} does not exist, skipping creating connection {}", creationRequest.getNamespace(), creationRequest.getName());
        }
        ConnectionId connectionId = new ConnectionId(namespaceSummary, creationRequest.getName());
        long now = System.currentTimeMillis();
        Connection connectionInfo = new Connection(creationRequest.getName(), connectionId.getConnectionId(), creationRequest.getPlugin().getName(), creationRequest.getDescription(), true, creationRequest.getName().equals(connectionConfig.getDefaultConnection()) ? true : false, now, now, creationRequest.getPlugin());
        try {
            connectionStore.saveConnection(connectionId, connectionInfo, false);
        } catch (ConnectionConflictException e) {
        // expected if the connection is already created
        }
    }
}
Also used : ConnectionConflictException(io.cdap.cdap.etl.proto.connection.ConnectionConflictException) ConnectionId(io.cdap.cdap.etl.proto.connection.ConnectionId) Connection(io.cdap.cdap.etl.proto.connection.Connection) NamespaceSummary(io.cdap.cdap.api.NamespaceSummary) ConnectionStore(io.cdap.cdap.datapipeline.connection.ConnectionStore) PreconfiguredConnectionCreationRequest(io.cdap.cdap.etl.proto.connection.PreconfiguredConnectionCreationRequest)

Aggregations

NamespaceSummary (io.cdap.cdap.api.NamespaceSummary)20 Test (org.junit.Test)14 DraftId (io.cdap.cdap.datapipeline.draft.DraftId)10 Connection (io.cdap.cdap.etl.proto.connection.Connection)8 ConnectionId (io.cdap.cdap.etl.proto.connection.ConnectionId)8 HttpResponse (io.cdap.common.http.HttpResponse)8 ArtifactSelectorConfig (io.cdap.cdap.etl.proto.ArtifactSelectorConfig)6 PluginInfo (io.cdap.cdap.etl.proto.connection.PluginInfo)6 Draft (io.cdap.cdap.datapipeline.draft.Draft)4 ConnectionConflictException (io.cdap.cdap.etl.proto.connection.ConnectionConflictException)4 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)2 FileSet (io.cdap.cdap.api.dataset.lib.FileSet)2 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)2 Get (io.cdap.cdap.api.dataset.table.Get)2 Put (io.cdap.cdap.api.dataset.table.Put)2 Table (io.cdap.cdap.api.dataset.table.Table)2 ConnectionStore (io.cdap.cdap.datapipeline.connection.ConnectionStore)2 DraftNotFoundException (io.cdap.cdap.datapipeline.draft.DraftNotFoundException)2 DraftStoreRequest (io.cdap.cdap.datapipeline.draft.DraftStoreRequest)2 ConnectionNotFoundException (io.cdap.cdap.etl.proto.connection.ConnectionNotFoundException)2