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
  IMPORT_IN_PROGRESS IMPORT_COMPLETE IMPORT_ROLLBACK_IN_PROGRESS IMPORT_ROLLBACK_FAILED IMPORT_ROLLBACK_COMPLETE
  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,
  IMPORT_COMPLETE:      :green,
  CREATE_FAILED:        :red,
  DELETE_FAILED:        :red,
  UPDATE_FAILED:        :red,
  ROLLBACK_IN_PROGRESS: :red,
  ROLLBACK_COMPLETE:    :red,
  ## resource action
  Add:    :green,
  Import: :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



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

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

.changes(opt) ⇒ Object



146
147
148
149
150
# File 'lib/stax/aws/cfn.rb', line 146

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

.changeset(opt) ⇒ Object



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

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

.clientObject



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

def client
  @_client ||= ::Aws::CloudFormation::Client.new(retry_limit: Stax::Aws::Sdk::RETRY_LIMIT)
end

.create(opt) ⇒ Object



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

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

.delete(name) ⇒ Object



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

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

.describe(name) ⇒ Object



70
71
72
73
74
# File 'lib/stax/aws/cfn.rb', line 70

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

.detect_drift(opt) ⇒ Object



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

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

.drift_status(id) ⇒ Object



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

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

.drifts(opt) ⇒ Object



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

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

.events(name) ⇒ Object



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

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

.execute(opt) ⇒ Object



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

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

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/stax/aws/cfn.rb', line 76

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

.exports(name) ⇒ Object

list of this stack output exports



93
94
95
# File 'lib/stax/aws/cfn.rb', line 93

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

.get_policy(opt) ⇒ Object



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

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

.id(name, id) ⇒ Object



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

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



98
99
100
101
102
103
104
105
106
# File 'lib/stax/aws/cfn.rb', line 98

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



140
141
142
143
144
# File 'lib/stax/aws/cfn.rb', line 140

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



88
89
90
# File 'lib/stax/aws/cfn.rb', line 88

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

.outputs(name) ⇒ Object



82
83
84
85
86
# File 'lib/stax/aws/cfn.rb', line 82

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



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

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

.protection(name, enable) ⇒ Object



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

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

.resources(name) ⇒ Object



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

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

.resources_by_type(name, type) ⇒ Object



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

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

.set_policy(opt) ⇒ Object



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

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

.stacksObject



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

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

.template(name) ⇒ Object



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

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

.update(opt) ⇒ Object



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

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

.validate(opt) ⇒ Object



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

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