github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_lambda_alias.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/lambda"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceAwsLambdaAlias() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsLambdaAliasCreate,
    15  		Read:   resourceAwsLambdaAliasRead,
    16  		Update: resourceAwsLambdaAliasUpdate,
    17  		Delete: resourceAwsLambdaAliasDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"description": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  			},
    24  			"function_name": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  			},
    28  			"function_version": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  			},
    32  			"name": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  			},
    36  			"arn": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Computed: true,
    39  			},
    40  		},
    41  	}
    42  }
    43  
    44  // resourceAwsLambdaAliasCreate maps to:
    45  // CreateAlias in the API / SDK
    46  func resourceAwsLambdaAliasCreate(d *schema.ResourceData, meta interface{}) error {
    47  	conn := meta.(*AWSClient).lambdaconn
    48  
    49  	functionName := d.Get("function_name").(string)
    50  	aliasName := d.Get("name").(string)
    51  
    52  	log.Printf("[DEBUG] Creating Lambda alias: alias %s for function %s", aliasName, functionName)
    53  
    54  	params := &lambda.CreateAliasInput{
    55  		Description:     aws.String(d.Get("description").(string)),
    56  		FunctionName:    aws.String(functionName),
    57  		FunctionVersion: aws.String(d.Get("function_version").(string)),
    58  		Name:            aws.String(aliasName),
    59  	}
    60  
    61  	aliasConfiguration, err := conn.CreateAlias(params)
    62  	if err != nil {
    63  		return fmt.Errorf("Error creating Lambda alias: %s", err)
    64  	}
    65  
    66  	d.SetId(*aliasConfiguration.AliasArn)
    67  
    68  	return resourceAwsLambdaAliasRead(d, meta)
    69  }
    70  
    71  // resourceAwsLambdaAliasRead maps to:
    72  // GetAlias in the API / SDK
    73  func resourceAwsLambdaAliasRead(d *schema.ResourceData, meta interface{}) error {
    74  	conn := meta.(*AWSClient).lambdaconn
    75  
    76  	log.Printf("[DEBUG] Fetching Lambda alias: %s:%s", d.Get("function_name"), d.Get("name"))
    77  
    78  	params := &lambda.GetAliasInput{
    79  		FunctionName: aws.String(d.Get("function_name").(string)),
    80  		Name:         aws.String(d.Get("name").(string)),
    81  	}
    82  
    83  	aliasConfiguration, err := conn.GetAlias(params)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	d.Set("description", aliasConfiguration.Description)
    89  	d.Set("function_version", aliasConfiguration.FunctionVersion)
    90  	d.Set("name", aliasConfiguration.Name)
    91  	d.Set("arn", aliasConfiguration.AliasArn)
    92  
    93  	return nil
    94  }
    95  
    96  // resourceAwsLambdaAliasDelete maps to:
    97  // DeleteAlias in the API / SDK
    98  func resourceAwsLambdaAliasDelete(d *schema.ResourceData, meta interface{}) error {
    99  	conn := meta.(*AWSClient).lambdaconn
   100  
   101  	log.Printf("[INFO] Deleting Lambda alias: %s:%s", d.Get("function_name"), d.Get("name"))
   102  
   103  	params := &lambda.DeleteAliasInput{
   104  		FunctionName: aws.String(d.Get("function_name").(string)),
   105  		Name:         aws.String(d.Get("name").(string)),
   106  	}
   107  
   108  	_, err := conn.DeleteAlias(params)
   109  	if err != nil {
   110  		return fmt.Errorf("Error deleting Lambda alias: %s", err)
   111  	}
   112  
   113  	d.SetId("")
   114  
   115  	return nil
   116  }
   117  
   118  // resourceAwsLambdaAliasUpdate maps to:
   119  // UpdateAlias in the API / SDK
   120  func resourceAwsLambdaAliasUpdate(d *schema.ResourceData, meta interface{}) error {
   121  	conn := meta.(*AWSClient).lambdaconn
   122  
   123  	log.Printf("[DEBUG] Updating Lambda alias: %s:%s", d.Get("function_name"), d.Get("name"))
   124  
   125  	params := &lambda.UpdateAliasInput{
   126  		Description:     aws.String(d.Get("description").(string)),
   127  		FunctionName:    aws.String(d.Get("function_name").(string)),
   128  		FunctionVersion: aws.String(d.Get("function_version").(string)),
   129  		Name:            aws.String(d.Get("name").(string)),
   130  	}
   131  
   132  	_, err := conn.UpdateAlias(params)
   133  	if err != nil {
   134  		return fmt.Errorf("Error updating Lambda alias: %s", err)
   135  	}
   136  
   137  	return nil
   138  }