Class: Hygroscope::Stack

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Stack

Returns a new instance of Stack.



9
10
11
12
13
14
15
16
17
18
# File 'lib/hygroscope/stack.rb', line 9

def initialize(name)
  @name = name
  @parameters = {}
  @tags = {}
  @template = ''
  @capabilities = []
  @timeout = 15
  @on_failure = 'DO_NOTHING'
  @client = Aws::CloudFormation::Client.new
end

Instance Attribute Details

#capabilities=(value) ⇒ Object (writeonly)

Sets the attribute capabilities

Parameters:

  • value

    the value to set the attribute capabilities to.



6
7
8
# File 'lib/hygroscope/stack.rb', line 6

def capabilities=(value)
  @capabilities = value
end

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/hygroscope/stack.rb', line 7

def client
  @client
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/hygroscope/stack.rb', line 5

def name
  @name
end

#on_failure=(value) ⇒ Object (writeonly)

Sets the attribute on_failure

Parameters:

  • value

    the value to set the attribute on_failure to.



6
7
8
# File 'lib/hygroscope/stack.rb', line 6

def on_failure=(value)
  @on_failure = value
end

#parametersObject

Returns the value of attribute parameters.



5
6
7
# File 'lib/hygroscope/stack.rb', line 5

def parameters
  @parameters
end

#tagsObject

Returns the value of attribute tags.



5
6
7
# File 'lib/hygroscope/stack.rb', line 5

def tags
  @tags
end

#template=(value) ⇒ Object (writeonly)

Sets the attribute template

Parameters:

  • value

    the value to set the attribute template to.



6
7
8
# File 'lib/hygroscope/stack.rb', line 6

def template=(value)
  @template = value
end

#timeout=(value) ⇒ Object (writeonly)

Sets the attribute timeout

Parameters:

  • value

    the value to set the attribute timeout to.



6
7
8
# File 'lib/hygroscope/stack.rb', line 6

def timeout=(value)
  @timeout = value
end

Instance Method Details

#create!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hygroscope/stack.rb', line 20

def create!
  stack_parameters = []
  @parameters.each do |k, v|
    stack_parameters << {
      parameter_key: k,
      parameter_value: v.to_s
    }
  end

  stack_tags = []
  @tags.each do |k, v|
    stack_tags << {
      key: k,
      value: v.to_s
    }
  end

  stack_opts = {
    stack_name: @name,
    template_body: @template,
    parameters: stack_parameters,
    timeout_in_minutes: @timeout,
    on_failure: @on_failure
  }

  stack_opts['capabilities'] = @capabilities unless @capabilities.empty?
  stack_opts['tags'] = stack_tags

  begin
    stack_id = @client.create_stack(stack_opts)
  rescue => e
    raise e
  end

  stack_id
end

#delete!Object



83
84
85
86
87
# File 'lib/hygroscope/stack.rb', line 83

def delete!
  @client.delete_stack(stack_name: @name)
rescue => e
  raise e
end

#describeObject



89
90
91
92
93
94
95
# File 'lib/hygroscope/stack.rb', line 89

def describe
  resp = @client.describe_stacks(stack_name: @name)
rescue => e
  raise e
else
  resp.stacks.first
end

#list_resourcesObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hygroscope/stack.rb', line 97

def list_resources
  resp = @client.describe_stack_resources(stack_name: @name)
rescue => e
  raise e
else
  resources = []
  resp.stack_resources.each do |r|
    resources << {
      name: r.logical_resource_id,
      type: r.resource_type,
      status: r.resource_status
    }
  end

  resources
end

#update!Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hygroscope/stack.rb', line 57

def update!
  stack_parameters = []
  @parameters.each do |k, v|
    stack_parameters << {
      parameter_key: k,
      parameter_value: v.to_s
    }
  end

  stack_opts = {
    stack_name: @name,
    template_body: @template,
    parameters: stack_parameters
  }

  stack_opts['capabilities'] = @capabilities unless @capabilities.empty?

  begin
    stack_id = @client.update_stack(stack_opts)
  rescue => e
    raise e
  end

  stack_id
end