From 85627630bd3b06d3f5e003f1cbea0df355b16580 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Fri, 27 Nov 2015 15:23:45 -0800 Subject: [PATCH] New resource (AWS provider) - aws_lambda_event_source_mapping --- builtin/providers/aws/provider.go | 1 + ...esource_aws_lambda_event_source_mapping.go | 171 +++++++++++ ...ce_aws_lambda_event_source_mapping_test.go | 279 ++++++++++++++++++ .../lambda_event_source_mapping.html.markdown | 47 +++ 4 files changed, 498 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_lambda_event_source_mapping.go create mode 100644 builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go create mode 100644 website/source/docs/providers/aws/r/lambda_event_source_mapping.html.markdown diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index ba627d2ec..c123cc184 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -223,6 +223,7 @@ func Provider() terraform.ResourceProvider { "aws_kinesis_firehose_delivery_stream": resourceAwsKinesisFirehoseDeliveryStream(), "aws_kinesis_stream": resourceAwsKinesisStream(), "aws_lambda_function": resourceAwsLambdaFunction(), + "aws_lambda_event_source_mapping": resourceAwsLambdaEventSourceMapping(), "aws_launch_configuration": resourceAwsLaunchConfiguration(), "aws_lb_cookie_stickiness_policy": resourceAwsLBCookieStickinessPolicy(), "aws_main_route_table_association": resourceAwsMainRouteTableAssociation(), diff --git a/builtin/providers/aws/resource_aws_lambda_event_source_mapping.go b/builtin/providers/aws/resource_aws_lambda_event_source_mapping.go new file mode 100644 index 000000000..70ca3a01c --- /dev/null +++ b/builtin/providers/aws/resource_aws_lambda_event_source_mapping.go @@ -0,0 +1,171 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/lambda" + + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsLambdaEventSourceMapping() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsLambdaEventSourceMappingCreate, + Read: resourceAwsLambdaEventSourceMappingRead, + Update: resourceAwsLambdaEventSourceMappingUpdate, + Delete: resourceAwsLambdaEventSourceMappingDelete, + + Schema: map[string]*schema.Schema{ + "event_source_arn": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "function_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "starting_position": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "batch_size": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 100, + }, + "enabled": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + "function_arn": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "last_modified": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "last_processing_result": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "state": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "state_transition_reason": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "uuid": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +// resourceAwsLambdaEventSourceMappingCreate maps to: +// CreateEventSourceMapping in the API / SDK +func resourceAwsLambdaEventSourceMappingCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).lambdaconn + + functionName := d.Get("function_name").(string) + eventSourceArn := d.Get("event_source_arn").(string) + + log.Printf("[DEBUG] Creating Lambda event source mapping: source %s to function %s", eventSourceArn, functionName) + + params := &lambda.CreateEventSourceMappingInput{ + EventSourceArn: aws.String(eventSourceArn), + FunctionName: aws.String(functionName), + StartingPosition: aws.String(d.Get("starting_position").(string)), + BatchSize: aws.Int64(int64(d.Get("batch_size").(int))), + Enabled: aws.Bool(d.Get("enabled").(bool)), + } + + eventSourceMappingConfiguration, err := conn.CreateEventSourceMapping(params) + if err != nil { + return fmt.Errorf("Error creating Lambda event source mapping: %s", err) + } + + d.Set("uuid", eventSourceMappingConfiguration.UUID) + d.SetId(*eventSourceMappingConfiguration.UUID) + + return resourceAwsLambdaEventSourceMappingRead(d, meta) +} + +// resourceAwsLambdaEventSourceMappingRead maps to: +// GetEventSourceMapping in the API / SDK +func resourceAwsLambdaEventSourceMappingRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).lambdaconn + + log.Printf("[DEBUG] Fetching Lambda event source mapping: %s", d.Id()) + + params := &lambda.GetEventSourceMappingInput{ + UUID: aws.String(d.Id()), + } + + eventSourceMappingConfiguration, err := conn.GetEventSourceMapping(params) + if err != nil { + return err + } + + d.Set("batch_size", eventSourceMappingConfiguration.BatchSize) + d.Set("event_source_arn", eventSourceMappingConfiguration.EventSourceArn) + d.Set("function_arn", eventSourceMappingConfiguration.FunctionArn) + d.Set("last_modified", eventSourceMappingConfiguration.LastModified) + d.Set("last_processing_result", eventSourceMappingConfiguration.LastProcessingResult) + d.Set("state", eventSourceMappingConfiguration.State) + d.Set("state_transition_reason", eventSourceMappingConfiguration.StateTransitionReason) + d.Set("uuid", eventSourceMappingConfiguration.UUID) + + return nil +} + +// resourceAwsLambdaEventSourceMappingDelete maps to: +// DeleteEventSourceMapping in the API / SDK +func resourceAwsLambdaEventSourceMappingDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).lambdaconn + + log.Printf("[INFO] Deleting Lambda event source mapping: %s", d.Id()) + + params := &lambda.DeleteEventSourceMappingInput{ + UUID: aws.String(d.Id()), + } + + _, err := conn.DeleteEventSourceMapping(params) + if err != nil { + return fmt.Errorf("Error deleting Lambda event source mapping: %s", err) + } + + d.SetId("") + + return nil +} + +// resourceAwsLambdaEventSourceMappingUpdate maps to: +// UpdateEventSourceMapping in the API / SDK +func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).lambdaconn + + log.Printf("[DEBUG] Updating Lambda event source mapping: %s", d.Id()) + + params := &lambda.UpdateEventSourceMappingInput{ + UUID: aws.String(d.Id()), + BatchSize: aws.Int64(int64(d.Get("batch_size").(int))), + FunctionName: aws.String(d.Get("function_name").(string)), + Enabled: aws.Bool(d.Get("enabled").(bool)), + } + + _, err := conn.UpdateEventSourceMapping(params) + if err != nil { + return fmt.Errorf("Error updating Lambda event source mapping: %s", err) + } + + return resourceAwsLambdaEventSourceMappingRead(d, meta) +} diff --git a/builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go b/builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go new file mode 100644 index 000000000..59fe5b56e --- /dev/null +++ b/builtin/providers/aws/resource_aws_lambda_event_source_mapping_test.go @@ -0,0 +1,279 @@ +package aws + +import ( + "fmt" + "regexp" + "strconv" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/lambda" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSLambdaEventSourceMapping_basic(t *testing.T) { + var conf lambda.EventSourceMappingConfiguration + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSLambdaEventSourceMappingConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), + testAccCheckAWSLambdaEventSourceMappingAttributes(&conf), + ), + }, + resource.TestStep{ + Config: testAccAWSLambdaEventSourceMappingConfigUpdate, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaEventSourceMappingExists("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", &conf), + resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", + "batch_size", + strconv.Itoa(200)), + resource.TestCheckResourceAttr("aws_lambda_event_source_mapping.lambda_event_source_mapping_test", + "enabled", + strconv.FormatBool(false)), + resource.TestMatchResourceAttr( + "aws_lambda_event_source_mapping.lambda_event_source_mapping_test", + "function_arn", + regexp.MustCompile("example_lambda_name_update$"), + ), + ), + }, + }, + }) +} + +func testAccCheckLambdaEventSourceMappingDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).lambdaconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_lambda_event_source_mapping" { + continue + } + + _, err := conn.GetEventSourceMapping(&lambda.GetEventSourceMappingInput{ + UUID: aws.String(rs.Primary.ID), + }) + + if err == nil { + return fmt.Errorf("Lambda event source mapping was not deleted") + } + + } + + return nil + +} + +func testAccCheckAwsLambdaEventSourceMappingExists(n string, mapping *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { + // Wait for IAM role + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Lambda event source mapping not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("Lambda event source mapping ID not set") + } + + conn := testAccProvider.Meta().(*AWSClient).lambdaconn + + params := &lambda.GetEventSourceMappingInput{ + UUID: aws.String(rs.Primary.ID), + } + + getSourceMappingConfiguration, err := conn.GetEventSourceMapping(params) + if err != nil { + return err + } + + *mapping = *getSourceMappingConfiguration + + return nil + } +} + +func testAccCheckAWSLambdaEventSourceMappingAttributes(mapping *lambda.EventSourceMappingConfiguration) resource.TestCheckFunc { + return func(s *terraform.State) error { + uuid := *mapping.UUID + if uuid == "" { + return fmt.Errorf("Could not read Lambda event source mapping's UUID") + } + + return nil + } +} + +const testAccAWSLambdaEventSourceMappingConfig = ` +resource "aws_iam_role" "iam_for_lambda" { + name = "iam_for_lambda" + assume_role_policy = <