Skip to content

Commit bdea1f9

Browse files
committed
Add ignore_resource_types to drift command for unsupported CF resource types
1 parent 113eb8a commit bdea1f9

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

lib/stack_master/commands/drift.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ def perform
1919
stack_drift_status_color(drift_results.stack_drift_status))
2020
return if drift_results.stack_drift_status == 'IN_SYNC'
2121

22-
failed
23-
2422
resp = cf.describe_stack_resource_drifts(stack_name: stack_name)
23+
actionable_drifts = resp.stack_resource_drifts.reject do |drift|
24+
ignore_resource_types.include?(drift.resource_type)
25+
end
26+
27+
failed unless actionable_drifts.empty?
28+
2529
resp.stack_resource_drifts.each do |drift|
2630
display_drift(drift)
2731
end
@@ -108,7 +112,7 @@ def puts(string)
108112
end
109113

110114
extend Forwardable
111-
def_delegators :@stack_definition, :stack_name, :region
115+
def_delegators :@stack_definition, :stack_name, :region, :ignore_resource_types
112116
def_delegators :StackMaster, :colorize
113117

114118
SLEEP_SECONDS = 1

lib/stack_master/stack_definition.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class StackDefinition
1919
:compiler_options,
2020
:parameters_dir,
2121
:parameters,
22-
:parameter_files
22+
:parameter_files,
23+
:ignore_resource_types
2324

2425
attr_reader :compiler
2526

@@ -41,6 +42,7 @@ def initialize(attributes = {})
4142
@allowed_accounts = Array(@allowed_accounts)
4243
@parameters ||= {}
4344
@parameter_files ||= []
45+
@ignore_resource_types ||= []
4446
end
4547

4648
def ==(other)

spec/stack_master/commands/drift_spec.rb

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
let(:cf) { instance_double(Aws::CloudFormation::Client) }
33
let(:config) { instance_double(StackMaster::Config) }
44
let(:options) { Commander::Command::Options.new }
5-
let(:stack_definition) { instance_double(StackMaster::StackDefinition, stack_name: 'myapp', region: 'us-east-1') }
5+
let(:ignore_resource_types) { [] }
6+
let(:stack_definition) { instance_double(StackMaster::StackDefinition, stack_name: 'myapp', region: 'us-east-1', ignore_resource_types: ignore_resource_types) }
67

78
subject(:drift) { described_class.new(config, stack_definition, options) }
89
let(:stack_drift_detection_id) { 123 }
@@ -118,6 +119,59 @@
118119
end
119120
end
120121

122+
context 'when all drifted resources are in the ignore list' do
123+
let(:stack_drift_status) { 'DRIFTED' }
124+
let(:ignore_resource_types) { ['AWS::ElastiCache::ParameterGroup'] }
125+
let(:stack_resource_drifts) do
126+
[
127+
Aws::CloudFormation::Types::StackResourceDrift.new(
128+
stack_resource_drift_status: 'MODIFIED',
129+
resource_type: 'AWS::ElastiCache::ParameterGroup',
130+
logical_resource_id: 'CacheParams',
131+
physical_resource_id: 'params-1',
132+
property_differences: []
133+
)
134+
]
135+
end
136+
137+
it 'exits with success' do
138+
drift.perform
139+
expect(drift).to be_success
140+
end
141+
142+
it 'still displays the drift for visibility' do
143+
expect { drift.perform }.to output(/MODIFIED AWS::ElastiCache::ParameterGroup CacheParams params-1/).to_stdout
144+
end
145+
end
146+
147+
context 'when only some drifted resources are in the ignore list' do
148+
let(:stack_drift_status) { 'DRIFTED' }
149+
let(:ignore_resource_types) { ['AWS::ElastiCache::ParameterGroup'] }
150+
let(:stack_resource_drifts) do
151+
[
152+
Aws::CloudFormation::Types::StackResourceDrift.new(
153+
stack_resource_drift_status: 'MODIFIED',
154+
resource_type: 'AWS::ElastiCache::ParameterGroup',
155+
logical_resource_id: 'CacheParams',
156+
physical_resource_id: 'params-1',
157+
property_differences: []
158+
),
159+
Aws::CloudFormation::Types::StackResourceDrift.new(
160+
stack_resource_drift_status: 'MODIFIED',
161+
resource_type: 'AWS::EC2::SecurityGroup',
162+
logical_resource_id: 'SecurityGroup',
163+
physical_resource_id: 'sg-123456',
164+
property_differences: [property_difference]
165+
)
166+
]
167+
end
168+
169+
it 'exits with failure' do
170+
drift.perform
171+
expect(drift).not_to be_success
172+
end
173+
end
174+
121175
context "when stack drift detection doesn't complete" do
122176
before do
123177
describe_stack_drift_detection_status_response.detection_status = 'UNKNOWN'

0 commit comments

Comments
 (0)