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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/apigateway"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsApiGatewayBasePathMapping() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsApiGatewayBasePathMappingCreate,
    18  		Read:   resourceAwsApiGatewayBasePathMappingRead,
    19  		Delete: resourceAwsApiGatewayBasePathMappingDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"api_id": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"base_path": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				ForceNew: true,
    31  			},
    32  			"stage_name": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  				ForceNew: true,
    36  			},
    37  			"domain_name": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func resourceAwsApiGatewayBasePathMappingCreate(d *schema.ResourceData, meta interface{}) error {
    47  	conn := meta.(*AWSClient).apigateway
    48  
    49  	err := resource.Retry(30*time.Second, func() *resource.RetryError {
    50  		_, err := conn.CreateBasePathMapping(&apigateway.CreateBasePathMappingInput{
    51  			RestApiId:  aws.String(d.Get("api_id").(string)),
    52  			DomainName: aws.String(d.Get("domain_name").(string)),
    53  			BasePath:   aws.String(d.Get("base_path").(string)),
    54  			Stage:      aws.String(d.Get("stage_name").(string)),
    55  		})
    56  
    57  		if err != nil {
    58  			if err, ok := err.(awserr.Error); ok && err.Code() != "BadRequestException" {
    59  				return resource.NonRetryableError(err)
    60  			}
    61  
    62  			return resource.RetryableError(
    63  				fmt.Errorf("Error creating Gateway base path mapping: %s", err),
    64  			)
    65  		}
    66  
    67  		return nil
    68  	})
    69  
    70  	if err != nil {
    71  		return fmt.Errorf("Error creating Gateway base path mapping: %s", err)
    72  	}
    73  
    74  	id := fmt.Sprintf("%s/%s", d.Get("domain_name").(string), d.Get("base_path").(string))
    75  	d.SetId(id)
    76  
    77  	return resourceAwsApiGatewayBasePathMappingRead(d, meta)
    78  }
    79  
    80  func resourceAwsApiGatewayBasePathMappingRead(d *schema.ResourceData, meta interface{}) error {
    81  	conn := meta.(*AWSClient).apigateway
    82  
    83  	domainName := d.Get("domain_name").(string)
    84  	basePath := d.Get("base_path").(string)
    85  
    86  	if domainName == "" {
    87  		return nil
    88  	}
    89  
    90  	mapping, err := conn.GetBasePathMapping(&apigateway.GetBasePathMappingInput{
    91  		DomainName: aws.String(domainName),
    92  		BasePath:   aws.String(basePath),
    93  	})
    94  	if err != nil {
    95  		if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" {
    96  			log.Printf("[WARN] API gateway base path mapping %s has vanished\n", d.Id())
    97  			d.SetId("")
    98  			return nil
    99  		}
   100  
   101  		return fmt.Errorf("Error reading Gateway base path mapping: %s", err)
   102  	}
   103  
   104  	d.Set("base_path", mapping.BasePath)
   105  	d.Set("api_id", mapping.RestApiId)
   106  	d.Set("stage_name", mapping.Stage)
   107  
   108  	return nil
   109  }
   110  
   111  func resourceAwsApiGatewayBasePathMappingDelete(d *schema.ResourceData, meta interface{}) error {
   112  	conn := meta.(*AWSClient).apigateway
   113  
   114  	_, err := conn.DeleteBasePathMapping(&apigateway.DeleteBasePathMappingInput{
   115  		DomainName: aws.String(d.Get("domain_name").(string)),
   116  		BasePath:   aws.String(d.Get("base_path").(string)),
   117  	})
   118  
   119  	if err != nil {
   120  		if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" {
   121  			return nil
   122  		}
   123  
   124  		return err
   125  	}
   126  
   127  	return nil
   128  }