def combineParameters()

in magenta-lib/src/main/scala/magenta/tasks/UpdateCloudFormationTask.scala [273:330]


  def combineParameters(
      deployParameters: Map[String, String],
      existingParameters: List[ExistingParameter],
      templateParameters: List[TemplateParameter],
      specifiedParameters: Map[String, String]
  ): Either[String, Map[String, ParameterValue]] = {

    // Start with the complete list of keys that must be found or have default values
    val allRequiredParamNames = templateParameters.map(_.key).toSet
    // use existing value for all template values in the existing parameter list
    val existingParametersMap: Map[String, ParameterValue] =
      existingParameters.collect {
        case ExistingParameter(key, _, _)
            if allRequiredParamNames.contains(key) =>
          key -> UseExistingValue
      }.toMap
    // Convert the full list of specified parameters
    val specifiedParametersMap: Map[String, ParameterValue] =
      specifiedParameters.view.mapValues(SpecifiedValue.apply).toMap
    // Get the deployment parameters that are present in the template
    val deployParametersInTemplate: Map[String, ParameterValue] =
      deployParameters
        .collect {
          case (key, value) if allRequiredParamNames.contains(key) =>
            key -> SpecifiedValue(value)
        }
    // Now combine them all together in increasing priority
    val parametersToProvide =
      existingParametersMap ++ specifiedParametersMap ++ deployParametersInTemplate

    // Next we need to check we have all the parameters we need and none that we don't recognise

    // Get the parameters that have defaults in the template
    val parametersWithTemplateDefaults =
      templateParameters.filter(_.default).map(_.key).toSet
    // Compute the set of parameters that we will neither provide nor have default values
    val missingParams =
      allRequiredParamNames -- parametersToProvide.keySet -- parametersWithTemplateDefaults

    // And the set of parameters that we think we should provide but don't exist
    val unknownParams = parametersToProvide.keySet -- allRequiredParamNames

    if (missingParams.nonEmpty) {
      Left(
        s"""Missing parameters for ${missingParams.toList.sorted.mkString(", ")}: all new parameters without a
           |default must be set when creating or updating stacks. Subsequent updates reuse existing parameters
           |where possible.""".stripMargin
      )
    } else if (unknownParams.nonEmpty) {
      Left(
        s"""User specified parameters that are not in template: ${unknownParams.toList.sorted
            .mkString(", ")}.
           | Ensure that you are not specifying a parameter directly or as an AMI parameter.
           |""".stripMargin
      )
    } else {
      Right(parametersToProvide)
    }