Sha256: 6e3691475b3fe28a705f4d9335d6051ef19af438cfe8310177eab2c93a4ae3c1

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

require 'json'

module Stackster
  class StackUpdater

    def initialize(args)
      @config = args[:config]
      @logger = @config.logger
      @entry = args[:entry]
      @name = args[:name]
      @template_body = args[:template_body]
    end

    def update_stack_if_parameters_changed(attributes)
      if parameter_updated?(attributes)
        @logger.info "Updated parameters found."
        update
      else
        @logger.info "No Cloud Formation parameters require updating."
        return false
      end
    end

    private

    def update
      @logger.info "Updating Cloud Formation stack #{@name}."
      cloud_formation.update :name => @name,
                             :parameters => read_parameters_from_entry_attributes,
                             :template => @template_body
    end

    def parameter_updated?(attributes)
      (template_parameters - updated_parameters(attributes)) != template_parameters
    end

    def template_parameters
      json = JSON.parse @template_body
      json['Parameters'].nil? ? [] : json['Parameters'].keys
    end

    def updated_parameters attributes
      (attributes.map { |s| s.keys }).flatten
    end

    def read_parameters_from_entry_attributes
      h = {}
      entry_attributes = @entry.attributes
      template_parameters.each do |p|
        h[p] = entry_attributes[p] if entry_attributes[p]
      end
      h
    end

    def cloud_formation
      @cloud_formation ||= AWS::CloudFormation.new :config => @config
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stackster-0.2.6 lib/stackster/stack/stack_updater.rb
stackster-0.2.5 lib/stackster/stack/stack_updater.rb
stackster-0.2.4 lib/stackster/stack/stack_updater.rb
stackster-0.2.3 lib/stackster/stack/stack_updater.rb