Skip to content

Commit

Permalink
Merge pull request #738 from atlanhq/DVX-526
Browse files Browse the repository at this point in the history
Adds find by ID for workflows
  • Loading branch information
cmgrote authored Jul 17, 2024
2 parents 9bc425f + c89e610 commit eeb69a0
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* SPDX-License-Identifier: Apache-2.0
Copyright 2022 Atlan Pte. Ltd. */
package com.atlan.java.sdk;

import static org.testng.Assert.*;

import com.atlan.exception.AtlanException;
import com.atlan.model.enums.AtlanPackageType;
import com.atlan.model.workflow.WorkflowSearchRequest;
import com.atlan.model.workflow.WorkflowSearchResult;
import com.atlan.model.workflow.WorkflowSearchResultDetail;
import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.Test;

/**
* Tests most aspects of workflows (packages).
*/
@Slf4j
public class WorkflowTest extends AtlanLiveTest {

private static String workflowName;
private static String workflowRun;
private static WorkflowSearchResultDetail wfl;
private static WorkflowSearchResult run;

@Test(groups = {"pkg.search.type"})
void findByType() throws AtlanException {
List<WorkflowSearchResult> results = WorkflowSearchRequest.findByType(AtlanPackageType.SNOWFLAKE, 5);
assertNotNull(results);
assertFalse(results.isEmpty());
wfl = results.get(0).get_source();
workflowName = results.get(0).get_id();
}

@Test(
groups = {"pkg.search.latest"},
dependsOnGroups = {"pkg.search.type"})
void findLatestRun() throws AtlanException {
run = WorkflowSearchRequest.findLatestRun(workflowName);
assertNotNull(run);
workflowRun = run.get_id();
}

@Test(
groups = {"pkg.search.run"},
dependsOnGroups = {"pkg.search.latest"})
void findRunByName() throws AtlanException {
WorkflowSearchResult result = WorkflowSearchRequest.findRunByName(workflowRun);
assertNotNull(result);
assertEquals(result, run);
}

@Test(
groups = {"pkg.search.id"},
dependsOnGroups = {"pkg.search.run"})
void findById() throws AtlanException {
WorkflowSearchResult result = WorkflowSearchRequest.findById(workflowName);
assertNotNull(result);
assertEquals(result.get_source(), wfl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class WorkflowSearchRequest extends IndexSearchDSL {
* Run the search.
*
* @return results from running the search
* @throws AtlanException on any API communication issue
*/
public WorkflowSearchResponse search() throws AtlanException {
return search(Atlan.getDefaultClient());
Expand All @@ -42,6 +43,7 @@ public WorkflowSearchResponse search() throws AtlanException {
*
* @param client connectivity to the Atlan tenant on which to run the search
* @return results from running the search
* @throws AtlanException on any API communication issue
*/
public WorkflowSearchResponse search(AtlanClient client) throws AtlanException {
return client.workflows.searchRuns(this);
Expand All @@ -52,6 +54,7 @@ public WorkflowSearchResponse search(AtlanClient client) throws AtlanException {
*
* @param workflowName name of the workflow for which to find the latest run
* @return the singular result giving the latest run of the workflow
* @throws AtlanException on any API communication issue
*/
public static WorkflowSearchResult findLatestRun(String workflowName) throws AtlanException {
return findLatestRun(Atlan.getDefaultClient(), workflowName);
Expand All @@ -63,6 +66,7 @@ public static WorkflowSearchResult findLatestRun(String workflowName) throws Atl
* @param client connectivity to the Atlan tenant on which to find the latest run of the workflow
* @param workflowName name of the workflow for which to find the latest run
* @return the singular result giving the latest run of the workflow
* @throws AtlanException on any API communication issue
*/
public static WorkflowSearchResult findLatestRun(AtlanClient client, String workflowName) throws AtlanException {

Expand Down Expand Up @@ -101,6 +105,7 @@ public static WorkflowSearchResult findLatestRun(AtlanClient client, String work
* @param client connectivity to the Atlan tenant on which to find the current run of the workflow
* @param workflowName name of the workflow for which to find the current run
* @return the singular result giving the latest currently-running run of the workflow, or null if it is not currently running
* @throws AtlanException on any API communication issue
*/
public static WorkflowSearchResult findCurrentRun(AtlanClient client, String workflowName) throws AtlanException {

Expand Down Expand Up @@ -143,6 +148,7 @@ public static WorkflowSearchResult findCurrentRun(AtlanClient client, String wor
*
* @param workflowRunName name of the specific workflow run to find
* @return the singular result giving the specific run of the workflow
* @throws AtlanException on any API communication issue
*/
public static WorkflowSearchResult findRunByName(String workflowRunName) throws AtlanException {
return findRunByName(Atlan.getDefaultClient(), workflowRunName);
Expand All @@ -154,6 +160,7 @@ public static WorkflowSearchResult findRunByName(String workflowRunName) throws
* @param client connectivity to the Atlan tenant on which to find a specific run of the workflow
* @param workflowRunName name of the specific workflow run to find
* @return the singular result giving the specific run of the workflow
* @throws AtlanException on any API communication issue
*/
public static WorkflowSearchResult findRunByName(AtlanClient client, String workflowRunName) throws AtlanException {
SortOptions sort = SortOptions.of(s -> s.field(FieldSort.of(f -> f.field("metadata.creationTimestamp")
Expand Down Expand Up @@ -190,6 +197,7 @@ public static WorkflowSearchResult findRunByName(AtlanClient client, String work
* @param prefix of the workflow, from a package class (for example {@link com.atlan.model.packages.ConnectionDelete#PREFIX}
* @param maxResults the maximum number of results to retrieve
* @return the list of workflows of the provided type, with the most-recently created first
* @throws AtlanException on any API communication issue
* @deprecated see {@link #findByType(AtlanPackageType, int)} for the replacement
*/
@Deprecated
Expand All @@ -204,6 +212,7 @@ public static List<WorkflowSearchResult> findByType(String prefix, int maxResult
* @param prefix of the workflow, from a package class (for example {@link com.atlan.model.packages.ConnectionDelete#PREFIX}
* @param maxResults the maximum number of results to retrieve
* @return the list of workflows of the provided type, with the most-recently created first
* @throws AtlanException on any API communication issue
* @deprecated see {@link #findByType(AtlanClient, AtlanPackageType, int)} for the replacement
*/
@Deprecated
Expand All @@ -218,6 +227,7 @@ public static List<WorkflowSearchResult> findByType(AtlanClient client, String p
* @param type of the workflow
* @param maxResults the maximum number of results to retrieve
* @return the list of workflows of the provided type, with the most-recently created first
* @throws AtlanException on any API communication issue
*/
public static List<WorkflowSearchResult> findByType(AtlanPackageType type, int maxResults) throws AtlanException {
return findByType(Atlan.getDefaultClient(), type, maxResults);
Expand All @@ -230,19 +240,58 @@ public static List<WorkflowSearchResult> findByType(AtlanPackageType type, int m
* @param type of the workflow
* @param maxResults the maximum number of results to retrieve
* @return the list of workflows of the provided type, with the most-recently created first
* @throws AtlanException on any API communication issue
*/
public static List<WorkflowSearchResult> findByType(AtlanClient client, AtlanPackageType type, int maxResults)
throws AtlanException {
return findByPrefix(client, type.getValue(), maxResults);
}

/**
* Find a workflow based on is ID (e.g. {@code atlan-snowflake-miner-1714638976}).
* Note: only workflows that have been run will be found.
*
* @param id unique identifier of the specific workflow to find
* @return singular result containing the workflow, or null if not found
* @throws AtlanException on any API communication issue
*/
public static WorkflowSearchResult findById(String id) throws AtlanException {
return findById(Atlan.getDefaultClient(), id);
}

/**
* Find a workflow based on is ID (e.g. {@code atlan-snowflake-miner-1714638976}).
* Note: only workflows that have been run will be found.
*
* @param client connectivity to the Atlan tenant on which to find the workflow
* @param id unique identifier of the specific workflow to find
* @return singular result containing the workflow, or null if not found
* @throws AtlanException on any API communication issue
*/
public static WorkflowSearchResult findById(AtlanClient client, String id) throws AtlanException {
Query term =
TermQuery.of(t -> t.field("metadata.name.keyword").value(id))._toQuery();
Query nested = NestedQuery.of(n -> n.path("metadata").query(term))._toQuery();
Query query = BoolQuery.of(b -> b.filter(nested))._toQuery();
WorkflowSearchRequest request =
WorkflowSearchRequest.builder().from(0).size(2).query(query).build();
WorkflowSearchResponse response = client.workflows.search(request);
if (response != null
&& response.getHits() != null
&& !response.getHits().getHits().isEmpty()) {
return response.getHits().getHits().get(0);
}
return null;
}

/**
* Find workflows based on their type.
*
* @param client connectivity to the Atlan tenant on which to find the workflows
* @param prefix of the workflow
* @param maxResults the maximum number of results to retrieve
* @return the list of workflows of the provided type, with the most-recently created first
* @throws AtlanException on any API communication issue
*/
private static List<WorkflowSearchResult> findByPrefix(AtlanClient client, String prefix, int maxResults)
throws AtlanException {
Expand Down

0 comments on commit eeb69a0

Please sign in to comment.