Skip to content

Commit

Permalink
Merge pull request #22487 from DrFaust92/appsync_domain_name
Browse files Browse the repository at this point in the history
r/appsync_domain_name + association resources
  • Loading branch information
ewbankkit authored Jan 18, 2022
2 parents f14a920 + 4cbc7cc commit 8026ea1
Show file tree
Hide file tree
Showing 13 changed files with 993 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .changelog/22487.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
aws_appsync_domain_name
```

```release-note:new-resource
aws_appsync_domain_name_api_association
```
14 changes: 8 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,12 +828,14 @@ func Provider() *schema.Provider {
"aws_appstream_user": appstream.ResourceUser(),
"aws_appstream_user_stack_association": appstream.ResourceUserStackAssociation(),

"aws_appsync_api_cache": appsync.ResourceAPICache(),
"aws_appsync_api_key": appsync.ResourceAPIKey(),
"aws_appsync_datasource": appsync.ResourceDataSource(),
"aws_appsync_function": appsync.ResourceFunction(),
"aws_appsync_graphql_api": appsync.ResourceGraphQLAPI(),
"aws_appsync_resolver": appsync.ResourceResolver(),
"aws_appsync_api_cache": appsync.ResourceAPICache(),
"aws_appsync_api_key": appsync.ResourceAPIKey(),
"aws_appsync_datasource": appsync.ResourceDataSource(),
"aws_appsync_domain_name": appsync.ResourceDomainName(),
"aws_appsync_domain_name_api_association": appsync.ResourceDomainNameApiAssociation(),
"aws_appsync_function": appsync.ResourceFunction(),
"aws_appsync_graphql_api": appsync.ResourceGraphQLAPI(),
"aws_appsync_resolver": appsync.ResourceResolver(),

"aws_athena_database": athena.ResourceDatabase(),
"aws_athena_named_query": athena.ResourceNamedQuery(),
Expand Down
22 changes: 22 additions & 0 deletions internal/service/appsync/appsync_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package appsync_test

import (
"os"
"testing"
)

Expand Down Expand Up @@ -81,6 +82,15 @@ func TestAccAppSync_serial(t *testing.T) {
"basic": testAccAppSyncApiCache_basic,
"disappears": testAccAppSyncApiCache_disappears,
},
"DomainName": {
"basic": testAccAppSyncDomainName_basic,
"disappears": testAccAppSyncDomainName_disappears,
"description": testAccAppSyncDomainName_description,
},
"DomainNameAssociation": {
"basic": testAccAppSyncDomainNameApiAssociation_basic,
"disappears": testAccAppSyncDomainNameApiAssociation_disappears,
},
}

for group, m := range testCases {
Expand All @@ -95,3 +105,15 @@ func TestAccAppSync_serial(t *testing.T) {
})
}
}

func getAppsyncCertDomain(t *testing.T) string {
value := os.Getenv("AWS_APPSYNC_DOMAIN_NAME_CERTIFICATE_DOMAIN")
if value == "" {
t.Skip(
"Environment variable AWS_APPSYNC_DOMAIN_NAME_CERTIFICATE_DOMAIN is not set. " +
"This environment variable must be set to any non-empty value " +
"to enable the test.")
}

return value
}
145 changes: 145 additions & 0 deletions internal/service/appsync/domain_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package appsync

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appsync"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceDomainName() *schema.Resource {

return &schema.Resource{
Create: resourceDomainNameCreate,
Read: resourceDomainNameRead,
Update: resourceDomainNameUpdate,
Delete: resourceDomainNameDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"appsync_domain_name": {
Type: schema.TypeString,
Computed: true,
},
"certificate_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: verify.ValidARN,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"domain_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"hosted_zone_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceDomainNameCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).AppSyncConn

params := &appsync.CreateDomainNameInput{
CertificateArn: aws.String(d.Get("certificate_arn").(string)),
Description: aws.String(d.Get("description").(string)),
DomainName: aws.String(d.Get("domain_name").(string)),
}

resp, err := conn.CreateDomainName(params)
if err != nil {
return fmt.Errorf("error creating Appsync Domain Name: %w", err)
}

d.SetId(aws.StringValue(resp.DomainNameConfig.DomainName))

return resourceDomainNameRead(d, meta)
}

func resourceDomainNameRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).AppSyncConn

domainName, err := FindDomainNameByID(conn, d.Id())
if domainName == nil && !d.IsNewResource() {
log.Printf("[WARN] AppSync Domain Name (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error getting Appsync Domain Name %q: %w", d.Id(), err)
}

d.Set("domain_name", domainName.DomainName)
d.Set("description", domainName.Description)
d.Set("certificate_arn", domainName.CertificateArn)
d.Set("hosted_zone_id", domainName.HostedZoneId)
d.Set("appsync_domain_name", domainName.AppsyncDomainName)

return nil
}

func resourceDomainNameUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).AppSyncConn

params := &appsync.UpdateDomainNameInput{
DomainName: aws.String(d.Id()),
}

if d.HasChange("description") {
params.Description = aws.String(d.Get("description").(string))
}

_, err := conn.UpdateDomainName(params)
if err != nil {
return fmt.Errorf("error updating Appsync Domain Name %q: %w", d.Id(), err)
}

return resourceDomainNameRead(d, meta)

}

func resourceDomainNameDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).AppSyncConn

input := &appsync.DeleteDomainNameInput{
DomainName: aws.String(d.Id()),
}

err := resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteDomainName(input)
if tfawserr.ErrCodeEquals(err, appsync.ErrCodeConcurrentModificationException) {
return resource.RetryableError(fmt.Errorf("deleting Appsync Domain Name %q: %w", d.Id(), err))
}
if err != nil {
return resource.NonRetryableError(err)
}

return nil
})
if tfresource.TimedOut(err) {
_, err = conn.DeleteDomainName(input)
}
if err != nil {
return fmt.Errorf("error deleting Appsync Domain Name %q: %w", d.Id(), err)
}

return nil
}
120 changes: 120 additions & 0 deletions internal/service/appsync/domain_name_api_association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package appsync

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appsync"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)

func ResourceDomainNameApiAssociation() *schema.Resource {

return &schema.Resource{
Create: resourceDomainNameApiAssociationCreate,
Read: resourceDomainNameApiAssociationRead,
Update: resourceDomainNameApiAssociationUpdate,
Delete: resourceDomainNameApiAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"api_id": {
Type: schema.TypeString,
Required: true,
},
"domain_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceDomainNameApiAssociationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).AppSyncConn

params := &appsync.AssociateApiInput{
ApiId: aws.String(d.Get("api_id").(string)),
DomainName: aws.String(d.Get("domain_name").(string)),
}

resp, err := conn.AssociateApi(params)
if err != nil {
return fmt.Errorf("error creating Appsync Domain Name API Association: %w", err)
}

d.SetId(aws.StringValue(resp.ApiAssociation.DomainName))

if err := waitDomainNameApiAssociation(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for Appsync Domain Name API (%s) Association: %w", d.Id(), err)
}

return resourceDomainNameApiAssociationRead(d, meta)
}

func resourceDomainNameApiAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).AppSyncConn

association, err := FindDomainNameApiAssociationByID(conn, d.Id())
if association == nil && !d.IsNewResource() {
log.Printf("[WARN] Appsync Domain Name API Association (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error getting Appsync Domain Name API Association %q: %w", d.Id(), err)
}

d.Set("domain_name", association.DomainName)
d.Set("api_id", association.ApiId)

return nil
}

func resourceDomainNameApiAssociationUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).AppSyncConn

params := &appsync.AssociateApiInput{
ApiId: aws.String(d.Get("api_id").(string)),
DomainName: aws.String(d.Get("domain_name").(string)),
}

_, err := conn.AssociateApi(params)
if err != nil {
return fmt.Errorf("error creating Appsync Domain Name API Association: %w", err)
}

if err := waitDomainNameApiAssociation(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for Appsync Domain Name API (%s) Association: %w", d.Id(), err)
}

return resourceDomainNameApiAssociationRead(d, meta)
}

func resourceDomainNameApiAssociationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).AppSyncConn

input := &appsync.DisassociateApiInput{
DomainName: aws.String(d.Id()),
}
_, err := conn.DisassociateApi(input)
if err != nil {
if tfawserr.ErrCodeEquals(err, appsync.ErrCodeNotFoundException) {
return nil
}
return fmt.Errorf("error deleting Appsync Domain Name API Association: %w", err)
}

if err := waitDomainNameApiDisassociation(conn, d.Id()); err != nil {
return fmt.Errorf("error waiting for Appsync Domain Name API (%s) Disassociation: %w", d.Id(), err)
}

return nil
}
Loading

0 comments on commit 8026ea1

Please sign in to comment.