Enable env parameters read 45/64945/5
authorkjaniak <kornel.janiak@nokia.com>
Thu, 6 Sep 2018 08:07:17 +0000 (10:07 +0200)
committerkjaniak <kornel.janiak@nokia.com>
Mon, 10 Sep 2018 07:22:56 +0000 (09:22 +0200)
Disabling require option on mandatory parameters
to fetch them from env variables.

Change-Id: I007dea1a7f369a04479801aa508cf1034ac1341a
Issue-ID: DCAEGEN2-741
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
hv-collector-dcae-app-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/config/ArgDcaeAppSimConfigurationTest.kt
hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt
hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt
hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt
hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentError.kt

index e7a22fc..7137fe1 100644 (file)
@@ -115,7 +115,7 @@ internal class ArgDcaeAppSimConfigurationTest : Spek({
             given("listen port is missing") {
                 it("should throw exception") {
                     assertThat(cut.parseExpectingFailure(
-                            "-p", kafkaTopics,
+                            "-p", listenPort,
                             "-s", kafkaBootstrapServers
                     )).isInstanceOf(WrongArgumentError::class.java)
                 }
index a99fef5..7381592 100644 (file)
 package org.onap.dcae.collectors.veshv.utils.arrow
 
 import arrow.core.Either
-import arrow.core.None
 import arrow.core.Option
-import arrow.core.Some
 import arrow.core.identity
 import arrow.syntax.collections.firstOption
-import java.util.*
 import java.util.concurrent.atomic.AtomicReference
 
 /**
index 1ebe4e4..da6f2d0 100644 (file)
@@ -36,19 +36,19 @@ abstract class ArgBasedConfiguration<T>(private val parser: CommandLineParser) {
     abstract val cmdLineOptionsList: List<CommandLineOption>
 
     fun parse(args: Array<out String>): Either<WrongArgumentError, T> {
-        val commandLineOptions = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption)
         val parseResult = Try {
+            val commandLineOptions = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption)
             parser.parse(commandLineOptions, args)
         }
         return parseResult
                 .toEither()
-                .mapLeft { ex -> WrongArgumentError(ex, commandLineOptions) }
+                .mapLeft { ex -> WrongArgumentError(ex, cmdLineOptionsList) }
                 .map(this::getConfiguration)
                 .flatMap {
                     it.toEither {
                         WrongArgumentError(
-                                "Unexpected error when parsing command line arguments",
-                                commandLineOptions)
+                                message = "Unexpected error when parsing command line arguments",
+                                cmdLineOptionsList = cmdLineOptionsList)
                     }
                 }
     }
index 3a154db..9379c40 100644 (file)
@@ -22,7 +22,7 @@ package org.onap.dcae.collectors.veshv.utils.commandline
 import org.apache.commons.cli.Option
 
 
-enum class CommandLineOption(val option: Option) {
+enum class CommandLineOption(val option: Option, val required: Boolean = false) {
     HEALTH_CHECK_API_PORT(Option.builder("H")
             .longOpt("health-check-api-port")
             .hasArg()
@@ -31,17 +31,17 @@ enum class CommandLineOption(val option: Option) {
     ),
     LISTEN_PORT(Option.builder("p")
             .longOpt("listen-port")
-            .required()
             .hasArg()
             .desc("Listen port")
-            .build()
+            .build(),
+            required = true
     ),
     CONSUL_CONFIG_URL(Option.builder("c")
             .longOpt("config-url")
-            .required()
             .hasArg()
             .desc("URL of ves configuration on consul")
-            .build()
+            .build(),
+            required = true
     ),
     CONSUL_FIRST_REQUEST_DELAY(Option.builder("d")
             .longOpt("first-request-delay")
@@ -57,31 +57,31 @@ enum class CommandLineOption(val option: Option) {
     ),
     VES_HV_PORT(Option.builder("v")
             .longOpt("ves-port")
-            .required()
             .hasArg()
             .desc("VesHvCollector port")
-            .build()
+            .build(),
+            required = true
     ),
     VES_HV_HOST(Option.builder("h")
             .longOpt("ves-host")
-            .required()
             .hasArg()
             .desc("VesHvCollector host")
-            .build()
+            .build(),
+            required = true
     ),
     KAFKA_SERVERS(Option.builder("s")
             .longOpt("kafka-bootstrap-servers")
-            .required()
             .hasArg()
             .desc("Comma-separated Kafka bootstrap servers in <host>:<port> format")
-            .build()
+            .build(),
+            required = true
     ),
     KAFKA_TOPICS(Option.builder("f")
             .longOpt("kafka-topics")
-            .required()
             .hasArg()
             .desc("Comma-separated Kafka topics")
-            .build()
+            .build(),
+            required = true
     ),
     SSL_DISABLE(Option.builder("l")
             .longOpt("ssl-disable")
index 632858b..61a461f 100644 (file)
  */
 package org.onap.dcae.collectors.veshv.utils.commandline
 
+import arrow.core.Option
 import org.apache.commons.cli.HelpFormatter
 import org.apache.commons.cli.Options
 
 
 data class WrongArgumentError(
         val message: String,
-        private val options: Options,
-        val cause: Throwable? = null) {
+        val cause: Throwable? = null,
+        val cmdLineOptionsList: List<CommandLineOption>) {
 
-    constructor(par: Throwable, options: Options) : this(par.message ?: "", options, par)
+    constructor(par: Throwable, cmdLineOptionsList: List<CommandLineOption>) :
+            this(par.message ?: "",
+                    par,
+                    cmdLineOptionsList)
 
     fun printMessage() {
         println(message)
@@ -36,7 +40,28 @@ data class WrongArgumentError(
 
     fun printHelp(programName: String) {
         val formatter = HelpFormatter()
-        formatter.printHelp(programName, options)
+        val footer = "All parameters can be specified as environment variables using upper-snake-case full " +
+                "name with prefix `VESHV_`."
+
+        formatter.printHelp(
+                programName,
+                generateRequiredParametersNote(),
+                getOptions(),
+                footer)
     }
+
+    private fun getOptions(): Options = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption)
+
+    private fun generateRequiredParametersNote(): String {
+        val requiredParams = Option.fromNullable(cmdLineOptionsList.filter { it.required }
+                .takeUnless { it.isEmpty() })
+        return requiredParams.fold(
+                { "" },
+                { it.map {commandLineOption -> commandLineOption.option.opt }
+                        .joinToString(prefix = "Required parameters: ", separator = ", ")
+                }
+        )
+    }
+
 }