github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_api_gateway_domain_name.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 resourceAwsApiGatewayDomainName() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsApiGatewayDomainNameCreate,
    18  		Read:   resourceAwsApiGatewayDomainNameRead,
    19  		Update: resourceAwsApiGatewayDomainNameUpdate,
    20  		Delete: resourceAwsApiGatewayDomainNameDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  
    24  			"certificate_body": {
    25  				Type:     schema.TypeString,
    26  				ForceNew: true,
    27  				Required: true,
    28  			},
    29  
    30  			"certificate_chain": {
    31  				Type:     schema.TypeString,
    32  				ForceNew: true,
    33  				Required: true,
    34  			},
    35  
    36  			"certificate_name": {
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  			},
    40  
    41  			"certificate_private_key": {
    42  				Type:     schema.TypeString,
    43  				ForceNew: true,
    44  				Required: true,
    45  			},
    46  
    47  			"domain_name": {
    48  				Type:     schema.TypeString,
    49  				Required: true,
    50  				ForceNew: true,
    51  			},
    52  
    53  			"cloudfront_domain_name": {
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  
    58  			"certificate_upload_date": {
    59  				Type:     schema.TypeString,
    60  				Computed: true,
    61  			},
    62  
    63  			"cloudfront_zone_id": {
    64  				Type:     schema.TypeString,
    65  				Computed: true,
    66  			},
    67  		},
    68  	}
    69  }
    70  
    71  func resourceAwsApiGatewayDomainNameCreate(d *schema.ResourceData, meta interface{}) error {
    72  	conn := meta.(*AWSClient).apigateway
    73  	log.Printf("[DEBUG] Creating API Gateway Domain Name")
    74  
    75  	domainName, err := conn.CreateDomainName(&apigateway.CreateDomainNameInput{
    76  		CertificateBody:       aws.String(d.Get("certificate_body").(string)),
    77  		CertificateChain:      aws.String(d.Get("certificate_chain").(string)),
    78  		CertificateName:       aws.String(d.Get("certificate_name").(string)),
    79  		CertificatePrivateKey: aws.String(d.Get("certificate_private_key").(string)),
    80  		DomainName:            aws.String(d.Get("domain_name").(string)),
    81  	})
    82  	if err != nil {
    83  		return fmt.Errorf("Error creating API Gateway Domain Name: %s", err)
    84  	}
    85  
    86  	d.SetId(*domainName.DomainName)
    87  	d.Set("cloudfront_domain_name", domainName.DistributionDomainName)
    88  	d.Set("cloudfront_zone_id", cloudFrontRoute53ZoneID)
    89  
    90  	return resourceAwsApiGatewayDomainNameRead(d, meta)
    91  }
    92  
    93  func resourceAwsApiGatewayDomainNameRead(d *schema.ResourceData, meta interface{}) error {
    94  	conn := meta.(*AWSClient).apigateway
    95  	log.Printf("[DEBUG] Reading API Gateway Domain Name %s", d.Id())
    96  
    97  	domainName, err := conn.GetDomainName(&apigateway.GetDomainNameInput{
    98  		DomainName: aws.String(d.Id()),
    99  	})
   100  	if err != nil {
   101  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
   102  			log.Printf("[WARN] API gateway domain name %s has vanished\n", d.Id())
   103  			d.SetId("")
   104  			return nil
   105  		}
   106  
   107  		return err
   108  	}
   109  
   110  	d.Set("certificate_name", domainName.CertificateName)
   111  	if err := d.Set("certificate_upload_date", domainName.CertificateUploadDate.Format(time.RFC3339)); err != nil {
   112  		log.Printf("[DEBUG] Error setting certificate_upload_date: %s", err)
   113  	}
   114  	d.Set("cloudfront_domain_name", domainName.DistributionDomainName)
   115  	d.Set("domain_name", domainName.DomainName)
   116  
   117  	return nil
   118  }
   119  
   120  func resourceAwsApiGatewayDomainNameUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
   121  	operations := make([]*apigateway.PatchOperation, 0)
   122  
   123  	if d.HasChange("certificate_name") {
   124  		operations = append(operations, &apigateway.PatchOperation{
   125  			Op:    aws.String("replace"),
   126  			Path:  aws.String("/certificateName"),
   127  			Value: aws.String(d.Get("certificate_name").(string)),
   128  		})
   129  	}
   130  
   131  	return operations
   132  }
   133  
   134  func resourceAwsApiGatewayDomainNameUpdate(d *schema.ResourceData, meta interface{}) error {
   135  	conn := meta.(*AWSClient).apigateway
   136  	log.Printf("[DEBUG] Updating API Gateway Domain Name %s", d.Id())
   137  
   138  	_, err := conn.UpdateDomainName(&apigateway.UpdateDomainNameInput{
   139  		DomainName:      aws.String(d.Id()),
   140  		PatchOperations: resourceAwsApiGatewayDomainNameUpdateOperations(d),
   141  	})
   142  	if err != nil {
   143  		return err
   144  	}
   145  
   146  	return resourceAwsApiGatewayDomainNameRead(d, meta)
   147  }
   148  
   149  func resourceAwsApiGatewayDomainNameDelete(d *schema.ResourceData, meta interface{}) error {
   150  	conn := meta.(*AWSClient).apigateway
   151  	log.Printf("[DEBUG] Deleting API Gateway Domain Name: %s", d.Id())
   152  
   153  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   154  		_, err := conn.DeleteDomainName(&apigateway.DeleteDomainNameInput{
   155  			DomainName: aws.String(d.Id()),
   156  		})
   157  
   158  		if err == nil {
   159  			return nil
   160  		}
   161  
   162  		if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" {
   163  			return nil
   164  		}
   165  
   166  		return resource.NonRetryableError(err)
   167  	})
   168  }