Class: OpenStax::Aws::ChangeSet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ ChangeSet

Returns a new instance of ChangeSet.



6
7
8
# File 'lib/openstax/aws/change_set.rb', line 6

def initialize(client:)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/openstax/aws/change_set.rb', line 4

def client
  @client
end

Instance Method Details

#create(options:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/openstax/aws/change_set.rb', line 10

def create(options:)
  create_change_set_output = client.create_change_set(options)
  @id = create_change_set_output.id

  wait_message = OpenStax::Aws::WaitMessage.new(
    message: "Waiting for change set #{id} to be ready"
  )

  begin
    client.wait_until(:change_set_create_complete, change_set_name: id) do |w|
      w.delay = OpenStax::Aws.configuration.stack_waiter_delay
      w.before_attempt do |attempts, response|
        wait_message.say_it
      end
    end
  rescue Aws::Waiters::Errors::FailureStateError => ee
    if ee.response&.status_reason =~ /didn't contain changes/
      logger.info("No changes detected, deleting change set")
      delete
      return self
    else
      logger.error(ee.response.status_reason)
      raise
    end
  rescue Aws::Waiters::Errors::WaiterFailed => ee
    logger.error("An error occurred: #{ee.message}")
    raise
  end

  @description = client.describe_change_set(change_set_name: id)

  self
end

#created?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/openstax/aws/change_set.rb', line 44

def created?
  @description.present?
end

#deleteObject



48
49
50
# File 'lib/openstax/aws/change_set.rb', line 48

def delete
  client.delete_change_set(change_set_name: id)
end

#descriptionObject



60
61
62
# File 'lib/openstax/aws/change_set.rb', line 60

def description
  @description || raise("Description not set; create failed?")
end

#executeObject



52
53
54
# File 'lib/openstax/aws/change_set.rb', line 52

def execute
  client.execute_change_set(change_set_name: id)
end

#has_change_caused_by?(entity_name) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/openstax/aws/change_set.rb', line 64

def has_change_caused_by?(entity_name)
  description.changes.any? do |change|
    change.resource_change.details.any? do |detail|
      detail.causing_entity == entity_name
    end
  end
end

#idObject



56
57
58
# File 'lib/openstax/aws/change_set.rb', line 56

def id
  @id || raise("ID isn't yet known!")
end

#parameter_value(parameter_name) ⇒ Object



72
73
74
75
76
# File 'lib/openstax/aws/change_set.rb', line 72

def parameter_value(parameter_name)
  description.parameters.select do |parameter|
    parameter.parameter_key == parameter_name
  end.first.parameter_value
end

#resource_change_summariesObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openstax/aws/change_set.rb', line 78

def resource_change_summaries
  summaries = description.changes.flat_map(&:resource_change).map do |change|
    summary = "#{change.action} '#{change.logical_resource_id}' (#{change.resource_type})"

    case change.action
    when "Modify"
      causes = change.details.map{|detail| [detail.change_source, detail.causing_entity].compact.join(":")}.join(", ")

      summary = "#{summary}: Replacement=#{change.replacement}; Due to change in #{change.scope}; Causes: #{causes}"
    end

    summary
  end
end