class SparkleFormation
  module SparkleAttribute
    module Aws

      require 'vault'
      require 'securerandom'

      # Polluting SparkleFormation::SparkleAttribute::Aws namespace ?
      include SfnVault::Utils

      # example usage: vault_parameter!(:masterpassword)
      def _vault_parameter(*vp_args)
        vp_name, vp_opts = vp_args
        __t_stringish(vp_name)
        if vp_opts
          vp_path = vp_opts[:path] if vp_opts[:path]
        end
        parameters.set!("vault_parameter_#{vp_name}") do
          no_echo true
          description "Automatically generated Vault param #{vp_name}"
          type 'String'
        end
      end
      alias_method :vault_parameter!, :_vault_parameter
    end
  end
end

# do stuff
# save to vault cubbyhole/name, ex: cubbyhole/masterpassword
# generate parameter json with NoEcho true
# inject parameter value, see sfn-parameters

class SparkleFormation
  module SparkleAttribute
    module Aws

      # A small helper method for adding the specific named
      # parameter struct with the custom type
      def _vault_parameter(vp_name)
        __t_stringish(vp_name)
        parameters.set!(vp_name) do
          no_echo true
          description "Generated secret automatically stored in Vault"
          type 'Vault::Generic::Secret'
        end
      end
      alias_method :vault_parameter!, :_vault_parameter
    end
  end
end

Simplify DSL helper method: vault_parameter!
Simplify DSL helper method: vault_parameter!

This simplifies the vault helper method provided to the template DSL to simply
insert a named parameter with a generic description and NoEcho set. This
parameter uses a custom type that is not recognized by AWS and will cause a
validation error if not replaced.

The template callback will handle finding all parameters of this custom type,
generating values for them and saving them safely in Vault, either to an
appropriate project path or to the cubbyhole for the current token. The type is
then converted to 'String' and the value is filled from Vault during stack
creation.
