Class: StackMaster::TestDriver::CloudFormation

Inherits:
Object
  • Object
show all
Defined in:
lib/stack_master/test_driver/cloud_formation.rb

Instance Method Summary collapse

Constructor Details

#initializeCloudFormation

Returns a new instance of CloudFormation.



64
65
66
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 64

def initialize
  reset
end

Instance Method Details

#add_stack(stack) ⇒ Object



171
172
173
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 171

def add_stack(stack)
  @stacks[stack.fetch(:stack_name)] = Stack.new(stack)
end

#add_stack_event(event) ⇒ Object



184
185
186
187
188
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 184

def add_stack_event(event)
  stack_name = event.fetch(:stack_name)
  @stack_events[stack_name] ||= []
  @stack_events[stack_name] << StackEvent.new(event)
end

#add_stack_resource(options) ⇒ Object



175
176
177
178
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 175

def add_stack_resource(options)
  @stack_resources[options.fetch(:stack_name)] ||= []
  @stack_resources[options.fetch(:stack_name)] << StackResource.new(options)
end

#create_change_set(options) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 85

def create_change_set(options)
  id = SecureRandom.uuid
  options.merge!(change_set_id: id)
  @change_sets[id] = options
  @change_sets[options.fetch(:change_set_name)] = options
  OpenStruct.new(id: id)
end

#create_stack(options) ⇒ Object



156
157
158
159
160
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 156

def create_stack(options)
  stack_name = options.fetch(:stack_name)
  add_stack(options)
  @stack_policies[stack_name] = options[:stack_policy_body]
end

#delete_change_set(options) ⇒ Object



112
113
114
115
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 112

def delete_change_set(options)
  change_set_id = options.fetch(:change_set_name)
  @change_sets.delete(change_set_id)
end

#delete_stack(options) ⇒ Object



162
163
164
165
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 162

def delete_stack(options)
  stack_name = options.fetch(:stack_name)
  @stacks.delete(stack_name)
end

#describe_change_set(options) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 93

def describe_change_set(options)
  change_set_id = options.fetch(:change_set_name)
  change_set = @change_sets.fetch(change_set_id)
  change_details = [
    OpenStruct.new(evaluation: 'Static', change_source: 'ResourceReference', target: OpenStruct.new(attribute: 'Properties', requires_recreation: 'Always', name: 'blah'))
  ]
  change = OpenStruct.new(action: 'Modify', replacement: 'True', scope: ['Properties'], details: change_details)
  changes = [
    OpenStruct.new(type: 'AWS::Resource', resource_change: change)
  ]
  OpenStruct.new(change_set.merge(changes: changes, status: 'CREATE_COMPLETE'))
end

#describe_stack_events(options) ⇒ Object



145
146
147
148
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 145

def describe_stack_events(options)
  events = @stack_events[options.fetch(:stack_name)] || []
  OpenStruct.new(stack_events: events, next_token: nil)
end

#describe_stack_resources(options = {}) ⇒ Object



131
132
133
134
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 131

def describe_stack_resources(options = {})
  @stacks.fetch(options.fetch(:stack_name)) { raise Aws::CloudFormation::Errors::ValidationError.new('', 'Stack does not exist') }
  OpenStruct.new(stack_resources: @stack_resources[options.fetch(:stack_name)])
end

#describe_stacks(options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 117

def describe_stacks(options = {})
  stack_name = options[:stack_name]
  stacks = if stack_name
    if @stacks[stack_name]
      [@stacks[stack_name]]
    else
      raise Aws::CloudFormation::Errors::ValidationError.new('', 'Stack does not exist')
    end
  else
    @stacks.values
  end
  OpenStruct.new(stacks: stacks, next_token: nil)
end

#execute_change_set(options) ⇒ Object



106
107
108
109
110
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 106

def execute_change_set(options)
  change_set_id = options.fetch(:change_set_name)
  change_set = @change_sets.fetch(change_set_id)
  update_stack(change_set)
end

#get_stack_policy(options) ⇒ Object



141
142
143
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 141

def get_stack_policy(options)
  OpenStruct.new(stack_policy_body: @stack_policies[options.fetch(:stack_name)])
end

#get_template(options) ⇒ Object



136
137
138
139
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 136

def get_template(options)
  template_body = @templates[options[:stack_name]] || nil
  OpenStruct.new(template_body: template_body)
end

#regionObject



68
69
70
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 68

def region
  @region ||= ENV['AWS_REGION'] || Aws.config[:region] || Aws.shared_config.region
end

#resetObject



76
77
78
79
80
81
82
83
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 76

def reset
  @stacks = {}
  @templates = {}
  @stack_events = {}
  @stack_resources = {}
  @stack_policies = {}
  @change_sets = {}
end

#set_region(region) ⇒ Object



72
73
74
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 72

def set_region(region)
  @region = region
end

#set_template(stack_name, template) ⇒ Object



180
181
182
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 180

def set_template(stack_name, template)
  @templates[stack_name] = template
end

#update_stack(options) ⇒ Object



150
151
152
153
154
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 150

def update_stack(options)
  stack_name = options.fetch(:stack_name)
  @stacks[stack_name].attributes = options
  @stack_policies[stack_name] = options[:stack_policy_body]
end

#validate_template(options) ⇒ Object



167
168
169
# File 'lib/stack_master/test_driver/cloud_formation.rb', line 167

def validate_template(options)
  true
end