github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 //According to AWS Documentation, ACM will be the only way to add certificates 25 //to ApiGateway DomainNames. When this happens, we will be deprecating all certificate methods 26 //except certificate_arn. We are not quite sure when this will happen. 27 "certificate_body": { 28 Type: schema.TypeString, 29 ForceNew: true, 30 Optional: true, 31 ConflictsWith: []string{"certificate_arn"}, 32 }, 33 34 "certificate_chain": { 35 Type: schema.TypeString, 36 ForceNew: true, 37 Optional: true, 38 ConflictsWith: []string{"certificate_arn"}, 39 }, 40 41 "certificate_name": { 42 Type: schema.TypeString, 43 Optional: true, 44 ConflictsWith: []string{"certificate_arn"}, 45 }, 46 47 "certificate_private_key": { 48 Type: schema.TypeString, 49 ForceNew: true, 50 Optional: true, 51 Sensitive: true, 52 ConflictsWith: []string{"certificate_arn"}, 53 }, 54 55 "domain_name": { 56 Type: schema.TypeString, 57 Required: true, 58 ForceNew: true, 59 }, 60 61 "certificate_arn": { 62 Type: schema.TypeString, 63 Optional: true, 64 ConflictsWith: []string{"certificate_body", "certificate_chain", "certificate_name", "certificate_private_key"}, 65 }, 66 67 "cloudfront_domain_name": { 68 Type: schema.TypeString, 69 Computed: true, 70 }, 71 72 "certificate_upload_date": { 73 Type: schema.TypeString, 74 Computed: true, 75 }, 76 77 "cloudfront_zone_id": { 78 Type: schema.TypeString, 79 Computed: true, 80 }, 81 }, 82 } 83 } 84 85 func resourceAwsApiGatewayDomainNameCreate(d *schema.ResourceData, meta interface{}) error { 86 conn := meta.(*AWSClient).apigateway 87 log.Printf("[DEBUG] Creating API Gateway Domain Name") 88 89 params := &apigateway.CreateDomainNameInput{ 90 DomainName: aws.String(d.Get("domain_name").(string)), 91 } 92 93 if v, ok := d.GetOk("certificate_arn"); ok { 94 params.CertificateArn = aws.String(v.(string)) 95 } 96 97 if v, ok := d.GetOk("certificate_name"); ok { 98 params.CertificateName = aws.String(v.(string)) 99 } 100 101 if v, ok := d.GetOk("certificate_body"); ok { 102 params.CertificateBody = aws.String(v.(string)) 103 } 104 105 if v, ok := d.GetOk("certificate_chain"); ok { 106 params.CertificateChain = aws.String(v.(string)) 107 } 108 109 if v, ok := d.GetOk("certificate_private_key"); ok { 110 params.CertificatePrivateKey = aws.String(v.(string)) 111 } 112 113 domainName, err := conn.CreateDomainName(params) 114 if err != nil { 115 return fmt.Errorf("Error creating API Gateway Domain Name: %s", err) 116 } 117 118 d.SetId(*domainName.DomainName) 119 d.Set("cloudfront_domain_name", domainName.DistributionDomainName) 120 d.Set("cloudfront_zone_id", cloudFrontRoute53ZoneID) 121 122 return resourceAwsApiGatewayDomainNameRead(d, meta) 123 } 124 125 func resourceAwsApiGatewayDomainNameRead(d *schema.ResourceData, meta interface{}) error { 126 conn := meta.(*AWSClient).apigateway 127 log.Printf("[DEBUG] Reading API Gateway Domain Name %s", d.Id()) 128 129 domainName, err := conn.GetDomainName(&apigateway.GetDomainNameInput{ 130 DomainName: aws.String(d.Id()), 131 }) 132 if err != nil { 133 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { 134 log.Printf("[WARN] API gateway domain name %s has vanished\n", d.Id()) 135 d.SetId("") 136 return nil 137 } 138 139 return err 140 } 141 142 d.Set("certificate_name", domainName.CertificateName) 143 if err := d.Set("certificate_upload_date", domainName.CertificateUploadDate.Format(time.RFC3339)); err != nil { 144 log.Printf("[DEBUG] Error setting certificate_upload_date: %s", err) 145 } 146 d.Set("cloudfront_domain_name", domainName.DistributionDomainName) 147 d.Set("domain_name", domainName.DomainName) 148 d.Set("certificate_arn", domainName.CertificateArn) 149 150 return nil 151 } 152 153 func resourceAwsApiGatewayDomainNameUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { 154 operations := make([]*apigateway.PatchOperation, 0) 155 156 if d.HasChange("certificate_name") { 157 operations = append(operations, &apigateway.PatchOperation{ 158 Op: aws.String("replace"), 159 Path: aws.String("/certificateName"), 160 Value: aws.String(d.Get("certificate_name").(string)), 161 }) 162 } 163 164 if d.HasChange("certificate_arn") { 165 operations = append(operations, &apigateway.PatchOperation{ 166 Op: aws.String("replace"), 167 Path: aws.String("/certificateArn"), 168 Value: aws.String(d.Get("certificate_arn").(string)), 169 }) 170 } 171 172 return operations 173 } 174 175 func resourceAwsApiGatewayDomainNameUpdate(d *schema.ResourceData, meta interface{}) error { 176 conn := meta.(*AWSClient).apigateway 177 log.Printf("[DEBUG] Updating API Gateway Domain Name %s", d.Id()) 178 179 _, err := conn.UpdateDomainName(&apigateway.UpdateDomainNameInput{ 180 DomainName: aws.String(d.Id()), 181 PatchOperations: resourceAwsApiGatewayDomainNameUpdateOperations(d), 182 }) 183 184 if err != nil { 185 return err 186 } 187 188 return resourceAwsApiGatewayDomainNameRead(d, meta) 189 } 190 191 func resourceAwsApiGatewayDomainNameDelete(d *schema.ResourceData, meta interface{}) error { 192 conn := meta.(*AWSClient).apigateway 193 log.Printf("[DEBUG] Deleting API Gateway Domain Name: %s", d.Id()) 194 195 return resource.Retry(5*time.Minute, func() *resource.RetryError { 196 _, err := conn.DeleteDomainName(&apigateway.DeleteDomainNameInput{ 197 DomainName: aws.String(d.Id()), 198 }) 199 200 if err == nil { 201 return nil 202 } 203 204 if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" { 205 return nil 206 } 207 208 return resource.NonRetryableError(err) 209 }) 210 }