turcsanyip commented on code in PR #6504: URL: https://github.com/apache/nifi/pull/6504#discussion_r1035214769
########## nifi-nar-bundles/nifi-asana-bundle/nifi-asana-services-api/src/main/java/org/apache/nifi/controller/asana/AsanaClient.java: ########## @@ -0,0 +1,195 @@ +/* + * 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.nifi.controller.asana; + +import com.asana.models.Attachment; +import com.asana.models.Project; +import com.asana.models.ProjectMembership; +import com.asana.models.ProjectStatus; +import com.asana.models.Section; +import com.asana.models.Story; +import com.asana.models.Tag; +import com.asana.models.Task; +import com.asana.models.Team; +import com.asana.models.User; + +import java.util.Map; + +/** + * This interface represents a client to Asana REST server, with some basic filtering options built in. + */ +public interface AsanaClient { + /** + * Find & retrieve an Asana project by its name. If multiple projects match, returns the first. + * If there is no match, then {@link RuntimeException} is thrown. Note that constant ordering Review Comment: ```suggestion * If there is no match, then {@link AsanaClientException} is thrown. Note that constant ordering ``` Also in the javadoc of `getTeamByName()` and `getSectionByName()`. ########## nifi-nar-bundles/nifi-asana-bundle/nifi-asana-services-api-nar/src/main/resources/META-INF/NOTICE: ########## @@ -0,0 +1,108 @@ +nifi-asana-services-api-nar +Copyright 2015-2022 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +=========================================== +Apache Software License v2 +=========================================== + +The following binary components are provided under the Apache Software License v2 + + (ASLv2) Apache Commons IO + The following NOTICE information applies: + Apache Commons IO + Copyright 2002-2022 The Apache Software Foundation Review Comment: Thanks for updating the `NOTICE` file, overall it looks good! `Commons IO` is not present in the services-api-nar so it should be removed. I forgot to mention earlier but the entries need to be added in the assembly nar as well (`nifi-assembly/NOTICE`). This "global" NOTICE file is kind of a union of all entries in the nar. Could you please copy those items to that NOTICE file which are not present there yet? ########## nifi-nar-bundles/nifi-asana-bundle/nifi-asana-processors/src/main/java/org/apache/nifi/processors/asana/GetAsanaObject.java: ########## @@ -0,0 +1,393 @@ +/* + * 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.nifi.processors.asana; + +import static java.lang.String.format; +import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.joining; +import static org.apache.commons.collections4.ListUtils.partition; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_EVENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_MEMBERS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_UPDATES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_STORIES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASKS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASK_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TEAM_MEMBERS; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.http.entity.ContentType; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.PrimaryNodeOnly; +import org.apache.nifi.annotation.behavior.TriggerSerially; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.controller.asana.AsanaClient; +import org.apache.nifi.controller.asana.AsanaClientProviderService; +import org.apache.nifi.distributed.cache.client.DistributedMapCacheClient; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.flowfile.attributes.CoreAttributes; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.processors.asana.utils.AsanaObject; +import org.apache.nifi.processors.asana.utils.AsanaObjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaObjectState; +import org.apache.nifi.processors.asana.utils.AsanaProjectEventFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectMembershipFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusFetcher; +import org.apache.nifi.processors.asana.utils.AsanaStoryFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTagFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamMemberFetcher; +import org.apache.nifi.processors.asana.utils.AsanaUserFetcher; +import org.apache.nifi.reporting.InitializationException; + +@TriggerSerially +@PrimaryNodeOnly +@InputRequirement(InputRequirement.Requirement.INPUT_FORBIDDEN) +@WritesAttribute(attribute = GetAsanaObject.ASANA_GID, description = "Global ID of the object in Asana.") +@Tags({"asana", "source", "ingest"}) +@CapabilityDescription("This processor collects data from Asana") +public class GetAsanaObject extends AbstractProcessor { + + protected static final String ASANA_GID = "asana.gid"; + protected static final String ASANA_CLIENT_SERVICE = "asana-controller-service"; + protected static final String DISTRIBUTED_CACHE_SERVICE = "distributed-cache-service"; + protected static final String ASANA_OBJECT_TYPE = "asana-object-type"; + protected static final String ASANA_PROJECT_NAME = "asana-project-name"; + protected static final String ASANA_SECTION_NAME = "asana-section-name"; + protected static final String ASANA_TAG_NAME = "asana-tag-name"; + protected static final String ASANA_TEAM_NAME = "asana-team-name"; + protected static final String ASANA_OUTPUT_BATCH_SIZE = "asana-output-batch-size"; + protected static final String REL_NAME_NEW = "new"; + protected static final String REL_NAME_UPDATED = "updated"; + protected static final String REL_NAME_REMOVED = "removed"; + + protected static final PropertyDescriptor PROP_ASANA_CLIENT_SERVICE = new PropertyDescriptor.Builder() + .name(ASANA_CLIENT_SERVICE) + .displayName("Asana Client Service") + .description("Specify which controller service to use for accessing Asana.") + .required(true) + .identifiesControllerService(AsanaClientProviderService.class) + .build(); + + protected static final PropertyDescriptor PROP_DISTRIBUTED_CACHE_SERVICE = new Builder() + .name(DISTRIBUTED_CACHE_SERVICE) + .displayName("Distributed Cache Service") + .description("Cache service to store fetched item fingerprints. These, from the last successful query" + + " are stored, in order to enable incremental loading and change detection.") + .required(true) + .identifiesControllerService(DistributedMapCacheClient.class) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_OBJECT_TYPE = new PropertyDescriptor.Builder() + .name(ASANA_OBJECT_TYPE) + .displayName("Object Type") + .description("Specify what kind of objects to be collected from Asana") + .required(true) + .allowableValues(AsanaObjectType.class) + .defaultValue(AV_COLLECT_TASKS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_PROJECT = new PropertyDescriptor.Builder() + .name(ASANA_PROJECT_NAME) + .displayName("Project Name") + .description("Fetch only objects in this project. Case sensitive.") + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn( + PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_MEMBERS.getValue(), + AV_COLLECT_STORIES.getValue(), + AV_COLLECT_PROJECT_STATUS_UPDATES.getValue(), + AV_COLLECT_PROJECT_STATUS_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_EVENTS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_SECTION = new PropertyDescriptor.Builder() + .name(ASANA_SECTION_NAME) + .displayName("Section Name") + .description("Fetch only objects in this section. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_STORIES.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_TAG = new PropertyDescriptor.Builder() + .name(ASANA_TAG_NAME) + .displayName("Tag") + .description("Fetch only objects having this tag. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_STORIES.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_TEAM_NAME = new PropertyDescriptor.Builder() + .name(ASANA_TEAM_NAME) + .displayName("Team") + .description("Team name. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, AV_COLLECT_TEAM_MEMBERS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_OUTPUT_BATCH_SIZE = new PropertyDescriptor.Builder() + .name(ASANA_OUTPUT_BATCH_SIZE) + .displayName("Output Batch Size") + .description("The number of items batched together in a single Flow File. If set to 1 (default), then each item is" + + " transferred in a separate Flow File and each will have an asana.gid attribute, to help identifying" + + " the fetched item on the server side, if needed. If the batch size is greater than 1, then the" + + " specified amount of items are batched together in a single Flow File as a Json array, and the" + + " Flow Files won't have the asana.gid attribute.") + .defaultValue("1") + .required(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .build(); + + protected static final List<PropertyDescriptor> DESCRIPTORS = Collections.unmodifiableList(Arrays.asList( + PROP_ASANA_CLIENT_SERVICE, + PROP_DISTRIBUTED_CACHE_SERVICE, + PROP_ASANA_OBJECT_TYPE, + PROP_ASANA_PROJECT, + PROP_ASANA_SECTION, + PROP_ASANA_TEAM_NAME, + PROP_ASANA_TAG, + PROP_ASANA_OUTPUT_BATCH_SIZE + )); + + protected static final Relationship REL_NEW = new Relationship.Builder() + .name(REL_NAME_NEW) + .description("Newly collected objects are routed to this relationship.") + .build(); + + protected static final Relationship REL_UPDATED = new Relationship.Builder() + .name(REL_NAME_UPDATED) + .description("Objects that have already been collected earlier, but were updated since, are routed to this relationship.") + .build(); + + protected static final Relationship REL_REMOVED = new Relationship.Builder() + .name(REL_NAME_REMOVED) + .description("Notification about deleted objects are routed to this relationship. " + + "Flow files will not have any payload. IDs of the resources no longer exist " + + "are carried by the asana.gid attribute of the generated FlowFiles.") + .build(); + + protected static final Set<Relationship> RELATIONSHIPS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + REL_NEW, + REL_UPDATED, + REL_REMOVED + ))); + + private volatile AsanaObjectFetcher objectFetcher; + private volatile Integer batchSize; + + @Override + public Set<Relationship> getRelationships() { + return RELATIONSHIPS; + } + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return DESCRIPTORS; + } + + @OnScheduled + public void onScheduled(final ProcessContext context) throws InitializationException { + AsanaClientProviderService controllerService = context.getProperty(PROP_ASANA_CLIENT_SERVICE).asControllerService(AsanaClientProviderService.class); + AsanaClient client = controllerService.createClient(); + batchSize = context.getProperty(PROP_ASANA_OUTPUT_BATCH_SIZE).asInteger(); + + try { + getLogger().debug("Initializing object fetcher..."); + objectFetcher = createObjectFetcher(context, client); + } catch (Exception e) { + throw new InitializationException(e); + } + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + try { + Map<String, String> state = recoverState(context).orElse(Collections.emptyMap()); + getLogger().debug("Attempting to load state: {}", state); + objectFetcher.loadState(state); + } catch (Exception e) { + getLogger().info("Failed to recover state. Falling back to clean start."); + objectFetcher.clearState(); + } + getLogger().debug("Initial state: {}", objectFetcher.saveState()); Review Comment: I would put it into `if (getLogger().isDebugEnabled()) { ... }` in order to prevent the unnecessary map conversions in `objectFetcher.saveState()`. ########## nifi-nar-bundles/nifi-asana-bundle/nifi-asana-processors/src/main/java/org/apache/nifi/processors/asana/GetAsanaObject.java: ########## @@ -0,0 +1,393 @@ +/* + * 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.nifi.processors.asana; + +import static java.lang.String.format; +import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.joining; +import static org.apache.commons.collections4.ListUtils.partition; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_EVENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_MEMBERS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_UPDATES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_STORIES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASKS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASK_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TEAM_MEMBERS; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.http.entity.ContentType; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.PrimaryNodeOnly; +import org.apache.nifi.annotation.behavior.TriggerSerially; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.controller.asana.AsanaClient; +import org.apache.nifi.controller.asana.AsanaClientProviderService; +import org.apache.nifi.distributed.cache.client.DistributedMapCacheClient; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.flowfile.attributes.CoreAttributes; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.processors.asana.utils.AsanaObject; +import org.apache.nifi.processors.asana.utils.AsanaObjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaObjectState; +import org.apache.nifi.processors.asana.utils.AsanaProjectEventFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectMembershipFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusFetcher; +import org.apache.nifi.processors.asana.utils.AsanaStoryFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTagFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamMemberFetcher; +import org.apache.nifi.processors.asana.utils.AsanaUserFetcher; +import org.apache.nifi.reporting.InitializationException; + +@TriggerSerially +@PrimaryNodeOnly +@InputRequirement(InputRequirement.Requirement.INPUT_FORBIDDEN) +@WritesAttribute(attribute = GetAsanaObject.ASANA_GID, description = "Global ID of the object in Asana.") +@Tags({"asana", "source", "ingest"}) +@CapabilityDescription("This processor collects data from Asana") +public class GetAsanaObject extends AbstractProcessor { + + protected static final String ASANA_GID = "asana.gid"; + protected static final String ASANA_CLIENT_SERVICE = "asana-controller-service"; + protected static final String DISTRIBUTED_CACHE_SERVICE = "distributed-cache-service"; + protected static final String ASANA_OBJECT_TYPE = "asana-object-type"; + protected static final String ASANA_PROJECT_NAME = "asana-project-name"; + protected static final String ASANA_SECTION_NAME = "asana-section-name"; + protected static final String ASANA_TAG_NAME = "asana-tag-name"; + protected static final String ASANA_TEAM_NAME = "asana-team-name"; + protected static final String ASANA_OUTPUT_BATCH_SIZE = "asana-output-batch-size"; + protected static final String REL_NAME_NEW = "new"; + protected static final String REL_NAME_UPDATED = "updated"; + protected static final String REL_NAME_REMOVED = "removed"; + + protected static final PropertyDescriptor PROP_ASANA_CLIENT_SERVICE = new PropertyDescriptor.Builder() + .name(ASANA_CLIENT_SERVICE) + .displayName("Asana Client Service") + .description("Specify which controller service to use for accessing Asana.") + .required(true) + .identifiesControllerService(AsanaClientProviderService.class) + .build(); + + protected static final PropertyDescriptor PROP_DISTRIBUTED_CACHE_SERVICE = new Builder() + .name(DISTRIBUTED_CACHE_SERVICE) + .displayName("Distributed Cache Service") + .description("Cache service to store fetched item fingerprints. These, from the last successful query" + + " are stored, in order to enable incremental loading and change detection.") + .required(true) + .identifiesControllerService(DistributedMapCacheClient.class) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_OBJECT_TYPE = new PropertyDescriptor.Builder() + .name(ASANA_OBJECT_TYPE) + .displayName("Object Type") + .description("Specify what kind of objects to be collected from Asana") + .required(true) + .allowableValues(AsanaObjectType.class) + .defaultValue(AV_COLLECT_TASKS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_PROJECT = new PropertyDescriptor.Builder() + .name(ASANA_PROJECT_NAME) + .displayName("Project Name") + .description("Fetch only objects in this project. Case sensitive.") + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn( + PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_MEMBERS.getValue(), + AV_COLLECT_STORIES.getValue(), + AV_COLLECT_PROJECT_STATUS_UPDATES.getValue(), + AV_COLLECT_PROJECT_STATUS_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_EVENTS.getValue()) Review Comment: Since [NIFI-10775](https://issues.apache.org/jira/browse/NIFI-10775) (merged recently), `getValue()` is not needed. `DescribedValue` can be used directly. ```suggestion .dependsOn( PROP_ASANA_OBJECT_TYPE, AV_COLLECT_TASKS, AV_COLLECT_TASK_ATTACHMENTS, AV_COLLECT_PROJECT_MEMBERS, AV_COLLECT_STORIES, AV_COLLECT_PROJECT_STATUS_UPDATES, AV_COLLECT_PROJECT_STATUS_ATTACHMENTS, AV_COLLECT_PROJECT_EVENTS) ``` ########## nifi-nar-bundles/nifi-asana-bundle/nifi-asana-processors/src/main/java/org/apache/nifi/processors/asana/GetAsanaObject.java: ########## @@ -0,0 +1,393 @@ +/* + * 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.nifi.processors.asana; + +import static java.lang.String.format; +import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.joining; +import static org.apache.commons.collections4.ListUtils.partition; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_EVENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_MEMBERS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_UPDATES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_STORIES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASKS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASK_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TEAM_MEMBERS; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.http.entity.ContentType; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.PrimaryNodeOnly; +import org.apache.nifi.annotation.behavior.TriggerSerially; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.controller.asana.AsanaClient; +import org.apache.nifi.controller.asana.AsanaClientProviderService; +import org.apache.nifi.distributed.cache.client.DistributedMapCacheClient; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.flowfile.attributes.CoreAttributes; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.processors.asana.utils.AsanaObject; +import org.apache.nifi.processors.asana.utils.AsanaObjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaObjectState; +import org.apache.nifi.processors.asana.utils.AsanaProjectEventFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectMembershipFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusFetcher; +import org.apache.nifi.processors.asana.utils.AsanaStoryFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTagFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamMemberFetcher; +import org.apache.nifi.processors.asana.utils.AsanaUserFetcher; +import org.apache.nifi.reporting.InitializationException; + +@TriggerSerially +@PrimaryNodeOnly +@InputRequirement(InputRequirement.Requirement.INPUT_FORBIDDEN) +@WritesAttribute(attribute = GetAsanaObject.ASANA_GID, description = "Global ID of the object in Asana.") +@Tags({"asana", "source", "ingest"}) +@CapabilityDescription("This processor collects data from Asana") +public class GetAsanaObject extends AbstractProcessor { + + protected static final String ASANA_GID = "asana.gid"; + protected static final String ASANA_CLIENT_SERVICE = "asana-controller-service"; + protected static final String DISTRIBUTED_CACHE_SERVICE = "distributed-cache-service"; + protected static final String ASANA_OBJECT_TYPE = "asana-object-type"; + protected static final String ASANA_PROJECT_NAME = "asana-project-name"; + protected static final String ASANA_SECTION_NAME = "asana-section-name"; + protected static final String ASANA_TAG_NAME = "asana-tag-name"; + protected static final String ASANA_TEAM_NAME = "asana-team-name"; + protected static final String ASANA_OUTPUT_BATCH_SIZE = "asana-output-batch-size"; + protected static final String REL_NAME_NEW = "new"; + protected static final String REL_NAME_UPDATED = "updated"; + protected static final String REL_NAME_REMOVED = "removed"; + + protected static final PropertyDescriptor PROP_ASANA_CLIENT_SERVICE = new PropertyDescriptor.Builder() + .name(ASANA_CLIENT_SERVICE) + .displayName("Asana Client Service") + .description("Specify which controller service to use for accessing Asana.") + .required(true) + .identifiesControllerService(AsanaClientProviderService.class) + .build(); + + protected static final PropertyDescriptor PROP_DISTRIBUTED_CACHE_SERVICE = new Builder() + .name(DISTRIBUTED_CACHE_SERVICE) + .displayName("Distributed Cache Service") + .description("Cache service to store fetched item fingerprints. These, from the last successful query" + + " are stored, in order to enable incremental loading and change detection.") + .required(true) + .identifiesControllerService(DistributedMapCacheClient.class) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_OBJECT_TYPE = new PropertyDescriptor.Builder() + .name(ASANA_OBJECT_TYPE) + .displayName("Object Type") + .description("Specify what kind of objects to be collected from Asana") + .required(true) + .allowableValues(AsanaObjectType.class) + .defaultValue(AV_COLLECT_TASKS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_PROJECT = new PropertyDescriptor.Builder() + .name(ASANA_PROJECT_NAME) + .displayName("Project Name") + .description("Fetch only objects in this project. Case sensitive.") + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn( + PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_MEMBERS.getValue(), + AV_COLLECT_STORIES.getValue(), + AV_COLLECT_PROJECT_STATUS_UPDATES.getValue(), + AV_COLLECT_PROJECT_STATUS_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_EVENTS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_SECTION = new PropertyDescriptor.Builder() + .name(ASANA_SECTION_NAME) + .displayName("Section Name") + .description("Fetch only objects in this section. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_STORIES.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_TAG = new PropertyDescriptor.Builder() + .name(ASANA_TAG_NAME) + .displayName("Tag") + .description("Fetch only objects having this tag. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_STORIES.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_TEAM_NAME = new PropertyDescriptor.Builder() + .name(ASANA_TEAM_NAME) + .displayName("Team") + .description("Team name. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, AV_COLLECT_TEAM_MEMBERS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_OUTPUT_BATCH_SIZE = new PropertyDescriptor.Builder() + .name(ASANA_OUTPUT_BATCH_SIZE) + .displayName("Output Batch Size") + .description("The number of items batched together in a single Flow File. If set to 1 (default), then each item is" + + " transferred in a separate Flow File and each will have an asana.gid attribute, to help identifying" + + " the fetched item on the server side, if needed. If the batch size is greater than 1, then the" + + " specified amount of items are batched together in a single Flow File as a Json array, and the" + + " Flow Files won't have the asana.gid attribute.") + .defaultValue("1") + .required(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .build(); + + protected static final List<PropertyDescriptor> DESCRIPTORS = Collections.unmodifiableList(Arrays.asList( + PROP_ASANA_CLIENT_SERVICE, + PROP_DISTRIBUTED_CACHE_SERVICE, + PROP_ASANA_OBJECT_TYPE, + PROP_ASANA_PROJECT, + PROP_ASANA_SECTION, + PROP_ASANA_TEAM_NAME, + PROP_ASANA_TAG, + PROP_ASANA_OUTPUT_BATCH_SIZE + )); + + protected static final Relationship REL_NEW = new Relationship.Builder() + .name(REL_NAME_NEW) + .description("Newly collected objects are routed to this relationship.") + .build(); + + protected static final Relationship REL_UPDATED = new Relationship.Builder() + .name(REL_NAME_UPDATED) + .description("Objects that have already been collected earlier, but were updated since, are routed to this relationship.") + .build(); + + protected static final Relationship REL_REMOVED = new Relationship.Builder() + .name(REL_NAME_REMOVED) + .description("Notification about deleted objects are routed to this relationship. " + + "Flow files will not have any payload. IDs of the resources no longer exist " + + "are carried by the asana.gid attribute of the generated FlowFiles.") + .build(); + + protected static final Set<Relationship> RELATIONSHIPS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + REL_NEW, + REL_UPDATED, + REL_REMOVED + ))); + + private volatile AsanaObjectFetcher objectFetcher; + private volatile Integer batchSize; + + @Override + public Set<Relationship> getRelationships() { + return RELATIONSHIPS; + } + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return DESCRIPTORS; + } + + @OnScheduled + public void onScheduled(final ProcessContext context) throws InitializationException { + AsanaClientProviderService controllerService = context.getProperty(PROP_ASANA_CLIENT_SERVICE).asControllerService(AsanaClientProviderService.class); + AsanaClient client = controllerService.createClient(); + batchSize = context.getProperty(PROP_ASANA_OUTPUT_BATCH_SIZE).asInteger(); + + try { + getLogger().debug("Initializing object fetcher..."); + objectFetcher = createObjectFetcher(context, client); + } catch (Exception e) { + throw new InitializationException(e); + } + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + try { + Map<String, String> state = recoverState(context).orElse(Collections.emptyMap()); + getLogger().debug("Attempting to load state: {}", state); + objectFetcher.loadState(state); + } catch (Exception e) { + getLogger().info("Failed to recover state. Falling back to clean start."); + objectFetcher.clearState(); + } + getLogger().debug("Initial state: {}", objectFetcher.saveState()); + + Collection<FlowFile> newItems = new ArrayList<>(); + Collection<FlowFile> updatedItems = new ArrayList<>(); + Collection<FlowFile> removedItems = new ArrayList<>(); + Map<AsanaObjectState, Collection<FlowFile>> flowFiles = new HashMap<>(); + flowFiles.put(AsanaObjectState.NEW, newItems); + flowFiles.put(AsanaObjectState.UPDATED, updatedItems); + flowFiles.put(AsanaObjectState.REMOVED, removedItems); + + List<AsanaObject> allObjects = new ArrayList<>(); + + AsanaObject nextObject; + while ((nextObject = objectFetcher.fetchNext()) != null) { + allObjects.add(nextObject); + } + + Map<AsanaObjectState, List<AsanaObject>> allObjectsByState = allObjects.stream() + .collect(groupingBy(AsanaObject::getState)); + + if (batchSize == 1) { + allObjectsByState + .forEach((asanaObjectState, asanaObjects) -> asanaObjects.forEach( + asanaObject -> { + final Map<String, String> attributes = new HashMap<>(2); + attributes.put(CoreAttributes.MIME_TYPE.key(), ContentType.APPLICATION_JSON.getMimeType()); + attributes.put(ASANA_GID, asanaObject.getGid()); + FlowFile flowFile = createFlowFileWithStringPayload(session, asanaObject.getContent()); + flowFile = session.putAllAttributes(flowFile, attributes); + flowFiles.get(asanaObject.getState()).add(flowFile); + } + )); + } else { + allObjectsByState + .forEach((asanaObjectState, asanaObjects) -> partition(asanaObjects, batchSize).forEach( + asanaObjectsInPartition -> { + FlowFile flowFile = createFlowFileWithStringPayload(session, format("[%s]", + asanaObjectsInPartition.stream().map(AsanaObject::getContent) + .collect(joining(",")))); + flowFile = session.putAllAttributes(flowFile, + singletonMap(CoreAttributes.MIME_TYPE.key(), + ContentType.APPLICATION_JSON.getMimeType())); + flowFiles.get(asanaObjectState).add(flowFile); + } + )); + } + + if (flowFiles.values().stream().allMatch(Collection::isEmpty)) { + context.yield(); + getLogger().debug("Yielding, as there are no new FlowFiles."); + } else { + session.transfer(newItems, REL_NEW); + session.transfer(updatedItems, REL_UPDATED); + session.transfer(removedItems, REL_REMOVED); + } + + Map<String, String> state = objectFetcher.saveState(); + persistState(state, context); + + getLogger().debug( + "New state after transferring {} new, {} updated, and {} removed items: {}", + newItems.size(), updatedItems.size(), removedItems.size(), state); + + session.commitAsync(); Review Comment: Unlike session state, the MapCache operations are not part of the session's transaction. For this reason, the session should be committed first, in order to guarantee at-least-once delivery. ########## nifi-nar-bundles/nifi-asana-bundle/nifi-asana-processors/src/main/java/org/apache/nifi/processors/asana/GetAsanaObject.java: ########## @@ -0,0 +1,393 @@ +/* + * 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.nifi.processors.asana; + +import static java.lang.String.format; +import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.joining; +import static org.apache.commons.collections4.ListUtils.partition; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_EVENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_MEMBERS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_UPDATES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_STORIES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASKS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASK_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TEAM_MEMBERS; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.http.entity.ContentType; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.PrimaryNodeOnly; +import org.apache.nifi.annotation.behavior.TriggerSerially; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.controller.asana.AsanaClient; +import org.apache.nifi.controller.asana.AsanaClientProviderService; +import org.apache.nifi.distributed.cache.client.DistributedMapCacheClient; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.flowfile.attributes.CoreAttributes; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.processors.asana.utils.AsanaObject; +import org.apache.nifi.processors.asana.utils.AsanaObjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaObjectState; +import org.apache.nifi.processors.asana.utils.AsanaProjectEventFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectMembershipFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusFetcher; +import org.apache.nifi.processors.asana.utils.AsanaStoryFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTagFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamMemberFetcher; +import org.apache.nifi.processors.asana.utils.AsanaUserFetcher; +import org.apache.nifi.reporting.InitializationException; + +@TriggerSerially +@PrimaryNodeOnly +@InputRequirement(InputRequirement.Requirement.INPUT_FORBIDDEN) +@WritesAttribute(attribute = GetAsanaObject.ASANA_GID, description = "Global ID of the object in Asana.") +@Tags({"asana", "source", "ingest"}) +@CapabilityDescription("This processor collects data from Asana") +public class GetAsanaObject extends AbstractProcessor { + + protected static final String ASANA_GID = "asana.gid"; + protected static final String ASANA_CLIENT_SERVICE = "asana-controller-service"; + protected static final String DISTRIBUTED_CACHE_SERVICE = "distributed-cache-service"; + protected static final String ASANA_OBJECT_TYPE = "asana-object-type"; + protected static final String ASANA_PROJECT_NAME = "asana-project-name"; + protected static final String ASANA_SECTION_NAME = "asana-section-name"; + protected static final String ASANA_TAG_NAME = "asana-tag-name"; + protected static final String ASANA_TEAM_NAME = "asana-team-name"; + protected static final String ASANA_OUTPUT_BATCH_SIZE = "asana-output-batch-size"; + protected static final String REL_NAME_NEW = "new"; + protected static final String REL_NAME_UPDATED = "updated"; + protected static final String REL_NAME_REMOVED = "removed"; + + protected static final PropertyDescriptor PROP_ASANA_CLIENT_SERVICE = new PropertyDescriptor.Builder() + .name(ASANA_CLIENT_SERVICE) + .displayName("Asana Client Service") + .description("Specify which controller service to use for accessing Asana.") + .required(true) + .identifiesControllerService(AsanaClientProviderService.class) + .build(); + + protected static final PropertyDescriptor PROP_DISTRIBUTED_CACHE_SERVICE = new Builder() + .name(DISTRIBUTED_CACHE_SERVICE) + .displayName("Distributed Cache Service") + .description("Cache service to store fetched item fingerprints. These, from the last successful query" + + " are stored, in order to enable incremental loading and change detection.") + .required(true) + .identifiesControllerService(DistributedMapCacheClient.class) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_OBJECT_TYPE = new PropertyDescriptor.Builder() + .name(ASANA_OBJECT_TYPE) + .displayName("Object Type") + .description("Specify what kind of objects to be collected from Asana") + .required(true) + .allowableValues(AsanaObjectType.class) + .defaultValue(AV_COLLECT_TASKS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_PROJECT = new PropertyDescriptor.Builder() + .name(ASANA_PROJECT_NAME) + .displayName("Project Name") + .description("Fetch only objects in this project. Case sensitive.") + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn( + PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_MEMBERS.getValue(), + AV_COLLECT_STORIES.getValue(), + AV_COLLECT_PROJECT_STATUS_UPDATES.getValue(), + AV_COLLECT_PROJECT_STATUS_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_EVENTS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_SECTION = new PropertyDescriptor.Builder() + .name(ASANA_SECTION_NAME) + .displayName("Section Name") + .description("Fetch only objects in this section. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_STORIES.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_TAG = new PropertyDescriptor.Builder() + .name(ASANA_TAG_NAME) + .displayName("Tag") + .description("Fetch only objects having this tag. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_STORIES.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_TEAM_NAME = new PropertyDescriptor.Builder() + .name(ASANA_TEAM_NAME) + .displayName("Team") + .description("Team name. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, AV_COLLECT_TEAM_MEMBERS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_OUTPUT_BATCH_SIZE = new PropertyDescriptor.Builder() + .name(ASANA_OUTPUT_BATCH_SIZE) + .displayName("Output Batch Size") + .description("The number of items batched together in a single Flow File. If set to 1 (default), then each item is" + + " transferred in a separate Flow File and each will have an asana.gid attribute, to help identifying" + + " the fetched item on the server side, if needed. If the batch size is greater than 1, then the" + + " specified amount of items are batched together in a single Flow File as a Json array, and the" + + " Flow Files won't have the asana.gid attribute.") + .defaultValue("1") + .required(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .build(); + + protected static final List<PropertyDescriptor> DESCRIPTORS = Collections.unmodifiableList(Arrays.asList( + PROP_ASANA_CLIENT_SERVICE, + PROP_DISTRIBUTED_CACHE_SERVICE, + PROP_ASANA_OBJECT_TYPE, + PROP_ASANA_PROJECT, + PROP_ASANA_SECTION, + PROP_ASANA_TEAM_NAME, + PROP_ASANA_TAG, + PROP_ASANA_OUTPUT_BATCH_SIZE + )); + + protected static final Relationship REL_NEW = new Relationship.Builder() + .name(REL_NAME_NEW) + .description("Newly collected objects are routed to this relationship.") + .build(); + + protected static final Relationship REL_UPDATED = new Relationship.Builder() + .name(REL_NAME_UPDATED) + .description("Objects that have already been collected earlier, but were updated since, are routed to this relationship.") + .build(); + + protected static final Relationship REL_REMOVED = new Relationship.Builder() + .name(REL_NAME_REMOVED) + .description("Notification about deleted objects are routed to this relationship. " + + "Flow files will not have any payload. IDs of the resources no longer exist " + + "are carried by the asana.gid attribute of the generated FlowFiles.") + .build(); + + protected static final Set<Relationship> RELATIONSHIPS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + REL_NEW, + REL_UPDATED, + REL_REMOVED + ))); + + private volatile AsanaObjectFetcher objectFetcher; + private volatile Integer batchSize; + + @Override + public Set<Relationship> getRelationships() { + return RELATIONSHIPS; + } + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return DESCRIPTORS; + } + + @OnScheduled + public void onScheduled(final ProcessContext context) throws InitializationException { + AsanaClientProviderService controllerService = context.getProperty(PROP_ASANA_CLIENT_SERVICE).asControllerService(AsanaClientProviderService.class); + AsanaClient client = controllerService.createClient(); + batchSize = context.getProperty(PROP_ASANA_OUTPUT_BATCH_SIZE).asInteger(); + + try { + getLogger().debug("Initializing object fetcher..."); + objectFetcher = createObjectFetcher(context, client); + } catch (Exception e) { + throw new InitializationException(e); + } + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + try { + Map<String, String> state = recoverState(context).orElse(Collections.emptyMap()); + getLogger().debug("Attempting to load state: {}", state); + objectFetcher.loadState(state); + } catch (Exception e) { + getLogger().info("Failed to recover state. Falling back to clean start."); + objectFetcher.clearState(); + } + getLogger().debug("Initial state: {}", objectFetcher.saveState()); + + Collection<FlowFile> newItems = new ArrayList<>(); + Collection<FlowFile> updatedItems = new ArrayList<>(); + Collection<FlowFile> removedItems = new ArrayList<>(); + Map<AsanaObjectState, Collection<FlowFile>> flowFiles = new HashMap<>(); + flowFiles.put(AsanaObjectState.NEW, newItems); + flowFiles.put(AsanaObjectState.UPDATED, updatedItems); + flowFiles.put(AsanaObjectState.REMOVED, removedItems); + + List<AsanaObject> allObjects = new ArrayList<>(); + + AsanaObject nextObject; + while ((nextObject = objectFetcher.fetchNext()) != null) { + allObjects.add(nextObject); + } + + Map<AsanaObjectState, List<AsanaObject>> allObjectsByState = allObjects.stream() + .collect(groupingBy(AsanaObject::getState)); + + if (batchSize == 1) { + allObjectsByState + .forEach((asanaObjectState, asanaObjects) -> asanaObjects.forEach( + asanaObject -> { + final Map<String, String> attributes = new HashMap<>(2); + attributes.put(CoreAttributes.MIME_TYPE.key(), ContentType.APPLICATION_JSON.getMimeType()); + attributes.put(ASANA_GID, asanaObject.getGid()); + FlowFile flowFile = createFlowFileWithStringPayload(session, asanaObject.getContent()); + flowFile = session.putAllAttributes(flowFile, attributes); + flowFiles.get(asanaObject.getState()).add(flowFile); + } + )); + } else { + allObjectsByState + .forEach((asanaObjectState, asanaObjects) -> partition(asanaObjects, batchSize).forEach( + asanaObjectsInPartition -> { + FlowFile flowFile = createFlowFileWithStringPayload(session, format("[%s]", + asanaObjectsInPartition.stream().map(AsanaObject::getContent) + .collect(joining(",")))); + flowFile = session.putAllAttributes(flowFile, + singletonMap(CoreAttributes.MIME_TYPE.key(), + ContentType.APPLICATION_JSON.getMimeType())); + flowFiles.get(asanaObjectState).add(flowFile); + } + )); + } + + if (flowFiles.values().stream().allMatch(Collection::isEmpty)) { + context.yield(); + getLogger().debug("Yielding, as there are no new FlowFiles."); + } else { + session.transfer(newItems, REL_NEW); + session.transfer(updatedItems, REL_UPDATED); + session.transfer(removedItems, REL_REMOVED); + } + + Map<String, String> state = objectFetcher.saveState(); + persistState(state, context); + + getLogger().debug( + "New state after transferring {} new, {} updated, and {} removed items: {}", + newItems.size(), updatedItems.size(), removedItems.size(), state); + + session.commitAsync(); + } + + protected AsanaObjectFetcher createObjectFetcher(final ProcessContext context, AsanaClient client) { + final String objectType = context.getProperty(PROP_ASANA_OBJECT_TYPE).getValue(); + final String projectName = context.getProperty(PROP_ASANA_PROJECT).getValue(); + final String sectionName = context.getProperty(PROP_ASANA_SECTION).getValue(); + final String teamName = context.getProperty(PROP_ASANA_TEAM_NAME).getValue(); + final String tagName = context.getProperty(PROP_ASANA_TAG).getValue(); + + switch (AsanaObjectType.fromValue(objectType)) { + case AV_COLLECT_TASKS: + return new AsanaTaskFetcher(client, projectName, sectionName, tagName); + case AV_COLLECT_PROJECTS: + return new AsanaProjectFetcher(client); + case AV_COLLECT_PROJECT_EVENTS: + return new AsanaProjectEventFetcher(client, projectName); + case AV_COLLECT_PROJECT_MEMBERS: + return new AsanaProjectMembershipFetcher(client, projectName); + case AV_COLLECT_PROJECT_STATUS_ATTACHMENTS: + return new AsanaProjectStatusAttachmentFetcher(client, projectName); + case AV_COLLECT_PROJECT_STATUS_UPDATES: + return new AsanaProjectStatusFetcher(client, projectName); + case AV_COLLECT_STORIES: + return new AsanaStoryFetcher(client, projectName, sectionName, tagName); + case AV_COLLECT_TAGS: + return new AsanaTagFetcher(client); + case AV_COLLECT_TASK_ATTACHMENTS: + return new AsanaTaskAttachmentFetcher(client, projectName, sectionName, tagName); + case AV_COLLECT_TEAMS: + return new AsanaTeamFetcher(client); + case AV_COLLECT_TEAM_MEMBERS: + return new AsanaTeamMemberFetcher(client, teamName); + case AV_COLLECT_USERS: + return new AsanaUserFetcher(client); + } + + throw new ProcessException("Cannot fetch objects of type: " + objectType); + } + + private Optional<Map<String, String>> recoverState(final ProcessContext context) { + final DistributedMapCacheClient client = getDistributedMapCacheClient(context); + try { + final Map<String, String> result = client.get(getIdentifier(), new GenericObjectSerDe<>(), new GenericObjectSerDe<>()); Review Comment: The same instance could be used for the key/value serializers. Also, it could be a singleton stored in a static field. ########## nifi-nar-bundles/nifi-asana-bundle/nifi-asana-processors/src/test/java/org/apache/nifi/processors/asana/AsanaTaskFetcherTest.java: ########## @@ -0,0 +1,436 @@ +/* + * 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.nifi.processors.asana; + +import com.asana.models.Project; +import com.asana.models.Section; +import com.asana.models.Tag; +import com.asana.models.Task; +import com.google.api.client.util.DateTime; +import org.apache.groovy.util.Maps; +import org.apache.nifi.controller.asana.AsanaClient; +import org.apache.nifi.processors.asana.utils.AsanaObject; +import org.apache.nifi.processors.asana.utils.AsanaObjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaObjectState; +import org.apache.nifi.processors.asana.utils.AsanaTaskFetcher; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +public class AsanaTaskFetcherTest { + + @Mock + private AsanaClient client; + private Project project; + private Section section; + private Tag tag; + + @BeforeEach + public void init() { + project = new Project(); + project.gid = "123"; + project.modifiedAt = new DateTime(123456789); + project.name = "My Project"; + + when(client.getProjectByName(project.name)).thenReturn(project); + + section = new Section(); + section.gid = "456"; + section.project = project; + section.name = "Some section"; + section.createdAt = new DateTime(123456789); + + when(client.getSections(project)).thenReturn(singletonMap(section.gid, section)); + when(client.getSectionByName(project, section.name)).thenReturn(section); + + tag = new Tag(); + tag.gid = "9876"; + tag.name = "Foo"; + tag.createdAt = new DateTime(123456789); + + when(client.getTags()).thenReturn(singletonMap(tag.gid, tag)); + } + + @Test + public void testNoObjectsFetchedWhenNoTasksReturned() { + when(client.getTasks(any(Project.class))).thenReturn(emptyMap()); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, null, null); + assertNull(fetcher.fetchNext()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, times(1)).getTasks(project); + verifyNoMoreInteractions(client); + } + + @Test + public void testNoObjectsFetchedWhenNoTasksReturnedBySection() { + when(client.getTasks(any(Section.class))).thenReturn(emptyMap()); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, section.name, null); + assertNull(fetcher.fetchNext()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, atLeastOnce()).getSectionByName(project, section.name); + verify(client, times(1)).getTasks(section); + verifyNoMoreInteractions(client); + } + + @Test + public void testNoObjectsFetchedWhenNoTasksReturnedByTag() { + when(client.getTasks(any(Project.class))).thenReturn(emptyMap()); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, null, tag.name); + assertNull(fetcher.fetchNext()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, atLeastOnce()).getTags(); + verify(client, times(1)).getTasks(project); + verify(client, times(1)).getTasks(tag); + verifyNoMoreInteractions(client); + } + + @Test + public void testSingleTaskFetched() { + final Task task = new Task(); + task.gid = "1234"; + task.name = "My first task"; + task.modifiedAt = new DateTime(123456789); + + when(client.getTasks(any(Project.class))).thenReturn(singletonMap(task.gid, task)); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, null, null); + final AsanaObject object = fetcher.fetchNext(); + + assertEquals(AsanaObjectState.NEW, object.getState()); + assertEquals(task.gid, object.getGid()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, times(1)).getTasks(project); + verifyNoMoreInteractions(client); + } + + @Test + public void testSingleTaskFetchedBySection() { + final Task task = new Task(); + task.gid = "1234"; + task.name = "My first task"; + task.modifiedAt = new DateTime(123456789); + + when(client.getTasks(any(Section.class))).thenReturn(singletonMap(task.gid, task)); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, section.name, null); + final AsanaObject object = fetcher.fetchNext(); + + assertEquals(AsanaObjectState.NEW, object.getState()); + assertEquals(task.gid, object.getGid()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, atLeastOnce()).getSectionByName(project, section.name); + verify(client, times(1)).getTasks(section); + verifyNoMoreInteractions(client); + } + + @Test + public void testSingleTaskFetchedByTag() { + final Task task = new Task(); + task.gid = "1234"; + task.name = "My first task"; + task.modifiedAt = new DateTime(123456789); + + when(client.getTasks(any(Project.class))).thenReturn(singletonMap(task.gid, task)); + when(client.getTasks(any(Tag.class))).thenReturn(singletonMap(task.gid, task)); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, null, tag.name); + final AsanaObject object = fetcher.fetchNext(); + + assertEquals(AsanaObjectState.NEW, object.getState()); + assertEquals(task.gid, object.getGid()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, atLeastOnce()).getTags(); + verify(client, times(1)).getTasks(project); + verify(client, times(1)).getTasks(tag); + verifyNoMoreInteractions(client); + } + + @Test + public void testNoTaskFetchedByNonMatchingTag() { + final Task task1 = new Task(); + task1.gid = "1234"; + task1.name = "My first task"; + task1.modifiedAt = new DateTime(123456789); + + final Task task2 = new Task(); + task2.gid = "5678"; + task2.name = "My other task"; + task2.modifiedAt = new DateTime(123456789); + + when(client.getTasks(any(Project.class))).thenReturn(singletonMap(task1.gid, task1)); + when(client.getTasks(any(Tag.class))).thenReturn(singletonMap(task2.gid, task2)); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, null, tag.name); + assertNull(fetcher.fetchNext()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, atLeastOnce()).getTags(); + verify(client, times(1)).getTasks(project); + verify(client, times(1)).getTasks(tag); + verifyNoMoreInteractions(client); + } + + @Test + public void testTaskRemovedFromSection() { + final Task task = new Task(); + task.gid = "1234"; + task.name = "My first task"; + task.modifiedAt = new DateTime(123456789); + + when(client.getTasks(any(Section.class))).thenReturn(singletonMap(task.gid, task)); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, section.name, null); + assertNotNull(fetcher.fetchNext()); + + when(client.getTasks(any(Section.class))).thenReturn(emptyMap()); + + final AsanaObject object = fetcher.fetchNext(); + assertEquals(AsanaObjectState.REMOVED, object.getState()); + assertEquals(task.gid, object.getGid()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, atLeastOnce()).getSectionByName(project, section.name); + verify(client, times(2)).getTasks(section); + verifyNoMoreInteractions(client); + } + + @Test + public void testTaskUntagged() { + final Task task = new Task(); + task.gid = "1234"; + task.name = "My first task"; + task.modifiedAt = new DateTime(123456789); + + when(client.getTasks(any(Project.class))).thenReturn(singletonMap(task.gid, task)); + when(client.getTasks(any(Tag.class))).thenReturn(singletonMap(task.gid, task)); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, null, tag.name); + assertNotNull(fetcher.fetchNext()); + + when(client.getTasks(any(Tag.class))).thenReturn(emptyMap()); + + final AsanaObject object = fetcher.fetchNext(); + assertEquals(AsanaObjectState.REMOVED, object.getState()); + assertEquals(task.gid, object.getGid()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, atLeastOnce()).getTags(); + verify(client, times(2)).getTasks(project); + verify(client, times(2)).getTasks(tag); + verifyNoMoreInteractions(client); + } + + @Test + public void testCollectMultipleTasksWithSameTagAndFilterOutDuplicates() { + final Tag anotherTagWithSameName = new Tag(); + anotherTagWithSameName.gid = "555"; + anotherTagWithSameName.name = tag.name; + + when(client.getTags()).thenReturn(Maps.of(tag.gid, tag, anotherTagWithSameName.gid, anotherTagWithSameName)); + + final Task task1 = new Task(); + task1.gid = "1234"; + task1.name = "My first task"; + task1.modifiedAt = new DateTime(123456789); + + final Task task2 = new Task(); + task2.gid = "1212"; + task2.name = "My other task"; + task2.modifiedAt = new DateTime(234567891); + + final Task task3 = new Task(); + task3.gid = "333"; + task3.name = "My third task"; + task3.modifiedAt = new DateTime(345678912); + + final Task task4 = new Task(); + task4.gid = "444"; + task4.name = "A task without tag"; + task4.modifiedAt = new DateTime(456789123); + + when(client.getTasks(any(Project.class))).thenReturn(Maps.of(task1.gid, task1, task2.gid, task2, task3.gid, task3, task4.gid, task4)); + when(client.getTasks(tag)).thenReturn(singletonMap(task1.gid, task1)); + when(client.getTasks(anotherTagWithSameName)).thenReturn(Maps.of(task1.gid, task1, task2.gid, task2, task3.gid, task3)); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, null, tag.name); + + final AsanaObject object1 = fetcher.fetchNext(); + assertEquals(AsanaObjectState.NEW, object1.getState()); + + final AsanaObject object2 = fetcher.fetchNext(); + assertEquals(AsanaObjectState.NEW, object2.getState()); + assertNotEquals(object1, object2); + + final AsanaObject object3 = fetcher.fetchNext(); + assertEquals(AsanaObjectState.NEW, object3.getState()); + assertNotEquals(object1, object3); + assertNotEquals(object2, object3); + + assertNull(fetcher.fetchNext()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, atLeastOnce()).getTags(); + verify(client, times(2)).getTasks(project); + verify(client, times(2)).getTasks(tag); + verify(client, times(2)).getTasks(anotherTagWithSameName); + verifyNoMoreInteractions(client); + } + + @Test + public void testTaskUpdatedOnlyWhenModificationDateChanges() { + final Task task = new Task(); + task.gid = "1234"; + task.name = "My first task"; + task.modifiedAt = new DateTime(123456789); + + when(client.getTasks(any(Project.class))).thenReturn(singletonMap(task.gid, task)); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, null, null); + assertNotNull(fetcher.fetchNext()); + assertNull(fetcher.fetchNext()); + + task.name = "Update my task"; + assertNull(fetcher.fetchNext()); + + task.modifiedAt = new DateTime(234567891); + final AsanaObject object = fetcher.fetchNext(); + + assertEquals(AsanaObjectState.UPDATED, object.getState()); + assertEquals(task.gid, object.getGid()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, times(4)).getTasks(project); + verifyNoMoreInteractions(client); + } + + @Test + public void testRestoreStateAndContinue() { + final Task task = new Task(); + task.gid = "1234"; + task.name = "My first task"; + task.modifiedAt = new DateTime(123456789); + + when(client.getTasks(any(Project.class))).thenReturn(singletonMap(task.gid, task)); + + final AsanaObjectFetcher fetcher1 = new AsanaTaskFetcher(client, project.name, null, null); + assertNotNull(fetcher1.fetchNext()); + + final AsanaObjectFetcher fetcher2 = new AsanaTaskFetcher(client, project.name, null, null); + fetcher2.loadState(fetcher1.saveState()); + + task.modifiedAt = new DateTime(234567891); + final AsanaObject object = fetcher2.fetchNext(); + + assertEquals(AsanaObjectState.UPDATED, object.getState()); + assertEquals(task.gid, object.getGid()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, times(2)).getTasks(project); + verifyNoMoreInteractions(client); + } + + @Test + public void testClearState() { + final Task task = new Task(); + task.gid = "1234"; + task.name = "My first task"; + task.modifiedAt = new DateTime(123456789); + + when(client.getTasks(any(Project.class))).thenReturn(singletonMap(task.gid, task)); + + final AsanaObjectFetcher fetcher = new AsanaTaskFetcher(client, project.name, null, null); + assertNotNull(fetcher.fetchNext()); + + fetcher.clearState(); + + task.modifiedAt = new DateTime(234567891); + final AsanaObject object = fetcher.fetchNext(); + + assertEquals(AsanaObjectState.NEW, object.getState()); + assertEquals(task.gid, object.getGid()); + + verify(client, atLeastOnce()).getProjectByName(project.name); + verify(client, times(2)).getTasks(project); + verifyNoMoreInteractions(client); + } + + @Test + public void testWrongStateForConfigurationThrows() { + final Project otherProject = new Project(); + otherProject.gid = "999"; + otherProject.name = "Other Project"; + + final Section otherSection = new Section(); + otherSection.gid = "888"; + otherSection.name = "Other Section"; + + final Tag otherTag = new Tag(); + otherTag.gid = "777"; + otherTag.name = "Other Tag"; + + when(client.getProjectByName(otherProject.name)).thenReturn(otherProject); + when(client.getSectionByName(project, otherSection.name)).thenReturn(otherSection); + + final AsanaObjectFetcher fetcher1 = new AsanaTaskFetcher(client, project.name, null, null); + final AsanaObjectFetcher fetcher2 = new AsanaTaskFetcher(client, otherProject.name, null, null); + assertThrows(RuntimeException.class, () -> fetcher2.loadState(fetcher1.saveState())); Review Comment: Minor: `AsanaObjectFetcherException.class` could be used in the tests (or the given specific exception class). ########## nifi-nar-bundles/nifi-asana-bundle/nifi-asana-processors/src/main/java/org/apache/nifi/processors/asana/GetAsanaObject.java: ########## @@ -0,0 +1,393 @@ +/* + * 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.nifi.processors.asana; + +import static java.lang.String.format; +import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.joining; +import static org.apache.commons.collections4.ListUtils.partition; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_EVENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_MEMBERS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_PROJECT_STATUS_UPDATES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_STORIES; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASKS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TASK_ATTACHMENTS; +import static org.apache.nifi.processors.asana.AsanaObjectType.AV_COLLECT_TEAM_MEMBERS; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.http.entity.ContentType; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.PrimaryNodeOnly; +import org.apache.nifi.annotation.behavior.TriggerSerially; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyDescriptor.Builder; +import org.apache.nifi.controller.asana.AsanaClient; +import org.apache.nifi.controller.asana.AsanaClientProviderService; +import org.apache.nifi.distributed.cache.client.DistributedMapCacheClient; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.flowfile.attributes.CoreAttributes; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.processors.asana.utils.AsanaObject; +import org.apache.nifi.processors.asana.utils.AsanaObjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaObjectState; +import org.apache.nifi.processors.asana.utils.AsanaProjectEventFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectMembershipFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaProjectStatusFetcher; +import org.apache.nifi.processors.asana.utils.AsanaStoryFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTagFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskAttachmentFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTaskFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamFetcher; +import org.apache.nifi.processors.asana.utils.AsanaTeamMemberFetcher; +import org.apache.nifi.processors.asana.utils.AsanaUserFetcher; +import org.apache.nifi.reporting.InitializationException; + +@TriggerSerially +@PrimaryNodeOnly +@InputRequirement(InputRequirement.Requirement.INPUT_FORBIDDEN) +@WritesAttribute(attribute = GetAsanaObject.ASANA_GID, description = "Global ID of the object in Asana.") +@Tags({"asana", "source", "ingest"}) +@CapabilityDescription("This processor collects data from Asana") +public class GetAsanaObject extends AbstractProcessor { + + protected static final String ASANA_GID = "asana.gid"; + protected static final String ASANA_CLIENT_SERVICE = "asana-controller-service"; + protected static final String DISTRIBUTED_CACHE_SERVICE = "distributed-cache-service"; + protected static final String ASANA_OBJECT_TYPE = "asana-object-type"; + protected static final String ASANA_PROJECT_NAME = "asana-project-name"; + protected static final String ASANA_SECTION_NAME = "asana-section-name"; + protected static final String ASANA_TAG_NAME = "asana-tag-name"; + protected static final String ASANA_TEAM_NAME = "asana-team-name"; + protected static final String ASANA_OUTPUT_BATCH_SIZE = "asana-output-batch-size"; + protected static final String REL_NAME_NEW = "new"; + protected static final String REL_NAME_UPDATED = "updated"; + protected static final String REL_NAME_REMOVED = "removed"; + + protected static final PropertyDescriptor PROP_ASANA_CLIENT_SERVICE = new PropertyDescriptor.Builder() + .name(ASANA_CLIENT_SERVICE) + .displayName("Asana Client Service") + .description("Specify which controller service to use for accessing Asana.") + .required(true) + .identifiesControllerService(AsanaClientProviderService.class) + .build(); + + protected static final PropertyDescriptor PROP_DISTRIBUTED_CACHE_SERVICE = new Builder() + .name(DISTRIBUTED_CACHE_SERVICE) + .displayName("Distributed Cache Service") + .description("Cache service to store fetched item fingerprints. These, from the last successful query" + + " are stored, in order to enable incremental loading and change detection.") + .required(true) + .identifiesControllerService(DistributedMapCacheClient.class) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_OBJECT_TYPE = new PropertyDescriptor.Builder() + .name(ASANA_OBJECT_TYPE) + .displayName("Object Type") + .description("Specify what kind of objects to be collected from Asana") + .required(true) + .allowableValues(AsanaObjectType.class) + .defaultValue(AV_COLLECT_TASKS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_PROJECT = new PropertyDescriptor.Builder() + .name(ASANA_PROJECT_NAME) + .displayName("Project Name") + .description("Fetch only objects in this project. Case sensitive.") + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn( + PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_MEMBERS.getValue(), + AV_COLLECT_STORIES.getValue(), + AV_COLLECT_PROJECT_STATUS_UPDATES.getValue(), + AV_COLLECT_PROJECT_STATUS_ATTACHMENTS.getValue(), + AV_COLLECT_PROJECT_EVENTS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_SECTION = new PropertyDescriptor.Builder() + .name(ASANA_SECTION_NAME) + .displayName("Section Name") + .description("Fetch only objects in this section. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_STORIES.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_TAG = new PropertyDescriptor.Builder() + .name(ASANA_TAG_NAME) + .displayName("Tag") + .description("Fetch only objects having this tag. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, + AV_COLLECT_TASKS.getValue(), + AV_COLLECT_TASK_ATTACHMENTS.getValue(), + AV_COLLECT_STORIES.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_TEAM_NAME = new PropertyDescriptor.Builder() + .name(ASANA_TEAM_NAME) + .displayName("Team") + .description("Team name. Case sensitive.") + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .dependsOn(PROP_ASANA_OBJECT_TYPE, AV_COLLECT_TEAM_MEMBERS.getValue()) + .build(); + + protected static final PropertyDescriptor PROP_ASANA_OUTPUT_BATCH_SIZE = new PropertyDescriptor.Builder() + .name(ASANA_OUTPUT_BATCH_SIZE) + .displayName("Output Batch Size") + .description("The number of items batched together in a single Flow File. If set to 1 (default), then each item is" + + " transferred in a separate Flow File and each will have an asana.gid attribute, to help identifying" + + " the fetched item on the server side, if needed. If the batch size is greater than 1, then the" + + " specified amount of items are batched together in a single Flow File as a Json array, and the" + + " Flow Files won't have the asana.gid attribute.") + .defaultValue("1") + .required(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .build(); + + protected static final List<PropertyDescriptor> DESCRIPTORS = Collections.unmodifiableList(Arrays.asList( + PROP_ASANA_CLIENT_SERVICE, + PROP_DISTRIBUTED_CACHE_SERVICE, + PROP_ASANA_OBJECT_TYPE, + PROP_ASANA_PROJECT, + PROP_ASANA_SECTION, + PROP_ASANA_TEAM_NAME, + PROP_ASANA_TAG, + PROP_ASANA_OUTPUT_BATCH_SIZE + )); + + protected static final Relationship REL_NEW = new Relationship.Builder() + .name(REL_NAME_NEW) + .description("Newly collected objects are routed to this relationship.") + .build(); + + protected static final Relationship REL_UPDATED = new Relationship.Builder() + .name(REL_NAME_UPDATED) + .description("Objects that have already been collected earlier, but were updated since, are routed to this relationship.") + .build(); + + protected static final Relationship REL_REMOVED = new Relationship.Builder() + .name(REL_NAME_REMOVED) + .description("Notification about deleted objects are routed to this relationship. " + + "Flow files will not have any payload. IDs of the resources no longer exist " + + "are carried by the asana.gid attribute of the generated FlowFiles.") + .build(); + + protected static final Set<Relationship> RELATIONSHIPS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( + REL_NEW, + REL_UPDATED, + REL_REMOVED + ))); + + private volatile AsanaObjectFetcher objectFetcher; + private volatile Integer batchSize; + + @Override + public Set<Relationship> getRelationships() { + return RELATIONSHIPS; + } + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return DESCRIPTORS; + } + + @OnScheduled + public void onScheduled(final ProcessContext context) throws InitializationException { + AsanaClientProviderService controllerService = context.getProperty(PROP_ASANA_CLIENT_SERVICE).asControllerService(AsanaClientProviderService.class); + AsanaClient client = controllerService.createClient(); + batchSize = context.getProperty(PROP_ASANA_OUTPUT_BATCH_SIZE).asInteger(); + + try { + getLogger().debug("Initializing object fetcher..."); + objectFetcher = createObjectFetcher(context, client); + } catch (Exception e) { + throw new InitializationException(e); + } + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + try { + Map<String, String> state = recoverState(context).orElse(Collections.emptyMap()); + getLogger().debug("Attempting to load state: {}", state); + objectFetcher.loadState(state); + } catch (Exception e) { + getLogger().info("Failed to recover state. Falling back to clean start."); + objectFetcher.clearState(); + } + getLogger().debug("Initial state: {}", objectFetcher.saveState()); + + Collection<FlowFile> newItems = new ArrayList<>(); + Collection<FlowFile> updatedItems = new ArrayList<>(); + Collection<FlowFile> removedItems = new ArrayList<>(); + Map<AsanaObjectState, Collection<FlowFile>> flowFiles = new HashMap<>(); + flowFiles.put(AsanaObjectState.NEW, newItems); + flowFiles.put(AsanaObjectState.UPDATED, updatedItems); + flowFiles.put(AsanaObjectState.REMOVED, removedItems); + + List<AsanaObject> allObjects = new ArrayList<>(); + + AsanaObject nextObject; + while ((nextObject = objectFetcher.fetchNext()) != null) { + allObjects.add(nextObject); Review Comment: All fetched entities are collected in memory which can lead memory issues. Could not we iterate over the entities and process them on the fly? Like: fetchNext => create FF => transfer In case of `batchSize > 1` we need some buffers though. -- 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]
