Class: Bora::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/bora/stack.rb

Constant Summary collapse

NO_UPDATE_MESSAGE =
"No updates are to be performed"

Instance Method Summary collapse

Constructor Details

#initialize(stack_name) ⇒ Stack

Returns a new instance of Stack.



13
14
15
16
17
# File 'lib/bora/stack.rb', line 13

def initialize(stack_name)
  @stack_name = stack_name
  @cfn = Aws::CloudFormation::Client.new
  @processed_events = Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)



89
90
91
# File 'lib/bora/stack.rb', line 89

def method_missing(sym, *args, &block)
  underlying_stack ? underlying_stack.send(sym, *args, &block) : nil
end

Instance Method Details

#create(options, &block) ⇒ Object



19
20
21
# File 'lib/bora/stack.rb', line 19

def create(options, &block)
  call_cfn_action(:create, options, &block)
end

#create_or_update(options, &block) ⇒ Object



27
28
29
# File 'lib/bora/stack.rb', line 27

def create_or_update(options, &block)
  exists? ? update(options, &block) : create(options, &block)
end

#delete(&block) ⇒ Object



36
37
38
# File 'lib/bora/stack.rb', line 36

def delete(&block)
  call_cfn_action(:delete, &block)
end

#diff(options) ⇒ Object



69
70
71
# File 'lib/bora/stack.rb', line 69

def diff(options)
  Diffy::Diff.new(template, new_template(options))
end

#eventsObject



40
41
42
43
44
# File 'lib/bora/stack.rb', line 40

def events
  return if !exists?
  events = @cfn.describe_stack_events({stack_name: underlying_stack.stack_id}).stack_events
  events.reverse.map { |e| Event.new(e) }
end

#exists?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/bora/stack.rb', line 81

def exists?
  status.exists?
end

#new_template(options, pretty = true) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/bora/stack.rb', line 58

def new_template(options, pretty = true)
  options = resolve_options(options, true)
  template = options[:template_body]
  if template
    template = JSON.pretty_generate(JSON.parse(template)) if pretty
    template
  else
    raise "new_template not yet implemented for URL #{options[:template_url]}"
  end
end

#outputsObject



46
47
48
49
# File 'lib/bora/stack.rb', line 46

def outputs
  return if !exists?
  underlying_stack.outputs.map { |output| Output.new(output) }
end

#recreate(options, &block) ⇒ Object



31
32
33
34
# File 'lib/bora/stack.rb', line 31

def recreate(options, &block)
  delete(&block) if exists?
  create(options, &block) if !exists?
end

#statusObject



77
78
79
# File 'lib/bora/stack.rb', line 77

def status
  StackStatus.new(underlying_stack)
end

#template(pretty = true) ⇒ Object



51
52
53
54
55
56
# File 'lib/bora/stack.rb', line 51

def template(pretty = true)
  return if !exists?
  template = @cfn.get_template({stack_name: @stack_name}).template_body
  template = JSON.pretty_generate(JSON.parse(template)) if pretty
  template
end

#update(options, &block) ⇒ Object



23
24
25
# File 'lib/bora/stack.rb', line 23

def update(options, &block)
  call_cfn_action(:update, options, &block)
end

#validate(options) ⇒ Object



73
74
75
# File 'lib/bora/stack.rb', line 73

def validate(options)
  @cfn.validate_template(resolve_options(options).select { |k| [:template_body, :template_url].include?(k) })
end