diff --git a/README.md b/README.md index 430156b37..3229e0d89 100644 --- a/README.md +++ b/README.md @@ -112,9 +112,9 @@ $ awspec generate ec2 vpc-ab123cde >> spec/ec2_spec.rb ### How to add new resource type (ex. Redshift resource) 1. Create your feature branch (`git checkout -b add-type-redshift`) -2. Generate template files (`bundle exec awspec/bin/toolbox template redshift`) +2. Generate template files (`bundle exec ./lib/awspec/bin/toolbox template redshift`) 3. Fill files with code. -4. Generate [doc/resource_types.md](doc/resource_types.md) (`bundle exec awspec/bin/toolbox docgen > doc/resource_type.md`) +4. Generate [doc/resource_types.md](doc/resource_types.md) (`bundle exec ./lib/awspec/bin/toolbox docgen > doc/resource_type.md`) 5. Run test (`bundle exec rake spec`) 6. Push to the branch (`git push origin add-type-redshift`) 7. Create a new Pull Request diff --git a/doc/resource_types.md b/doc/resource_types.md index 2f1871e0b..df5a277bd 100644 --- a/doc/resource_types.md +++ b/doc/resource_types.md @@ -12,6 +12,7 @@ | [route_table](#route_table) | [ebs](#ebs) | [elb](#elb) +| [lambda](#lambda) ## ec2 @@ -256,3 +257,12 @@ end ### belong_to_vpc #### its(:health_check_target), its(:health_check_interval), its(:health_check_timeout), its(:health_check_unhealthy_threshold), its(:health_check_healthy_threshold), its(:load_balancer_name), its(:dns_name), its(:canonical_hosted_zone_name), its(:canonical_hosted_zone_name_id), its(:vpc_id), its(:created_time), its(:scheme) +## lambda + +Lambda resource type. + +### exist + +### have_event_source + +#### its(:function_name), its(:function_arn), its(:runtime), its(:role), its(:handler), its(:code_size), its(:description), its(:timeout), its(:memory_size), its(:last_modified) diff --git a/lib/awspec/generator/doc/type/lambda.rb b/lib/awspec/generator/doc/type/lambda.rb new file mode 100644 index 000000000..3512dccb2 --- /dev/null +++ b/lib/awspec/generator/doc/type/lambda.rb @@ -0,0 +1,17 @@ +module Awspec::Generator + module Doc + module Type + class Lambda < Base + def initialize + super + @type_name = 'Lambda' + @type = Awspec::Type::Lambda.new('my-lambda-function-name') + @ret = @type.function + @matchers = [] + @ignore_matchers = [] + @describes = [] + end + end + end + end +end diff --git a/lib/awspec/helper/finder.rb b/lib/awspec/helper/finder.rb index 1156dfba2..1839798a3 100644 --- a/lib/awspec/helper/finder.rb +++ b/lib/awspec/helper/finder.rb @@ -8,6 +8,7 @@ require 'awspec/helper/finder/auto_scaling' require 'awspec/helper/finder/ebs' require 'awspec/helper/finder/elb' +require 'awspec/helper/finder/lambda' module Awspec::Helper module Finder @@ -21,6 +22,7 @@ module Finder include Awspec::Helper::Finder::AutoScaling include Awspec::Helper::Finder::Ebs include Awspec::Helper::Finder::Elb + include Awspec::Helper::Finder::Lambda # rubocop:disable all def initialize(id = nil) @@ -30,6 +32,7 @@ def initialize(id = nil) @s3_client = Aws::S3::Client.new @auto_scaling_client = Aws::AutoScaling::Client.new @elb_client = Aws::ElasticLoadBalancing::Client.new + @lambda_client = Aws::Lambda::Client.new end end end diff --git a/lib/awspec/helper/finder/lambda.rb b/lib/awspec/helper/finder/lambda.rb new file mode 100644 index 000000000..114dc75db --- /dev/null +++ b/lib/awspec/helper/finder/lambda.rb @@ -0,0 +1,31 @@ +module Awspec::Helper + module Finder + module Lambda + def find_lambda(id) + functions = [] + marker = nil + loop do + res = @lambda_client.list_functions( + marker: marker + ) + marker = res.next_marker + break if res.functions.empty? + res.functions.each do |function| + if function.function_name == id || function.function_arn == id + functions.push(function) + end + end + break unless marker + end + return functions[0] if functions.count == 1 + end + + def select_event_source_by_function_arn(function_arn) + res = @lambda_client.list_event_source_mappings({ + function_name: function_arn + }) + res[:event_source_mappings] + end + end + end +end diff --git a/lib/awspec/helper/type.rb b/lib/awspec/helper/type.rb index 8a0923eca..c2f399d34 100644 --- a/lib/awspec/helper/type.rb +++ b/lib/awspec/helper/type.rb @@ -4,7 +4,7 @@ module Type TYPES = %w( base ec2 rds rds_db_parameter_group security_group vpc s3 route53_hosted_zone auto_scaling_group subnet - route_table ebs elb + route_table ebs elb lambda ) TYPES.each do |type| diff --git a/lib/awspec/stub/lambda.rb b/lib/awspec/stub/lambda.rb new file mode 100644 index 000000000..afb36e609 --- /dev/null +++ b/lib/awspec/stub/lambda.rb @@ -0,0 +1,18 @@ +Aws.config[:lambda] = { + stub_responses: { + list_functions: { + functions: [ + { + function_name: 'my-lambda-function-name', + function_arn: 'arn:aws:lambda:us-east-1:123456789012:function:my-lambda-function-name', + runtime: 'nodejs', + timeout: 5, + memory_size: 256 + } + ] + }, + list_event_source_mappings: { + # @todo + } + } +} diff --git a/lib/awspec/type/lambda.rb b/lib/awspec/type/lambda.rb new file mode 100644 index 000000000..764ab326b --- /dev/null +++ b/lib/awspec/type/lambda.rb @@ -0,0 +1,31 @@ +module Awspec::Type + class Lambda < Base + attr_reader :function + + def initialize(id) + super + @function = find_lambda(id) + @id = @function[:function_arn] if @function + end + + def method_missing(name) + describe = name.to_sym + if @function.members.include?(describe) + @function[describe] + else + super + end + end + + def timeout + @function[:timeout] + end + + def has_event_source?(event_source_arn) + sources = select_event_source_by_function_arn(@id) + sources.find do |source| + source.event_source_arn == event_source_arn + end + end + end +end diff --git a/spec/type/lambda_spec.rb b/spec/type/lambda_spec.rb new file mode 100644 index 000000000..fbd65f369 --- /dev/null +++ b/spec/type/lambda_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' +Awspec::Stub.load 'lambda' + +describe lambda('my-lambda-function-name') do + it { should exist } + its(:function_name) { should eq 'my-lambda-function-name' } + its(:runtime) { should eq 'nodejs' } + its(:timeout) { should eq 5 } + its(:memory_size) { should eq 256 } +end + +describe lambda('not-exist-function') do + it { should_not exist } +end