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