Class: Stax::Aws::Cfn

Inherits:
Sdk
  • Object
show all
Defined in:
lib/stax/aws/cfn.rb

Constant Summary collapse

STATUSES =

stack statuses that are not DELETE_COMPLETE

%i[
  CREATE_IN_PROGRESS CREATE_FAILED CREATE_COMPLETE
  ROLLBACK_IN_PROGRESS ROLLBACK_FAILED ROLLBACK_COMPLETE
  DELETE_IN_PROGRESS DELETE_FAILED
  UPDATE_IN_PROGRESS UPDATE_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_COMPLETE
  UPDATE_ROLLBACK_IN_PROGRESS UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_ROLLBACK_COMPLETE
  REVIEW_IN_PROGRESS
]
COLORS =
{
  ## stack status
  CREATE_COMPLETE:      :green,
  DELETE_COMPLETE:      :green,
  UPDATE_COMPLETE:      :green,
  CREATE_FAILED:        :red,
  DELETE_FAILED:        :red,
  UPDATE_FAILED:        :red,
  ROLLBACK_IN_PROGRESS: :red,
  ROLLBACK_COMPLETE:    :red,
  ## resource action
  Add:    :green,
  Modify: :yellow,
  Remove: :red,
}

Class Method Summary collapse

Methods inherited from Sdk

paginate

Class Method Details

.cancel(name) ⇒ Object



127
128
129
# File 'lib/stax/aws/cfn.rb', line 127

def cancel(name)
  client.cancel_update_stack(stack_name: name)
end

.changes(opt) ⇒ Object



149
150
151
152
153
# File 'lib/stax/aws/cfn.rb', line 149

def changes(opt)
  paginate(:changes) do |next_token|
    client.describe_change_set(opt.merge(next_token: next_token))
  end
end

.changeset(opt) ⇒ Object



155
156
157
# File 'lib/stax/aws/cfn.rb', line 155

def changeset(opt)
  client.create_change_set(opt)
end

.clientObject



33
34
35
# File 'lib/stax/aws/cfn.rb', line 33

def client
  @_client ||= ::Aws::CloudFormation::Client.new
end

.create(opt) ⇒ Object



115
116
117
# File 'lib/stax/aws/cfn.rb', line 115

def create(opt)
  client.create_stack(opt)&.stack_id
end

.delete(name) ⇒ Object



123
124
125
# File 'lib/stax/aws/cfn.rb', line 123

def delete(name)
  client.delete_stack(stack_name: name)
end

.describe(name) ⇒ Object



73
74
75
76
77
# File 'lib/stax/aws/cfn.rb', line 73

def describe(name)
  client.describe_stacks(stack_name: name).stacks.first
rescue ::Aws::CloudFormation::Errors::ValidationError
  nil
end

.detect_drift(opt) ⇒ Object



163
164
165
# File 'lib/stax/aws/cfn.rb', line 163

def detect_drift(opt)
  client.detect_stack_drift(opt).stack_drift_detection_id
end

.drift_status(id) ⇒ Object



167
168
169
# File 'lib/stax/aws/cfn.rb', line 167

def drift_status(id)
  client.describe_stack_drift_detection_status(stack_drift_detection_id: id)
end

.drifts(opt) ⇒ Object



171
172
173
# File 'lib/stax/aws/cfn.rb', line 171

def drifts(opt)
  client.describe_stack_resource_drifts(opt).map(&:stack_resource_drifts).flatten
end

.events(name) ⇒ Object



59
60
61
62
63
# File 'lib/stax/aws/cfn.rb', line 59

def events(name)
  paginate(:stack_events) do |token|
    client.describe_stack_events(stack_name: name, next_token: token)
  end
end

.execute(opt) ⇒ Object



159
160
161
# File 'lib/stax/aws/cfn.rb', line 159

def execute(opt)
  client.execute_change_set(opt)
end

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/stax/aws/cfn.rb', line 79

def exists?(name)
  Aws::Cfn.describe(name) && true
rescue ::Aws::CloudFormation::Errors::ValidationError
  false
end

.exports(name) ⇒ Object

list of this stack output exports



96
97
98
# File 'lib/stax/aws/cfn.rb', line 96

def exports(name)
  describe(name).outputs.select(&:export_name)
end

.get_policy(opt) ⇒ Object



135
136
137
# File 'lib/stax/aws/cfn.rb', line 135

def get_policy(opt)
  client.get_stack_policy(opt).stack_policy_body
end

.id(name, id) ⇒ Object



65
66
67
# File 'lib/stax/aws/cfn.rb', line 65

def id(name, id)
  client.describe_stack_resource(stack_name: name, logical_resource_id: id).stack_resource_detail.physical_resource_id
end

.imports(name) ⇒ Object

list of stacks that import from this one



101
102
103
104
105
106
107
108
109
# File 'lib/stax/aws/cfn.rb', line 101

def imports(name)
  client.list_imports(export_name: name).map(&:imports)
rescue ::Aws::CloudFormation::Errors::ValidationError
  []
rescue ::Aws::CloudFormation::Errors::Throttling => e # this call rate-limits aggressively
  warn(e.message)
  sleep 1
  retry
end

.list_change_sets(name) ⇒ Object



143
144
145
146
147
# File 'lib/stax/aws/cfn.rb', line 143

def list_change_sets(name)
  paginate(:summaries) do |next_token|
    client.list_change_sets(stack_name: name, next_token: next_token)
  end
end

.output(name, key) ⇒ Object



91
92
93
# File 'lib/stax/aws/cfn.rb', line 91

def output(name, key)
  outputs(name)[key]
end

.outputs(name) ⇒ Object



85
86
87
88
89
# File 'lib/stax/aws/cfn.rb', line 85

def outputs(name)
  describe(name).outputs.each_with_object(HashWithIndifferentAccess.new) do |o, h|
    h[o.output_key] = o.output_value
  end
end

.parameters(name) ⇒ Object



69
70
71
# File 'lib/stax/aws/cfn.rb', line 69

def parameters(name)
  client.describe_stacks(stack_name: name).stacks.first.parameters
end

.protection(name, enable) ⇒ Object



131
132
133
# File 'lib/stax/aws/cfn.rb', line 131

def protection(name, enable)
  client.update_termination_protection(stack_name: name, enable_termination_protection: enable)
end

.resources(name) ⇒ Object



47
48
49
50
51
# File 'lib/stax/aws/cfn.rb', line 47

def resources(name)
  paginate(:stack_resource_summaries) do |token|
    client.list_stack_resources(stack_name: name, next_token: token)
  end
end

.resources_by_type(name, type) ⇒ Object



53
54
55
56
57
# File 'lib/stax/aws/cfn.rb', line 53

def resources_by_type(name, type)
  resources(name).select do |r|
    r.resource_type == type
  end
end

.set_policy(opt) ⇒ Object



139
140
141
# File 'lib/stax/aws/cfn.rb', line 139

def set_policy(opt)
  client.set_stack_policy(opt)
end

.stacksObject



37
38
39
40
41
# File 'lib/stax/aws/cfn.rb', line 37

def stacks
  paginate(:stack_summaries) do |token|
    client.list_stacks(stack_status_filter: STATUSES, next_token: token)
  end
end

.template(name) ⇒ Object



43
44
45
# File 'lib/stax/aws/cfn.rb', line 43

def template(name)
  client.get_template(stack_name: name).template_body
end

.update(opt) ⇒ Object



119
120
121
# File 'lib/stax/aws/cfn.rb', line 119

def update(opt)
  client.update_stack(opt)&.stack_id
end

.validate(opt) ⇒ Object



111
112
113
# File 'lib/stax/aws/cfn.rb', line 111

def validate(opt)
  client.validate_template(opt)
end