Make auth login command parameters as part of cmd 95/24395/9
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Tue, 24 Oct 2017 13:41:35 +0000 (19:11 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Tue, 21 Nov 2017 12:37:45 +0000 (18:07 +0530)
Now auth commands parameters are added into the command
parameters list

Issue-Id: CLI-66

Change-Id: If6d7629b9efeb773f90ba53fb2ed8d6e464f3a73
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java
framework/src/main/java/org/onap/cli/fw/ad/OnapAuthClient.java
framework/src/main/java/org/onap/cli/fw/utils/OnapCommandDiscoveryUtils.java
framework/src/main/java/org/onap/cli/fw/utils/OnapCommandSchemaLoaderUtils.java
framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java
framework/src/test/java/org/onap/cli/fw/ad/OnapAuthClientCommandBasedTest.java
framework/src/test/resources/open-cli-schema/testauth-login.yaml [new file with mode: 0644]
framework/src/test/resources/open-cli-schema/testauth-logout.yaml [new file with mode: 0644]
framework/src/test/resources/sample-test-schema-yes-auth-with-additional-params-no-catalog.yaml [new file with mode: 0644]

index 9d1d9d0..d50ff46 100644 (file)
@@ -207,7 +207,18 @@ public class OnapCommandRegistrar {
         return this.get(cmdName, this.getEnabledProductVersion());
     }
 
-    private OnapCommand get(String cmdName, String version) throws OnapCommandException {
+    /**
+     * Get the OnapCommand, which CLI main would use to find the command based on the command name.
+     *
+     * @param cmdName
+     *            Name of command
+     * @param version
+     *            product version
+     * @return OnapCommand
+     * @throws OnapCommandException
+     *             Exception
+     */
+    public OnapCommand get(String cmdName, String version) throws OnapCommandException {
         Class<? extends OnapCommand> cls = registry.get(cmdName + ":" + version);
         //mrkanag: Restrict auth/catalog type commands only available during devMode. in production
         //don't expose the auth type and catalog type commands
@@ -233,7 +244,7 @@ public class OnapCommandRegistrar {
                 if (ano.schema() != null && !ano.schema().isEmpty()) {
                     map.put(ano.schema(), cmd);
                 } else if (ano.type() != null && !ano.type().isEmpty()) {
-                       this.registerProfilePlugin(ano.type(), cmd);
+                    this.registerProfilePlugin(ano.type(), cmd);
                     map.put(ano.type(), cmd);
                 } else {
                     throw new OnapUnsupportedSchemaProfile(ano.schema());
index 471b7ea..6a32480 100644 (file)
@@ -32,6 +32,7 @@ import org.onap.cli.fw.http.HttpInput;
 import org.onap.cli.fw.http.HttpResult;
 import org.onap.cli.fw.http.OnapHttpConnection;
 import org.onap.cli.fw.output.OnapCommandResultAttribute;
+import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
 import org.onap.cli.fw.utils.OnapCommandUtils;
 
 /**
@@ -62,7 +63,7 @@ public class OnapAuthClient {
             return;
         }
 
-        OnapCommand login = this.findAuthCommand("login");
+        OnapCommand login = OnapCommandDiscoveryUtils.findAuthCommand(this.cmd, "login");
 
         OnapCommandUtils.copyParamsFrom(this.cmd, login);
         login.execute();
@@ -91,7 +92,7 @@ public class OnapAuthClient {
             return;
         }
 
-        OnapCommand logout = this.findAuthCommand("logout");
+        OnapCommand logout = OnapCommandDiscoveryUtils.findAuthCommand(this.cmd, "logout");
 
         OnapCommandUtils.copyParamsFrom(this.cmd, logout);
 
@@ -116,7 +117,11 @@ public class OnapAuthClient {
         } else { //Catalog mode
             OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog");
 
-            OnapCommandUtils.copyParamsFrom(cmd, catalog);
+            Map<String, String> paramsOverrides = new HashMap<>();
+            paramsOverrides.put(Constants.CATALOG_SERVICE_NAME, cmd.getService().getName());
+            paramsOverrides.put(Constants.CATALOG_SERVICE_VERSION, cmd.getService().getVersion());
+
+            OnapCommandUtils.copyParamsFrom(cmd, catalog, paramsOverrides);
 
             catalog.execute();
 
@@ -153,26 +158,4 @@ public class OnapAuthClient {
     public HttpResult run(HttpInput input) throws OnapCommandHttpFailure {
         return this.http.request(input);
     }
-
-    /**
-     *
-     * @param authAction login/logout
-     * @return
-     * @throws OnapCommandException
-     */
-    private OnapCommand findAuthCommand(String authAction) throws OnapCommandException {
-        OnapCommand auth = null;
-        try {
-            //Find the auth command for the given service and version under current enabled product
-            auth = OnapCommandRegistrar.getRegistrar().get(
-                    this.cmd.getInfo().getService() + "-" +
-                    this.cmd.getService().getAuthType() + "-" + authAction);
-        } catch (OnapCommandNotFound e) {
-            //Find the auth command for current enabled product
-            auth = OnapCommandRegistrar.getRegistrar().get(
-                        this.cmd.getService().getAuthType() + "-" + authAction);
-        }
-
-        return auth;
-    }
 }
index 3722043..e1b3ea2 100644 (file)
@@ -35,12 +35,15 @@ import java.util.Map;
 import java.util.ServiceLoader;
 
 import org.onap.cli.fw.OnapCommand;
+import org.onap.cli.fw.OnapCommandRegistrar;
+import org.onap.cli.fw.cmd.OnapHttpCommand;
 import org.onap.cli.fw.conf.Constants;
 import org.onap.cli.fw.conf.OnapCommandConfg;
 import org.onap.cli.fw.error.OnapCommandDiscoveryFailed;
 import org.onap.cli.fw.error.OnapCommandException;
 import org.onap.cli.fw.error.OnapCommandInstantiationFailed;
 import org.onap.cli.fw.error.OnapCommandInvalidSchema;
+import org.onap.cli.fw.error.OnapCommandNotFound;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 import org.springframework.core.io.support.ResourcePatternResolver;
@@ -319,4 +322,27 @@ public class OnapCommandDiscoveryUtils {
         }
 
     }
+
+    /**
+    *
+    * @param authAction login/logout
+    * @return
+    * @throws OnapCommandException
+    */
+   public static OnapCommand findAuthCommand(OnapHttpCommand forCmd, String authAction) throws OnapCommandException {
+       OnapCommand auth = null;
+       try {
+           //mrkanag: fix this to discover the auth command by matching info->product & service
+           auth = OnapCommandRegistrar.getRegistrar().get(
+                   forCmd.getInfo().getService() + "-" +
+                   forCmd.getService().getAuthType() + "-" + authAction,
+                   forCmd.getInfo().getProduct());
+       } catch (OnapCommandNotFound e) {
+           auth = OnapCommandRegistrar.getRegistrar().get(
+                   forCmd.getService().getAuthType() + "-" + authAction,
+                   forCmd.getInfo().getProduct());
+       }
+
+       return auth;
+   }
 }
index ba72abd..04bfd23 100644 (file)
@@ -705,6 +705,13 @@ public class OnapCommandSchemaLoaderUtils {
         }catch (OnapCommandException e) {
             OnapCommandUtils.throwOrCollect(e, errorList, validate);
         }
+
+        //Handle the parameters for auth
+        if (!cmd.getService().isNoAuth()) {
+            OnapCommand login = OnapCommandDiscoveryUtils.findAuthCommand(cmd, "login");
+            OnapCommandUtils.copyParamSchemasFrom(login, cmd);
+        }
+
         return errorList;
     }
 
index 0c5a75f..57a2e30 100644 (file)
@@ -705,7 +705,7 @@ public class OnapCommandUtils {
      *
      * @throws OnapCommandInvalidParameterValue
      */
-    public static void copyParamsFrom(OnapHttpCommand from, OnapCommand to) throws OnapCommandInvalidParameterValue {
+    public static void copyParamsFrom(OnapCommand from, OnapCommand to, Map<String, String> paramOverride) throws OnapCommandInvalidParameterValue {
         for (OnapCommandParameter param: to.getParameters()) {
 
             OnapCommandParameter fromParam = from.getParametersMap().get(param.getName());
@@ -713,10 +713,28 @@ public class OnapCommandUtils {
             if (fromParam != null) {
                 param.setValue(fromParam.getValue());
                 param.setDefaultValue(fromParam.getDefaultValue());
-            } else if (param.getName().equalsIgnoreCase(Constants.CATALOG_SERVICE_NAME)) { // for catalog cmd
-                param.setValue(from.getService().getName());
-            } else if (param.getName().equalsIgnoreCase(Constants.CATALOG_SERVICE_VERSION)) {  // for catalog cmd
-                param.setValue(from.getService().getVersion());
+            }
+
+            if (paramOverride.containsKey(param.getName())) {
+                 param.setValue(paramOverride.get(param.getName()));
+            }
+        }
+    }
+
+    public static void copyParamsFrom(OnapCommand from, OnapCommand to) throws OnapCommandInvalidParameterValue {
+        OnapCommandUtils.copyParamsFrom(from, to, new HashMap<String, String>());
+    }
+
+    /**
+     * Copy param schema from source command to destination command, useful in adding login command params into command
+     * @param from
+     * @param to
+     * @throws OnapCommandException
+     */
+    public static void copyParamSchemasFrom(OnapCommand from, OnapCommand to) throws OnapCommandException {
+        for (OnapCommandParameter param: from.getParameters()) {
+            if (!to.getParametersMap().containsKey(param.getName())) {
+                to.getParameters().add(param);
             }
         }
     }
index 9c733c9..58b44f4 100644 (file)
@@ -16,6 +16,7 @@
 
 package org.onap.cli.fw.ad;
 
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import org.junit.Before;
@@ -91,6 +92,17 @@ public class OnapAuthClientCommandBasedTest {
         }
     }
 
+    @Test
+    public void noCatalogYesAuthWithAdditionalParamsTest() throws OnapCommandException {
+        try {
+            OnapHttpCommand cmd = getCommand("sample-test-schema-yes-auth-with-additional-params-no-catalog.yaml");
+            assertTrue(cmd.getParametersMap().containsKey("string-param"));
+        } catch (OnapCommandException e) {
+            fail("External command Yes Auth No Catalog failed to run");
+            e.printStackTrace(System.out);
+        }
+    }
+
     @Test
     public void noCatalogNoAuthTest() throws OnapCommandException {
         try {
diff --git a/framework/src/test/resources/open-cli-schema/testauth-login.yaml b/framework/src/test/resources/open-cli-schema/testauth-login.yaml
new file mode 100644 (file)
index 0000000..8f3a833
--- /dev/null
@@ -0,0 +1,28 @@
+open_cli_schema_version: 1.0
+
+name: testauth-login
+
+description: basic login auth command
+
+info:
+  product: open-cli
+  service: test
+  type: auth
+  author: Kanagaraj Manickam mkr1481@gmail.com
+
+parameters:
+  - name: string-param
+    type: string
+    description: Oclip string param
+    long_option: string-param
+    short_option: c
+    is_optional: false
+    default_Value: test
+
+# followings are dummy simulator for http command
+http:
+   request:
+     uri: /
+     method: GET
+   success_codes:
+     - 200
diff --git a/framework/src/test/resources/open-cli-schema/testauth-logout.yaml b/framework/src/test/resources/open-cli-schema/testauth-logout.yaml
new file mode 100644 (file)
index 0000000..d8de7c6
--- /dev/null
@@ -0,0 +1,19 @@
+open_cli_schema_version: 1.0
+
+name: testauth-logout
+
+description: basic logout auth command
+
+info:
+  product: open-cli
+  service: test
+  type: auth
+  author: Kanagaraj Manickam mkr1481@gmail.com
+
+# followings are dummy simulator for http command
+http:
+   request:
+     uri: /
+     method: GET
+   success_codes:
+     - 200
\ No newline at end of file
diff --git a/framework/src/test/resources/sample-test-schema-yes-auth-with-additional-params-no-catalog.yaml b/framework/src/test/resources/sample-test-schema-yes-auth-with-additional-params-no-catalog.yaml
new file mode 100644 (file)
index 0000000..4470e63
--- /dev/null
@@ -0,0 +1,28 @@
+open_cli_schema_version: 1.0
+
+name: sample-cmd-yes-auth-no-catalog-extra-params
+
+description: sample
+
+
+info:
+  product: open-cli
+  service: test
+  type: cmd
+  author: Kanagaraj Manickam mkr1481@gmail.com
+
+http:
+
+  service:
+    name: sample
+    version: v1
+    auth: testauth
+    mode: catalog
+
+  request:
+    uri: /test
+    method: GET
+  success_codes:
+    - 200
+  result_map:
+    name: ${name}