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,
}

Constants inherited from Sdk

Sdk::RETRY_LIMIT

Class Method Summary collapse

Methods inherited from Sdk

paginate

Class Method Details

.cancel(name) ⇒ Object



121
122
123
# File 'lib/stax/aws/cfn.rb', line 121

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

.changes(opt) ⇒ Object



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

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

.changeset(opt) ⇒ Object



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

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(retry_limit: Stax::Aws::Sdk::RETRY_LIMIT)
end

.create(opt) ⇒ Object



109
110
111
# File 'lib/stax/aws/cfn.rb', line 109

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

.delete(name) ⇒ Object



117
118
119
# File 'lib/stax/aws/cfn.rb', line 117

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

.describe(name) ⇒ Object



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

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

.detect_drift(opt) ⇒ Object



157
158
159
# File 'lib/stax/aws/cfn.rb', line 157

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

.drift_status(id) ⇒ Object



161
162
163
# File 'lib/stax/aws/cfn.rb', line 161

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

.drifts(opt) ⇒ Object



165
166
167
# File 'lib/stax/aws/cfn.rb', line 165

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

.events(name) ⇒ Object



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

def events(name)
  client.describe_stack_events(stack_name: name).map(&:stack_events).flatten
end

.execute(opt) ⇒ Object



153
154
155
# File 'lib/stax/aws/cfn.rb', line 153

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

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.exports(name) ⇒ Object

list of this stack output exports



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

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

.get_policy(opt) ⇒ Object



129
130
131
# File 'lib/stax/aws/cfn.rb', line 129

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

.id(name, id) ⇒ Object



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

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



95
96
97
98
99
100
101
102
103
# File 'lib/stax/aws/cfn.rb', line 95

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



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

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



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

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

.outputs(name) ⇒ Object



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

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



63
64
65
# File 'lib/stax/aws/cfn.rb', line 63

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

.protection(name, enable) ⇒ Object



125
126
127
# File 'lib/stax/aws/cfn.rb', line 125

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

.resources(name) ⇒ Object



45
46
47
# File 'lib/stax/aws/cfn.rb', line 45

def resources(name)
  client.list_stack_resources(stack_name: name).map(&:stack_resource_summaries).flatten
end

.resources_by_type(name, type) ⇒ Object



49
50
51
52
53
# File 'lib/stax/aws/cfn.rb', line 49

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

.set_policy(opt) ⇒ Object



133
134
135
# File 'lib/stax/aws/cfn.rb', line 133

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

.stacksObject



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

def stacks
  client.list_stacks(stack_status_filter: STATUSES).map(&:stack_summaries).flatten
end

.template(name) ⇒ Object



41
42
43
# File 'lib/stax/aws/cfn.rb', line 41

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

.update(opt) ⇒ Object



113
114
115
# File 'lib/stax/aws/cfn.rb', line 113

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

.validate(opt) ⇒ Object



105
106
107
# File 'lib/stax/aws/cfn.rb', line 105

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