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, region, profile) ⇒ Stack



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

def initialize(name, region, profile)
  @name = name
  @parameters = {}
  @tags = {}
  @template = ''
  @capabilities = []
  @timeout = 15
  @on_failure = 'DO_NOTHING'

  @region = region
  @profile = profile
  @credentials = Aws::SharedCredentials.new(profile_name: @profile)
  @client = Aws::CloudFormation::Client.new(region: @region, credentials: @credentials)
end

Instance Attribute Details

#capabilities=(value) ⇒ Object (writeonly)

Sets the attribute capabilities



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



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



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

def template=(value)
  @template = value
end

#timeout=(value) ⇒ Object (writeonly)

Sets the attribute timeout



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

def timeout=(value)
  @timeout = value
end

Instance Method Details

#create!Object



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
56
57
58
59
# File 'lib/hygroscope/stack.rb', line 24

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



94
95
96
97
98
# File 'lib/hygroscope/stack.rb', line 94

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

#describeObject



100
101
102
103
104
105
106
# File 'lib/hygroscope/stack.rb', line 100

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

#list_resourcesObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/hygroscope/stack.rb', line 108

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hygroscope/stack.rb', line 61

def update!
  stack_parameters = []
  @parameters.each do |k, v|
    stack_parameters << if v == 'HYGROSCOPE_USE_PREVIOUS_VALUE'
                          {
                            parameter_key:      k,
                            use_previous_value: true
                          }
                        else
                          {
                            parameter_key:   k,
                            parameter_value: v.to_s
                          }
                        end
  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