[
https://issues.apache.org/jira/browse/NIFI-3695?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15971562#comment-15971562
]
ASF GitHub Bot commented on NIFI-3695:
--------------------------------------
Github user brosander commented on a diff in the pull request:
https://github.com/apache/nifi/pull/1669#discussion_r111808071
--- Diff:
nifi-toolkit/nifi-toolkit-admin/src/main/groovy/org/apache/nifi/toolkit/admin/client/NiFiClientUtil.groovy
---
@@ -0,0 +1,156 @@
+/*
+ * 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.toolkit.admin.client
+
+import com.google.common.collect.Lists
+import com.sun.jersey.api.client.Client
+import com.sun.jersey.api.client.ClientResponse
+import com.sun.jersey.api.client.WebResource
+import org.apache.nifi.util.NiFiProperties
+import org.apache.nifi.util.StringUtils
+import org.apache.nifi.web.api.dto.NodeDTO
+import org.apache.nifi.web.api.entity.ClusterEntity
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
+
+public class NiFiClientUtil {
+
+ private static final Logger logger =
LoggerFactory.getLogger(NiFiClientUtil.class)
+ private final static String GET_CLUSTER_ENDPOINT
="/nifi-api/controller/cluster"
+
+ public static Boolean isCluster(final NiFiProperties niFiProperties){
+ String clusterNode =
niFiProperties.getProperty(NiFiProperties.CLUSTER_IS_NODE)
+ return Boolean.valueOf(clusterNode)
+ }
+
+ public static String getUrl(NiFiProperties niFiProperties, String
endpoint){
+
+ final StringBuilder urlBuilder = new StringBuilder();
+
+
if(!StringUtils.isEmpty(niFiProperties.getProperty(NiFiProperties.WEB_HTTPS_PORT))){
+ urlBuilder.append("https://")
+
urlBuilder.append(StringUtils.isEmpty(niFiProperties.getProperty(NiFiProperties.WEB_HTTPS_HOST))
? "localhost": niFiProperties.getProperty(NiFiProperties.WEB_HTTPS_HOST))
+ urlBuilder.append(":")
+
urlBuilder.append(StringUtils.isEmpty(niFiProperties.getProperty(NiFiProperties.WEB_HTTPS_PORT))
? "8081" : niFiProperties.getProperty(NiFiProperties.WEB_HTTPS_PORT))
+ }else{
+ urlBuilder.append("http://")
+
urlBuilder.append(StringUtils.isEmpty(niFiProperties.getProperty(NiFiProperties.WEB_HTTP_HOST))
? "localhost": niFiProperties.getProperty(NiFiProperties.WEB_HTTP_HOST))
+ urlBuilder.append(":")
+
urlBuilder.append(StringUtils.isEmpty(niFiProperties.getProperty(NiFiProperties.WEB_HTTPS_PORT))
? "8080": niFiProperties.getProperty(NiFiProperties.WEB_HTTPS_PORT))
+ }
+
+ if(!StringUtils.isEmpty(endpoint)) {
+ urlBuilder.append(endpoint)
+ }
+
+ urlBuilder.toString()
+ }
+
+ public static String getUrl(NiFiProperties niFiProperties, NodeDTO
nodeDTO, String endpoint){
+
+ final StringBuilder urlBuilder = new StringBuilder();
+
if(!StringUtils.isEmpty(niFiProperties.getProperty(NiFiProperties.WEB_HTTPS_PORT))){
+ urlBuilder.append("https://")
+
+ }else{
+ urlBuilder.append("http://")
+ }
+ urlBuilder.append(nodeDTO.address)
+ urlBuilder.append(":")
+ urlBuilder.append(nodeDTO.apiPort)
+
+ if(!StringUtils.isEmpty(endpoint)) {
+ urlBuilder.append(endpoint)
+ }
+
+ urlBuilder.toString()
+ }
+
+ public static ClusterEntity getCluster(final Client client,
NiFiProperties niFiProperties, List<String> activeUrls){
+
+ if(activeUrls.isEmpty()){
+ final String url = getUrl(niFiProperties,null)
+ activeUrls.add(url)
+ }
+
+ for(String activeUrl: activeUrls) {
+
+ try {
+
+ String url = activeUrl + GET_CLUSTER_ENDPOINT
+ final WebResource webResource = client.resource(url)
+ final ClientResponse response =
webResource.type("application/json").get(ClientResponse.class)
+
+ Integer status = response.getStatus()
+
+ if (status != 200) {
+ if (status == 404) {
+ logger.warn("This node is not attached to a
cluster. Please connect to a node that is attached to the cluster for
information")
+ } else {
+ logger.warn("Failed with HTTP error code: {},
message: {}", status, response.getStatusInfo().getReasonPhrase())
+ }
+ } else if (status == 200) {
+ return response.getEntity(ClusterEntity.class)
+ }
+
+ }catch(Exception ex){
+ logger.warn("Exception occurred during connection attempt:
{}",ex.localizedMessage)
+ }
+
+ }
+
+ throw new RuntimeException("Unable to obtain cluster information")
+
+ }
+
+ public static List<String> getActiveClusterUrls(final Client client,
NiFiProperties niFiProperties){
+
+ final ClusterEntity clusterEntity = getCluster(client,
niFiProperties, Lists.newArrayList())
+ final List<NodeDTO> activeNodes =
clusterEntity.cluster.nodes.findAll{ it.status == "CONNECTED" }
+ final List<String> activeUrls = Lists.newArrayList()
+
+ activeNodes.each {
+ activeUrls.add(getUrl(niFiProperties,it, null))
+ }
+ activeUrls
+ }
+
+ public static String convertToJson(NodeDTO nodeDTO){
--- End diff --
Can we use jackson for this?
> Create Node Manager & Notification Utilities
> --------------------------------------------
>
> Key: NIFI-3695
> URL: https://issues.apache.org/jira/browse/NIFI-3695
> Project: Apache NiFi
> Issue Type: Sub-task
> Components: Tools and Build
> Reporter: Yolanda M. Davis
> Assignee: Yolanda M. Davis
>
> The node manager utility should allow system administrators to connect,
> disconnect or remove a node from a cluster on the command line. If a node is
> not part of a cluster an error message should display if node is not part of
> a cluster. If a node is disconnected from a cluster and needs to be
> connected or removed from that cluster the tool should support receiving a
> list of urls to connected nodes which can be used to send the required
> command to the active cluster.
> The notification utility should allow administrators to send messages as
> bulletins to the NiFi with levels of INFO, WARN or ERROR.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)