Skip to content

Commit

Permalink
NIFI-11992: Processor and sink service for filing tickets in Zendesk
Browse files Browse the repository at this point in the history
This closes apache#7644.

Signed-off-by: Peter Turcsanyi <[email protected]>
  • Loading branch information
mark-bathori authored and turcsanyip committed Dec 4, 2023
1 parent 3b0d810 commit 7770a17
Show file tree
Hide file tree
Showing 33 changed files with 2,998 additions and 221 deletions.
6 changes: 6 additions & 0 deletions nifi-assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,12 @@ language governing permissions and limitations under the License. -->
<version>2.0.0-SNAPSHOT</version>
<type>nar</type>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-zendesk-services-nar</artifactId>
<version>2.0.0-SNAPSHOT</version>
<type>nar</type>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-dropbox-processors-nar</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-extension-utils</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>nifi-record-path-property</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-record-path</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.record.path.property;

import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.record.path.FieldValue;
import org.apache.nifi.record.path.RecordPath;
import org.apache.nifi.record.path.RecordPathResult;
import org.apache.nifi.serialization.record.DataType;
import org.apache.nifi.serialization.record.Record;
import org.apache.nifi.serialization.record.RecordFieldType;
import org.apache.nifi.serialization.record.type.ChoiceDataType;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.stream.Collectors.toList;

public final class RecordPathPropertyUtil {

private static final String NULL_VALUE = "null";
private static final Pattern RECORD_PATH_PATTERN = Pattern.compile("@\\{(/.*?)\\}");

private RecordPathPropertyUtil() {
}

/**
* Resolves the property value to be handled as a record path or a constant value.
*
* @param propertyValue property value to be resolved
* @param record record to resolve the value from if the property value is a record path
*/
public static String resolvePropertyValue(String propertyValue, Record record) {
if (propertyValue != null && !propertyValue.isBlank()) {
final Matcher matcher = RECORD_PATH_PATTERN.matcher(propertyValue);
if (matcher.matches()) {
return resolveRecordState(matcher.group(1), record);
} else {
return propertyValue;
}
}

return null;
}

private static String resolveRecordState(String pathValue, final Record record) {
final RecordPath recordPath = RecordPath.compile(pathValue);
final RecordPathResult result = recordPath.evaluate(record);
final List<FieldValue> fieldValues = result.getSelectedFields().collect(toList());
final FieldValue fieldValue = getMatchingFieldValue(recordPath, fieldValues);

if (fieldValue.getValue() == null || fieldValue.getValue() == NULL_VALUE) {
return null;
}

return getFieldValue(recordPath, fieldValue);
}

/**
* The method checks if only one result were received for the give record path.
*
* @param recordPath path to the requested field
* @param resultList result list
* @return matching field
*/
private static FieldValue getMatchingFieldValue(final RecordPath recordPath, final List<FieldValue> resultList) {
if (resultList.isEmpty()) {
throw new ProcessException(String.format("Evaluated RecordPath [%s] against Record but got no results", recordPath));
}

if (resultList.size() > 1) {
throw new ProcessException(String.format("Evaluated RecordPath [%s] against Record and received multiple distinct results [%s]", recordPath, resultList));
}

return resultList.get(0);
}

/**
* The method checks the field's type and filters out every non-compatible type.
*
* @param recordPath path to the requested field
* @param fieldValue record field
* @return value of the record field
*/
private static String getFieldValue(final RecordPath recordPath, FieldValue fieldValue) {
final RecordFieldType fieldType = fieldValue.getField().getDataType().getFieldType();

if (fieldType == RecordFieldType.RECORD || fieldType == RecordFieldType.ARRAY || fieldType == RecordFieldType.MAP) {
throw new ProcessException(String.format("The provided RecordPath [%s] points to a [%s] type value", recordPath, fieldType));
}

if (fieldType == RecordFieldType.CHOICE) {
final ChoiceDataType choiceDataType = (ChoiceDataType) fieldValue.getField().getDataType();
final List<DataType> possibleTypes = choiceDataType.getPossibleSubTypes();
if (possibleTypes.stream().anyMatch(type -> type.getFieldType() == RecordFieldType.RECORD)) {
throw new ProcessException(String.format("The provided RecordPath [%s] points to a [CHOICE] type value with Record subtype", recordPath));
}
}

return String.valueOf(fieldValue.getValue());
}
}
1 change: 1 addition & 0 deletions nifi-nar-bundles/nifi-extension-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@
<module>nifi-service-utils</module>
<module>nifi-syslog-utils</module>
<module>nifi-conflict-resolution</module>
<module>nifi-record-path-property</module>
</modules>
</project>
65 changes: 65 additions & 0 deletions nifi-nar-bundles/nifi-zendesk-bundle/nifi-zendesk-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-zendesk-bundle</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>nifi-zendesk-common</artifactId>
<packaging>jar</packaging>

<dependencies>
<!-- Internal dependencies -->
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-utils</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-web-client-provider-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-record-path-property</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>

<!-- External dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-ssl-context-service-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.common.zendesk;

public class ZendeskAuthenticationContext {

private final String subdomain;
private final String user;
private final ZendeskAuthenticationType authenticationType;
private final String authenticationCredentials;

public ZendeskAuthenticationContext(String subdomain, String user, ZendeskAuthenticationType authenticationType, String authenticationCredentials) {
this.subdomain = subdomain;
this.user = user;
this.authenticationType = authenticationType;
this.authenticationCredentials =authenticationCredentials;
}

public String getSubdomain() {
return subdomain;
}

public String getUser() {
return user;
}

public ZendeskAuthenticationType getAuthenticationType() {
return authenticationType;
}

public String getAuthenticationCredentials() {
return authenticationCredentials;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
* limitations under the License.
*/

package org.apache.nifi.processors.zendesk;
package org.apache.nifi.common.zendesk;

import static java.lang.String.format;
import org.apache.nifi.components.DescribedValue;

import java.util.stream.Stream;
import org.apache.nifi.components.DescribedValue;

import static java.lang.String.format;

public enum ZendeskAuthenticationType implements DescribedValue {
PASSWORD("password", "Password",
Expand Down
Loading

0 comments on commit 7770a17

Please sign in to comment.