Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Exclude tags #18

Merged
merged 2 commits into from
Oct 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Usage: piculet [options]
-f, --file FILE
-n, --names SG_LIST
-x, --exclude SG_LIST
-t, --exclude_tag TAGS
--ec2s VPC_IDS
--dry-run
-e, --export
Expand Down
1 change: 1 addition & 0 deletions bin/piculet
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ARGV.options do |opt|
opt.on('-f', '--file FILE') {|v| file = v }
opt.on('-n', '--names SG_LIST', Array) {|v| options[:sg_names] = v }
opt.on('-x', '--exclude SG_LIST', Array) {|v| options[:exclude_sgs] = v }
opt.on('-t', '--exclude_tag TAGS', Array) {|v| options[:exclude_tags] = v }
opt.on('', '--ec2s VPC_IDS', Array) {|v| options[:ec2s] = v }
opt.on('', '--dry-run') {|v| options[:dry_run] = true }
opt.on('-e', '--export') {|v| mode = :export }
Expand Down
42 changes: 14 additions & 28 deletions lib/piculet/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ def apply(file)
AWS.memoize { walk(file) }
end

def should_skip(sg_name, sg)
# Name
if @options.sg_names
return true unless @options.sg_names.include?(sg_name)
end
return true if @options.exclude_sgs and @options.exclude_sgs.any? {|regex| sg_name =~ regex}
# Tag
return true if sg and @options.exclude_tags and @options.exclude_tags.any? {|tagname| !sg.tags[tagname].to_s.empty?}
end

def export(options = {})
exported = AWS.memoize do
Exporter.export(@options.ec2, @options_hash.merge(options))
Expand Down Expand Up @@ -98,13 +108,7 @@ def walk_ec2(vpc, ec2_dsl, ec2_aws, collection_api)
sg_list_dsl.each do |key, sg_dsl|
name = key[0]

if @options.sg_names
next unless @options.sg_names.include?(name)
end

if @options.exclude_sgs
next if @options.exclude_sgs.any? {|regex| name =~ regex}
end
next if should_skip(name,sg_list_aws[key])

sg_aws = sg_list_aws[key]

Expand All @@ -122,13 +126,7 @@ def walk_ec2(vpc, ec2_dsl, ec2_aws, collection_api)
sg_list_dsl.each do |key, sg_dsl|
name = key[0]

if @options.sg_names
next unless @options.sg_names.include?(name)
end

if @options.exclude_sgs
next if @options.exclude_sgs.any? {|regex| name =~ regex}
end
next if should_skip(name,sg_list_aws[key])

sg_aws = sg_list_aws.delete(key)
walk_security_group(sg_dsl, sg_aws)
Expand All @@ -137,13 +135,7 @@ def walk_ec2(vpc, ec2_dsl, ec2_aws, collection_api)
sg_list_aws.each do |key, sg_aws|
name = key[0]

if @options.sg_names
next unless @options.sg_names.include?(name)
end

if @options.exclude_sgs
next if @options.exclude_sgs.any? {|regex| name =~ regex}
end
next if should_skip(name,sg_list_aws[key])

sg_aws.ingress_ip_permissions.each {|i| i.delete }
sg_aws.egress_ip_permissions.each {|i| i.delete } if vpc
Expand All @@ -152,13 +144,7 @@ def walk_ec2(vpc, ec2_dsl, ec2_aws, collection_api)
sg_list_aws.each do |key, sg_aws|
name = key[0]

if @options.sg_names
next unless @options.sg_names.include?(name)
end

if @options.exclude_sgs
next if @options.exclude_sgs.any? {|regex| name =~ regex}
end
next if should_skip(name,sg_list_aws[key])

sg_aws.delete
end
Expand Down