Class: CloudFormationRSpec::ChangeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudformation_rspec/change_set.rb

Constant Summary collapse

END_STATES =
[
  'CREATE_COMPLETE',
  'DELETE_COMPLETE',
  'FAILED'
]
WAIT_DELAY =
3
ChangeSetNotComplete =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_body:, parameters: {}) ⇒ ChangeSet

Returns a new instance of ChangeSet.



20
21
22
23
# File 'lib/cloudformation_rspec/change_set.rb', line 20

def initialize(template_body:, parameters: {})
  @template_body = template_body
  @parameters = parameters ? parameters : {}
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



18
19
20
# File 'lib/cloudformation_rspec/change_set.rb', line 18

def changes
  @changes
end

#statusObject (readonly)

Returns the value of attribute status.



18
19
20
# File 'lib/cloudformation_rspec/change_set.rb', line 18

def status
  @status
end

Class Method Details

.from_cloudformation_template(template_body:, parameters:) ⇒ Object



25
26
27
# File 'lib/cloudformation_rspec/change_set.rb', line 25

def self.from_cloudformation_template(template_body:, parameters:)
  new(template_body: template_body, parameters: parameters).tap { |change_set| change_set.create_change_set }
end

.from_sparkleformation_template(template_file:, compile_state:, parameters:) ⇒ Object



29
30
31
32
33
# File 'lib/cloudformation_rspec/change_set.rb', line 29

def self.from_sparkleformation_template(template_file:, compile_state:, parameters:)
  template_body = CloudFormationRSpec::Sparkle.compile_sparkle_template(template_file, compile_state)

  new(template_body: template_body, parameters: parameters).tap { |change_set| change_set.create_change_set }
end

Instance Method Details

#create_change_setObject



35
36
37
38
39
40
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
# File 'lib/cloudformation_rspec/change_set.rb', line 35

def create_change_set
  change_set_hash = generate_change_set_hash

  if change_set = self.class.get_from_cache(change_set_hash)
    @status = change_set.status
    @changes = change_set.changes.map { |change| CloudFormationRSpec::ResourceChange.new(change.resource_change.resource_type, change.resource_change.logical_resource_id) }
    return change_set
  end

  client = Aws::CloudFormation::Client.new
  change_set = client.create_change_set(
    change_set_name: change_set_name,
    stack_name: change_set_name,
    change_set_type: 'CREATE',
    template_body: @template_body,
    parameters: flat_parameters,
    capabilities: ['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM'],
  )
  @change_set_id = change_set.id
  stack_created = wait_change_set_review(client, change_set_name)
  response = client.describe_change_set(change_set_name: @change_set_id, stack_name: change_set_name)
  if !END_STATES.include? response.status
    raise ChangeSetNotComplete.new("Change set did not complete in time. #{response.status}")
  end
  @status = response.status
  @changes = response.changes.map { |change| CloudFormationRSpec::ResourceChange.new(change.resource_change.resource_type, change.resource_change.logical_resource_id) }
  client.delete_change_set(change_set_name: change_set_name, stack_name: change_set_name)
  client.delete_stack(stack_name: change_set_name) if stack_created
  self.class.add_to_cache(change_set_hash, response)
  response
end