|
| 1 | +package vpclattice |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/vpclattice" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/vpclattice/types" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | + "github.com/hashicorp/terraform-provider-aws/internal/conns" |
| 16 | + "github.com/hashicorp/terraform-provider-aws/internal/create" |
| 17 | + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" |
| 19 | + "github.com/hashicorp/terraform-provider-aws/internal/verify" |
| 20 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 21 | +) |
| 22 | + |
| 23 | +// @SDKResource("aws_vpclattice_access_log_subscription", name="Access Log Subscription") |
| 24 | +// @Tags(identifierAttribute="arn") |
| 25 | +func ResourceAccessLogSubscription() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + CreateWithoutTimeout: resourceAccessLogSubscriptionCreate, |
| 28 | + ReadWithoutTimeout: resourceAccessLogSubscriptionRead, |
| 29 | + UpdateWithoutTimeout: resourceAccessLogSubscriptionUpdate, |
| 30 | + DeleteWithoutTimeout: resourceAccessLogSubscriptionDelete, |
| 31 | + |
| 32 | + Importer: &schema.ResourceImporter{ |
| 33 | + StateContext: schema.ImportStatePassthroughContext, |
| 34 | + }, |
| 35 | + |
| 36 | + Schema: map[string]*schema.Schema{ |
| 37 | + "arn": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "destination_arn": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + ForceNew: true, |
| 45 | + ValidateFunc: verify.ValidARN, |
| 46 | + }, |
| 47 | + "resource_arn": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "resource_identifier": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Required: true, |
| 54 | + ForceNew: true, |
| 55 | + }, |
| 56 | + names.AttrTags: tftags.TagsSchema(), |
| 57 | + names.AttrTagsAll: tftags.TagsSchemaComputed(), |
| 58 | + }, |
| 59 | + |
| 60 | + CustomizeDiff: verify.SetTagsDiff, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +const ( |
| 65 | + ResNameAccessLogSubscription = "Access Log Subscription" |
| 66 | +) |
| 67 | + |
| 68 | +func resourceAccessLogSubscriptionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 69 | + conn := meta.(*conns.AWSClient).VPCLatticeClient() |
| 70 | + |
| 71 | + in := &vpclattice.CreateAccessLogSubscriptionInput{ |
| 72 | + ClientToken: aws.String(id.UniqueId()), |
| 73 | + DestinationArn: aws.String(d.Get("destination_arn").(string)), |
| 74 | + ResourceIdentifier: aws.String(d.Get("resource_identifier").(string)), |
| 75 | + Tags: GetTagsIn(ctx), |
| 76 | + } |
| 77 | + |
| 78 | + out, err := conn.CreateAccessLogSubscription(ctx, in) |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + return create.DiagError(names.VPCLattice, create.ErrActionCreating, ResNameAccessLogSubscription, d.Get("destination_arn").(string), err) |
| 82 | + } |
| 83 | + |
| 84 | + d.SetId(aws.ToString(out.Id)) |
| 85 | + |
| 86 | + return resourceAccessLogSubscriptionRead(ctx, d, meta) |
| 87 | +} |
| 88 | + |
| 89 | +func resourceAccessLogSubscriptionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 90 | + conn := meta.(*conns.AWSClient).VPCLatticeClient() |
| 91 | + |
| 92 | + out, err := findAccessLogSubscriptionByID(ctx, conn, d.Id()) |
| 93 | + |
| 94 | + if !d.IsNewResource() && tfresource.NotFound(err) { |
| 95 | + log.Printf("[WARN] VPCLattice AccessLogSubscription (%s) not found, removing from state", d.Id()) |
| 96 | + d.SetId("") |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + if err != nil { |
| 101 | + return create.DiagError(names.VPCLattice, create.ErrActionReading, ResNameAccessLogSubscription, d.Id(), err) |
| 102 | + } |
| 103 | + |
| 104 | + d.Set("arn", out.Arn) |
| 105 | + d.Set("destination_arn", out.DestinationArn) |
| 106 | + d.Set("resource_arn", out.ResourceArn) |
| 107 | + d.Set("resource_identifier", out.ResourceId) |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func resourceAccessLogSubscriptionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 113 | + // Tags only. |
| 114 | + return resourceAccessLogSubscriptionRead(ctx, d, meta) |
| 115 | +} |
| 116 | + |
| 117 | +func resourceAccessLogSubscriptionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 118 | + conn := meta.(*conns.AWSClient).VPCLatticeClient() |
| 119 | + |
| 120 | + log.Printf("[INFO] Deleting VPCLattice AccessLogSubscription %s", d.Id()) |
| 121 | + _, err := conn.DeleteAccessLogSubscription(ctx, &vpclattice.DeleteAccessLogSubscriptionInput{ |
| 122 | + AccessLogSubscriptionIdentifier: aws.String(d.Id()), |
| 123 | + }) |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + var nfe *types.ResourceNotFoundException |
| 127 | + if errors.As(err, &nfe) { |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + return create.DiagError(names.VPCLattice, create.ErrActionDeleting, ResNameAccessLogSubscription, d.Id(), err) |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func findAccessLogSubscriptionByID(ctx context.Context, conn *vpclattice.Client, id string) (*vpclattice.GetAccessLogSubscriptionOutput, error) { |
| 138 | + in := &vpclattice.GetAccessLogSubscriptionInput{ |
| 139 | + AccessLogSubscriptionIdentifier: aws.String(id), |
| 140 | + } |
| 141 | + out, err := conn.GetAccessLogSubscription(ctx, in) |
| 142 | + if err != nil { |
| 143 | + var nfe *types.ResourceNotFoundException |
| 144 | + if errors.As(err, &nfe) { |
| 145 | + return nil, &retry.NotFoundError{ |
| 146 | + LastError: err, |
| 147 | + LastRequest: in, |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + |
| 154 | + if out == nil || out.Id == nil { |
| 155 | + return nil, tfresource.NewEmptyResultError(in) |
| 156 | + } |
| 157 | + |
| 158 | + return out, nil |
| 159 | +} |
0 commit comments