Skip to content

Commit

Permalink
Merge pull request #32 from k1LoW/add-type-lambda
Browse files Browse the repository at this point in the history
Add lambda type
  • Loading branch information
k1LoW committed Aug 27, 2015
2 parents beb53ef + 61caa9a commit 11b8a16
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions doc/resource_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
| [route_table](#route_table)
| [ebs](#ebs)
| [elb](#elb)
| [lambda](#lambda)

## <a name="ec2">ec2</a>

Expand Down Expand Up @@ -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)
## <a name="lambda">lambda</a>

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)
17 changes: 17 additions & 0 deletions lib/awspec/generator/doc/type/lambda.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lib/awspec/helper/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
31 changes: 31 additions & 0 deletions lib/awspec/helper/finder/lambda.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/awspec/helper/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
18 changes: 18 additions & 0 deletions lib/awspec/stub/lambda.rb
Original file line number Diff line number Diff line change
@@ -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
}
}
}
31 changes: 31 additions & 0 deletions lib/awspec/type/lambda.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions spec/type/lambda_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 11b8a16

Please sign in to comment.