This is an automated email from the ASF dual-hosted git repository.

sshenoy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 5b14a34dac HDDS-9080. om roles CLI should provide Json output option 
(#5136)
5b14a34dac is described below

commit 5b14a34dace31efa8d233a369e8e914984ad2edf
Author: Tejaskriya <[email protected]>
AuthorDate: Tue Aug 8 11:00:22 2023 +0530

    HDDS-9080. om roles CLI should provide Json output option (#5136)
---
 .../dist/src/main/smoketest/omha/om-roles.robot    | 30 ++++++++++++++++
 .../ozone/admin/om/GetServiceRolesSubcommand.java  | 42 +++++++++++++++++++---
 2 files changed, 68 insertions(+), 4 deletions(-)

diff --git a/hadoop-ozone/dist/src/main/smoketest/omha/om-roles.robot 
b/hadoop-ozone/dist/src/main/smoketest/omha/om-roles.robot
new file mode 100644
index 0000000000..9c022a5846
--- /dev/null
+++ b/hadoop-ozone/dist/src/main/smoketest/omha/om-roles.robot
@@ -0,0 +1,30 @@
+# 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.
+
+*** Settings ***
+Documentation       Smoke test for listing om roles.
+Resource            ../commonlib.robot
+Test Timeout        5 minutes
+Test Setup          Run Keyword if    '${SECURITY_ENABLED}' == 'true'    Kinit 
test user     testuser     testuser.keytab
+
+*** Test Cases ***
+List om roles
+    ${output} =         Execute          ozone admin om roles 
--service-id=omservice
+                        Should Match Regexp   ${output}  [om (: LEADER|)]
+
+List om roles as JSON
+    ${output} =         Execute          ozone admin om roles 
--service-id=omservice --json
+    ${leader} =         Execute          echo '${output}' | jq -r '.[] | 
select(.serverRole == "LEADER")'
+                        Should Not Be Equal       ${leader}       ${EMPTY}
diff --git 
a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/admin/om/GetServiceRolesSubcommand.java
 
b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/admin/om/GetServiceRolesSubcommand.java
index 482e46306f..67b20a51b2 100644
--- 
a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/admin/om/GetServiceRolesSubcommand.java
+++ 
b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/admin/om/GetServiceRolesSubcommand.java
@@ -20,13 +20,18 @@ package org.apache.hadoop.ozone.admin.om;
 
 import org.apache.hadoop.hdds.cli.HddsVersionProvider;
 import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
+import org.apache.hadoop.hdds.server.JsonUtils;
 import org.apache.hadoop.ozone.client.OzoneClientException;
 import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRoleInfo;
 import org.apache.hadoop.ozone.om.protocol.OzoneManagerProtocol;
 import org.apache.hadoop.ozone.om.helpers.ServiceInfo;
 import picocli.CommandLine;
 
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.Callable;
 
 /**
@@ -47,13 +52,22 @@ public class GetServiceRolesSubcommand implements 
Callable<Void> {
       required = true)
   private String omServiceId;
 
+  @CommandLine.Option(names = { "--json" },
+      defaultValue = "false",
+      description = "Format output as JSON")
+  private boolean json;
+
   private OzoneManagerProtocol ozoneManagerClient;
 
   @Override
   public Void call() throws Exception {
     try {
       ozoneManagerClient =  parent.createOmClient(omServiceId);
-      getOmServerRoles(ozoneManagerClient.getServiceList());
+      if (json) {
+        printOmServerRolesAsJson(ozoneManagerClient.getServiceList());
+      } else {
+        printOmServerRoles(ozoneManagerClient.getServiceList());
+      }
     } catch (OzoneClientException ex) {
       System.out.printf("Error: %s", ex.getMessage());
     } finally {
@@ -64,16 +78,36 @@ public class GetServiceRolesSubcommand implements 
Callable<Void> {
     return null;
   }
 
-  private void getOmServerRoles(List<ServiceInfo> serviceList) {
+  private void printOmServerRoles(List<ServiceInfo> serviceList) {
     for (ServiceInfo serviceInfo : serviceList) {
       OMRoleInfo omRoleInfo = serviceInfo.getOmRoleInfo();
       if (omRoleInfo != null &&
           serviceInfo.getNodeType() == HddsProtos.NodeType.OM) {
         System.out.println(
-            serviceInfo.getOmRoleInfo().getNodeId() + " : " +
-                serviceInfo.getOmRoleInfo().getServerRole() + " (" +
+            omRoleInfo.getNodeId() + " : " +
+                omRoleInfo.getServerRole() + " (" +
                 serviceInfo.getHostname() + ")");
       }
     }
   }
+
+  private void printOmServerRolesAsJson(List<ServiceInfo> serviceList)
+      throws IOException {
+    List<Map<String, Map<String, String>>> omServiceList = new ArrayList<>();
+    for (ServiceInfo serviceInfo : serviceList) {
+      OMRoleInfo omRoleInfo = serviceInfo.getOmRoleInfo();
+      if (omRoleInfo != null &&
+          serviceInfo.getNodeType() == HddsProtos.NodeType.OM) {
+        Map<String, Map<String, String>> omService = new HashMap<>();
+        omService.put(omRoleInfo.getNodeId(),
+            new HashMap<String, String>() {{
+              put("serverRole", omRoleInfo.getServerRole());
+              put("hostname", serviceInfo.getHostname());
+            }});
+        omServiceList.add(omService);
+      }
+    }
+    System.out.print(
+        JsonUtils.toJsonStringWithDefaultPrettyPrinter(omServiceList));
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to