Skip to content

Commit

Permalink
Allow specifying exclude option in a DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
sorah committed Sep 21, 2018
1 parent 726b890 commit 03242d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/miam/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ class Miam::Client
include Miam::Logger::Helper

def initialize(options = {})
@options = {:format => :ruby}.merge(options)
@options = {
format: :ruby,
exclude: []
}.merge(options)
aws_config = options.delete(:aws_config) || {}
@iam = Aws::IAM::Client.new(aws_config)
@sts = Aws::STS::Client.new(aws_config)
Expand Down Expand Up @@ -58,6 +61,7 @@ def apply(file)

def walk(file)
expected = load_file(file)
@options[:exclude] += expected[:exclude]

actual, group_users, instance_profile_roles = Miam::Exporter.export(@iam, @options)
updated = pre_walk_managed_policies(expected[:policies], actual[:policies])
Expand Down Expand Up @@ -539,7 +543,7 @@ def target_matched?(name)
result = true

if @options[:exclude]
result &&= @options[:exclude].all? {|r| name !~ r}
result &&= @options[:exclude].all? {|r| name !~ r }
end

if @options[:target]
Expand Down
6 changes: 5 additions & 1 deletion lib/miam/dsl/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.eval(dsl, path, options = {})
def initialize(path, options = {}, &block)
@path = path
@options = options
@result = {:users => {}, :groups => {}, :roles => {}, :instance_profiles => {}, :policies => {}}
@result = {:users => {}, :groups => {}, :roles => {}, :instance_profiles => {}, :policies => {}, :exclude => []}

@context = Hashie::Mash.new(
:path => path,
Expand Down Expand Up @@ -41,6 +41,10 @@ def require(file)
end
end

def exclude(pattern)
@result[:exclude] << pattern
end

def user(name, user_options = {}, &block)
name = name.to_s

Expand Down
15 changes: 15 additions & 0 deletions spec/miam/exclude_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,19 @@
expect(updated).to be_falsey
end
end

context 'when specifying exclude option in a DSL' do
let(:exclude_everything) do
<<-RUBY
exclude /.*/
RUBY
end

subject { client(exclude: []) }

it do
updated = apply(subject) { exclude_everything }
expect(updated).to be_falsey
end
end
end

0 comments on commit 03242d0

Please sign in to comment.