Class: OpenStax::Aws::StackFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/aws/stack_factory.rb

Defined Under Namespace

Classes: ParameterDefaultsFactory, VolatileParametersFactory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, deployment:) ⇒ StackFactory

Returns a new instance of StackFactory.



5
6
7
8
9
10
11
12
13
14
# File 'lib/openstax/aws/stack_factory.rb', line 5

def initialize(id:, deployment:)
  raise "`deployment` cannot be nil" if deployment.nil?

  @id = id
  @deployment = deployment
  @attributes = {
    parameter_defaults: {},
    tags: {}
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/openstax/aws/stack_factory.rb', line 22

def method_missing(name, *args, &block)
  if args.empty? && !block_given?
    attributes[name.to_sym]
  else
    attributes[name.to_sym] = args.empty? ?
                                @deployment.instance_eval(&block) :
                                args[0]
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/openstax/aws/stack_factory.rb', line 3

def attributes
  @attributes
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/openstax/aws/stack_factory.rb', line 3

def id
  @id
end

Class Method Details

.build(id:, deployment:, &block) ⇒ Object



16
17
18
19
20
# File 'lib/openstax/aws/stack_factory.rb', line 16

def self.build(id:, deployment:, &block)
  factory = new(id: id, deployment: deployment)
  factory.instance_eval(&block) if block_given?
  factory.build
end

Instance Method Details

#autoset_absolute_template_path(fallback_base_directory) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/openstax/aws/stack_factory.rb', line 41

def autoset_absolute_template_path(fallback_base_directory)
  base_directory = template_directory || fallback_base_directory

  if base_directory.blank?
    raise "Tried to autoset the absolute_template_path but didn't have " \
          "access to a base directory (e.g. set with template_directory)"
  end

  if relative_template_path
    path = File.join(base_directory, relative_template_path)
  else
    path = File.join(base_directory, "#{@id}.yml")

    if !File.file?(path)
      path = File.join(base_directory, "#{@id}.yml.erb")

      if !File.file?(path)
        path = File.join(base_directory, "#{@id}.json")
        if !File.file?(path)
          raise "Couldn't infer an existing template file for stack #{@id}"
        end
      end
    end
  end

  self.absolute_template_path(path)
end

#buildObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/openstax/aws/stack_factory.rb', line 93

def build
  autoset_absolute_template_path(nil) if absolute_template_path.blank?

  initializer_args = {
    id: id,
    name: attributes[:name],
    tags: @deployment.tags.merge(attributes[:tags]),
    region: attributes[:region],
    enable_termination_protection: attributes[:enable_termination_protection],
    absolute_template_path: attributes[:absolute_template_path],
    capabilities: attributes[:capabilities],
    parameter_defaults: attributes[:parameter_defaults],
    volatile_parameters_block: attributes[:volatile_parameters_block],
    secrets_blocks: attributes[:secrets_blocks],
    secrets_context: @deployment,
    secrets_namespace: [@deployment.env_name, @deployment.name],
    shared_secrets_substitutions_block: @deployment.shared_secrets_substitutions_block,
    cycle_if_different_parameter: attributes[:cycle_if_different_parameter],
    dry_run: attributes[:dry_run]
  }

  template = Template.from_absolute_file_path(absolute_template_path)
  if template.is_sam?
    if !@deployment.respond_to?(:sam_build_directory)
      raise "You must set the SAM build directory with a call to `sam_build_directory`"
    end

    initializer_args[:build_directory] = @deployment.sam_build_directory
    SamStack.new(**initializer_args)
  else
    Stack.new(**initializer_args)
  end
end

#cycle_if_different_parameter(name = nil, &block) ⇒ Object



84
85
86
# File 'lib/openstax/aws/stack_factory.rb', line 84

def cycle_if_different_parameter(name=nil, &block)
  attributes[:cycle_if_different_parameter] = block.present? ? block.call : name
end

#parameter_defaults(&block) ⇒ Object



69
70
71
72
73
# File 'lib/openstax/aws/stack_factory.rb', line 69

def parameter_defaults(&block)
  factory = ParameterDefaultsFactory.new(@deployment)
  factory.instance_eval(&block) if block_given?
  attributes[:parameter_defaults].merge!(factory.attributes)
end

#secrets(&block) ⇒ Object



79
80
81
82
# File 'lib/openstax/aws/stack_factory.rb', line 79

def secrets(&block)
  attributes[:secrets_blocks] ||= []
  attributes[:secrets_blocks].push(block)
end

#tag(key, value) ⇒ Object



88
89
90
91
# File 'lib/openstax/aws/stack_factory.rb', line 88

def tag(key, value)
  raise 'The first argument to `tag` must not be blank' if key.blank?
  (attributes[:tags] ||= {})[key] = value
end

#template_directory(*directory_parts) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/openstax/aws/stack_factory.rb', line 32

def template_directory(*directory_parts)
  if directory_parts.empty? && !block_given?
    attributes[:template_directory]
  else
    directory_parts = yield if directory_parts.empty?
    attributes[:template_directory] = File.join(*directory_parts)
  end
end

#volatile_parameters(&block) ⇒ Object



75
76
77
# File 'lib/openstax/aws/stack_factory.rb', line 75

def volatile_parameters(&block)
  attributes[:volatile_parameters_block] = block
end