Skip to content

Commit

Permalink
WIP: add the ability to only generate data for objects matching confi…
Browse files Browse the repository at this point in the history
…gurable patterns.
  • Loading branch information
iragsdale committed Jun 25, 2015
1 parent 4c25769 commit d82159c
Show file tree
Hide file tree
Showing 28 changed files with 277 additions and 111 deletions.
4 changes: 4 additions & 0 deletions lib/terraforming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
require "terraforming/util"
require "terraforming/version"

require "terraforming/matcher/default_matcher.rb"
require "terraforming/matcher/property_matcher.rb"
require "terraforming/matcher/compound_matcher.rb"

require "terraforming/cli"
require "terraforming/resource/db_parameter_group"
require "terraforming/resource/db_security_group"
Expand Down
16 changes: 11 additions & 5 deletions lib/terraforming/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Terraforming
class CLI < Thor
class_option :merge, type: :string, desc: "tfstate file to merge"
class_option :tfstate, type: :boolean, desc: "Generate tfstate"
class_option :limit, type: :array, banner: "image_id=ami-xxxx placement.availability_zone=us-east-1b tags=~name", desc: "List matching resources. Arguments using =~ match using regular expressions."

desc "dbpg", "Database Parameter Group"
def dbpg
Expand Down Expand Up @@ -111,20 +112,25 @@ def vpc
private

def execute(klass, options)
matcher = if options[:limit]
Terraforming::Matcher::CompoundMatcher.new(options[:limit])
else
Terraforming::Matcher::DefaultMatcher.new
end
result = if options[:tfstate]
tfstate(klass, options[:merge])
tfstate(klass, options[:merge], matcher)
else
klass.tf
klass.tf(matcher: matcher)
end

puts result
end

def tfstate(klass, tfstate_path)
return klass.tfstate unless tfstate_path
def tfstate(klass, tfstate_path, matcher)
return klass.tfstate(matcher: matcher) unless tfstate_path

tfstate_base = JSON.parse(open(tfstate_path).read)
klass.tfstate(tfstate_base: tfstate_base)
klass.tfstate(tfstate_base: tfstate_base, matcher: matcher)
end
end
end
15 changes: 15 additions & 0 deletions lib/terraforming/matcher/compound_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Terraforming
module Matcher
# matches a set of properties
class CompoundMatcher
def initialize(patterns)
@matchers = patterns.map{|p| Matcher::PropertyMatcher.new(p)}
end

def match(anything)
return false unless @matchers
return @matchers.inject(true){|result, matcher| result && matcher.match(anything) }
end
end
end
end
10 changes: 10 additions & 0 deletions lib/terraforming/matcher/default_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Terraforming
module Matcher
# matches anything by default
class DefaultMatcher
def match(anything)
true
end
end
end
end
42 changes: 42 additions & 0 deletions lib/terraforming/matcher/property_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Terraforming
module Matcher
# matches a particular property
class PropertyMatcher
# string in the form of key=value or key=~value
# if the latter, uses a regular expression match instead of a string equals
def initialize(definition)
@key, @condition = definition.split("=", 2)
if @condition =~ /^~/
@condition = Regexp.new(@condition[1..@condition.length])
end
end

# returns true if the property's value for key matches the given value, false otherwise
def match(anything)
return false unless anything
case @condition
when String
return resolve(anything).to_s == @condition
when Regexp
return nil != (resolve(anything).to_s =~ @condition)
else
return false
end
end

def resolve(anything)
PropertyMatcher.resolve(anything, @key)
end

def self.resolve(anything, key)
first, rest = key.split(".", 2)
val = anything.method(first.to_sym).call rescue nil
if rest
return resolve(val, rest)
else
return val.to_s
end
end
end
end
end
11 changes: 6 additions & 5 deletions lib/terraforming/resource/db_parameter_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class DBParameterGroup
include Terraforming::Util

def self.tf(client: Aws::RDS::Client.new)
self.new(client).tf
def self.tf(client: Aws::RDS::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
11 changes: 6 additions & 5 deletions lib/terraforming/resource/db_security_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class DBSecurityGroup
include Terraforming::Util

def self.tf(client: Aws::RDS::Client.new)
self.new(client).tf
def self.tf(client: Aws::RDS::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
11 changes: 6 additions & 5 deletions lib/terraforming/resource/db_subnet_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class DBSubnetGroup
include Terraforming::Util

def self.tf(client: Aws::RDS::Client.new)
self.new(client).tf
def self.tf(client: Aws::RDS::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
13 changes: 7 additions & 6 deletions lib/terraforming/resource/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class EC2
include Terraforming::Util

def self.tf(client: Aws::EC2::Client.new)
self.new(client).tf
def self.tf(client: Aws::EC2::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::EC2::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::EC2::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down Expand Up @@ -60,7 +61,7 @@ def tfstate(tfstate_base)
private

def instances
@client.describe_instances.reservations.map(&:instances).flatten
@client.describe_instances.reservations.map(&:instances).flatten.select{|i| matcher.match(i) }
end

def module_name_of(instance)
Expand Down
11 changes: 6 additions & 5 deletions lib/terraforming/resource/elb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class ELB
include Terraforming::Util

def self.tf(client: Aws::ElasticLoadBalancing::Client.new)
self.new(client).tf
def self.tf(client: Aws::ElasticLoadBalancing::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::ElasticLoadBalancing::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::ElasticLoadBalancing::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
11 changes: 6 additions & 5 deletions lib/terraforming/resource/iam_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class IAMGroup
include Terraforming::Util

def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
def self.tf(client: Aws::IAM::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
11 changes: 6 additions & 5 deletions lib/terraforming/resource/iam_group_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class IAMGroupPolicy
include Terraforming::Util

def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
def self.tf(client: Aws::IAM::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
11 changes: 6 additions & 5 deletions lib/terraforming/resource/iam_instance_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class IAMInstanceProfile
include Terraforming::Util

def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
def self.tf(client: Aws::IAM::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
11 changes: 6 additions & 5 deletions lib/terraforming/resource/iam_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class IAMPolicy
include Terraforming::Util

def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
def self.tf(client: Aws::IAM::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
11 changes: 6 additions & 5 deletions lib/terraforming/resource/iam_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class IAMRole
include Terraforming::Util

def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
def self.tf(client: Aws::IAM::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
11 changes: 6 additions & 5 deletions lib/terraforming/resource/iam_role_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ module Resource
class IAMRolePolicy
include Terraforming::Util

def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
def self.tf(client: Aws::IAM::Client.new, matcher: nil)
self.new(client, matcher:matcher).tf
end

def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil, matcher: nil)
self.new(client, matcher:matcher).tfstate(tfstate_base)
end

def initialize(client)
def initialize(client, matcher: nil)
@client = client
@matcher = matcher
end

def tf
Expand Down
Loading

0 comments on commit d82159c

Please sign in to comment.