Skip to content

Commit

Permalink
Merge pull request #30896 from C123R/f-aws_vpclattice_access_log_subs…
Browse files Browse the repository at this point in the history
…cription

New VPCLattice resource for Access log subscription: aws_vpclattice_access_log_subscription
  • Loading branch information
ewbankkit authored May 1, 2023
2 parents 8434ab2 + 1d6edf7 commit 6ac1baa
Show file tree
Hide file tree
Showing 5 changed files with 469 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/30896.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_vpclattice_access_log_subscription
```
159 changes: 159 additions & 0 deletions internal/service/vpclattice/access_log_subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package vpclattice

import (
"context"
"errors"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/vpclattice"
"github.com/aws/aws-sdk-go-v2/service/vpclattice/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/create"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_vpclattice_access_log_subscription", name="Access Log Subscription")
// @Tags(identifierAttribute="arn")
func ResourceAccessLogSubscription() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAccessLogSubscriptionCreate,
ReadWithoutTimeout: resourceAccessLogSubscriptionRead,
UpdateWithoutTimeout: resourceAccessLogSubscriptionUpdate,
DeleteWithoutTimeout: resourceAccessLogSubscriptionDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"destination_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: verify.ValidARN,
},
"resource_arn": {
Type: schema.TypeString,
Computed: true,
},
"resource_identifier": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
}
}

const (
ResNameAccessLogSubscription = "Access Log Subscription"
)

func resourceAccessLogSubscriptionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).VPCLatticeClient()

in := &vpclattice.CreateAccessLogSubscriptionInput{
ClientToken: aws.String(id.UniqueId()),
DestinationArn: aws.String(d.Get("destination_arn").(string)),
ResourceIdentifier: aws.String(d.Get("resource_identifier").(string)),
Tags: GetTagsIn(ctx),
}

out, err := conn.CreateAccessLogSubscription(ctx, in)

if err != nil {
return create.DiagError(names.VPCLattice, create.ErrActionCreating, ResNameAccessLogSubscription, d.Get("destination_arn").(string), err)
}

d.SetId(aws.ToString(out.Id))

return resourceAccessLogSubscriptionRead(ctx, d, meta)
}

func resourceAccessLogSubscriptionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).VPCLatticeClient()

out, err := findAccessLogSubscriptionByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] VPCLattice AccessLogSubscription (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return create.DiagError(names.VPCLattice, create.ErrActionReading, ResNameAccessLogSubscription, d.Id(), err)
}

d.Set("arn", out.Arn)
d.Set("destination_arn", out.DestinationArn)
d.Set("resource_arn", out.ResourceArn)
d.Set("resource_identifier", out.ResourceId)

return nil
}

func resourceAccessLogSubscriptionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Tags only.
return resourceAccessLogSubscriptionRead(ctx, d, meta)
}

func resourceAccessLogSubscriptionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).VPCLatticeClient()

log.Printf("[INFO] Deleting VPCLattice AccessLogSubscription %s", d.Id())
_, err := conn.DeleteAccessLogSubscription(ctx, &vpclattice.DeleteAccessLogSubscriptionInput{
AccessLogSubscriptionIdentifier: aws.String(d.Id()),
})

if err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
return nil
}

return create.DiagError(names.VPCLattice, create.ErrActionDeleting, ResNameAccessLogSubscription, d.Id(), err)
}

return nil
}

func findAccessLogSubscriptionByID(ctx context.Context, conn *vpclattice.Client, id string) (*vpclattice.GetAccessLogSubscriptionOutput, error) {
in := &vpclattice.GetAccessLogSubscriptionInput{
AccessLogSubscriptionIdentifier: aws.String(id),
}
out, err := conn.GetAccessLogSubscription(ctx, in)
if err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: in,
}
}

return nil, err
}

if out == nil || out.Id == nil {
return nil, tfresource.NewEmptyResultError(in)
}

return out, nil
}
Loading

0 comments on commit 6ac1baa

Please sign in to comment.