Class: Dev::Template::Application::Config

Inherits:
Dev::Template::ApplicationInterface show all
Defined in:
lib/firespring_dev_commands/templates/config.rb

Overview

Contains rake tasks for displaying and setting application variables in parameter store

Instance Attribute Summary collapse

Attributes inherited from Dev::Template::ApplicationInterface

#name

Attributes inherited from BaseInterface

#exclude

Instance Method Summary collapse

Methods inherited from BaseInterface

#create_tasks!

Constructor Details

#initialize(name, path_prefix, key_parameter_path: nil, exclude: []) ⇒ Config

Allow for custom config path_prefix for the application Allow for custom key parameter path (otherwise base it off the path prefix)



13
14
15
16
17
18
# File 'lib/firespring_dev_commands/templates/config.rb', line 13

def initialize(name, path_prefix, key_parameter_path: nil, exclude: [])
  @path_prefix = path_prefix
  @key_parameter_path = key_parameter_path || "#{path_prefix}/kms/id"

  super(name, exclude:)
end

Instance Attribute Details

#key_parameter_pathObject (readonly)

Returns the value of attribute key_parameter_path.



9
10
11
# File 'lib/firespring_dev_commands/templates/config.rb', line 9

def key_parameter_path
  @key_parameter_path
end

#path_prefixObject (readonly)

Returns the value of attribute path_prefix.



9
10
11
# File 'lib/firespring_dev_commands/templates/config.rb', line 9

def path_prefix
  @path_prefix
end

Instance Method Details

#create_list_task!Object

Create the list rake task



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/firespring_dev_commands/templates/config.rb', line 21

def create_list_task!
  # Have to set a local variable to be accessible inside of the instance_eval block
  application = @name
  path_prefix = @path_prefix
  exclude = @exclude

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace application do
      namespace :config do
        return if exclude.include?(:list)

        desc "List all #{application} configs"
        task list: %w(init) do
          puts
          puts "Listing all parameters which start with #{path_prefix}:".light_green
          puts
          Dev::Aws::Parameter.new.list(path_prefix).each do |it|
            puts "  #{it.name.light_white} => #{it.value.light_white} (#{it.type})"
          end
          puts
        end
      end
    end
  end
end

#create_set_task!Object

rubocop:disable Metrics/MethodLength Create the set task



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/firespring_dev_commands/templates/config.rb', line 49

def create_set_task!
  # Have to set a local variable to be accessible inside of the instance_eval block
  application = @name
  path_prefix = @path_prefix
  key_parameter_path = @key_parameter_path
  exclude = @exclude

  DEV_COMMANDS_TOP_LEVEL.instance_eval do
    namespace application do
      namespace :config do
        return if exclude.include?(:set)

        desc 'Updates the parameter with the given name to the new value' \
             "\n\tspecify NAME as the name of the parameter to be set (it will be prefixed with the base path for this app)" \
             "\n\tspecify VALUE is required and is the value you wish the paramete to be set to" \
             "\n\toptionally specify ENCRYPT=true to change a String type to a SecureString type"
        task set: %w(ensure_aws_credentials) do
          raise 'NAME is required' if ENV['NAME'].to_s.strip.blank?
          raise 'NAME is not found in this apps parameters' if Dev::Aws::Parameter.new.list(path_prefix).none? { |it| it.name == ENV['NAME'] }
          raise 'VALUE is required' if ENV['VALUE'].to_s.strip.blank?

          param_path = ENV.fetch('NAME', nil)
          new_value = ENV.fetch('VALUE', nil)
          old_value = Dev::Aws::Parameter.new.get(param_path)

          params = {type: 'String'}
          if ENV['ENCRYPT'].to_s.strip == 'true' || old_value.type == 'SecureString'
            params[:type] = 'SecureString'
            params[:key_id] = Dev::Aws::Parameter.new.get_value(key_parameter_path)
          end

          message = 'This will change '.light_green +
                    param_path.light_yellow +
                    ' from "'.light_green +
                    old_value.value.light_yellow +
                    '" ('.light_green +
                    old_value.type.light_yellow +
                    ') to "'.light_green +
                    new_value.light_yellow +
                    '" ('.light_green +
                    params[:type].light_yellow +
                    '). Continue'.light_green
          Dev::Common.new.with_confirmation(message, color_message: false) do
            Dev::Aws::Parameter.new.put(param_path, new_value, **params)
          end
        end
      end
    end
  end
end