Skip to content

Commit 1ba772f

Browse files
authored
Merge pull request #26673 from dionasaur/patch-1
Update vpnsite_customer_gateway.go
2 parents cf2c344 + 9fc745a commit 1ba772f

9 files changed

+117
-124
lines changed

.changelog/26673.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_customer_gateway: Make `ip_address` optional
3+
```

internal/acctest/acctest.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ func CheckACMPCACertificateAuthorityActivateRootCA(certificateAuthority *acmpca.
14211421
})
14221422

14231423
if err != nil {
1424-
return fmt.Errorf("error getting ACM PCA Certificate Authority (%s) CSR: %w", arn, err)
1424+
return fmt.Errorf("getting ACM PCA Certificate Authority (%s) CSR: %w", arn, err)
14251425
}
14261426

14271427
issueCertOutput, err := conn.IssueCertificate(&acmpca.IssueCertificateInput{
@@ -1437,7 +1437,7 @@ func CheckACMPCACertificateAuthorityActivateRootCA(certificateAuthority *acmpca.
14371437
})
14381438

14391439
if err != nil {
1440-
return fmt.Errorf("error issuing ACM PCA Certificate Authority (%s) Root CA certificate from CSR: %w", arn, err)
1440+
return fmt.Errorf("issuing ACM PCA Certificate Authority (%s) Root CA certificate from CSR: %w", arn, err)
14411441
}
14421442

14431443
// Wait for certificate status to become ISSUED.
@@ -1447,7 +1447,7 @@ func CheckACMPCACertificateAuthorityActivateRootCA(certificateAuthority *acmpca.
14471447
})
14481448

14491449
if err != nil {
1450-
return fmt.Errorf("error waiting for ACM PCA Certificate Authority (%s) Root CA certificate to become ISSUED: %w", arn, err)
1450+
return fmt.Errorf("waiting for ACM PCA Certificate Authority (%s) Root CA certificate to become ISSUED: %w", arn, err)
14511451
}
14521452

14531453
getCertOutput, err := conn.GetCertificate(&acmpca.GetCertificateInput{
@@ -1456,7 +1456,7 @@ func CheckACMPCACertificateAuthorityActivateRootCA(certificateAuthority *acmpca.
14561456
})
14571457

14581458
if err != nil {
1459-
return fmt.Errorf("error getting ACM PCA Certificate Authority (%s) issued Root CA certificate: %w", arn, err)
1459+
return fmt.Errorf("getting ACM PCA Certificate Authority (%s) issued Root CA certificate: %w", arn, err)
14601460
}
14611461

14621462
_, err = conn.ImportCertificateAuthorityCertificate(&acmpca.ImportCertificateAuthorityCertificateInput{
@@ -1465,7 +1465,7 @@ func CheckACMPCACertificateAuthorityActivateRootCA(certificateAuthority *acmpca.
14651465
})
14661466

14671467
if err != nil {
1468-
return fmt.Errorf("error importing ACM PCA Certificate Authority (%s) Root CA certificate: %w", arn, err)
1468+
return fmt.Errorf("importing ACM PCA Certificate Authority (%s) Root CA certificate: %w", arn, err)
14691469
}
14701470

14711471
return err
@@ -1487,7 +1487,7 @@ func CheckACMPCACertificateAuthorityActivateSubordinateCA(rootCertificateAuthori
14871487
})
14881488

14891489
if err != nil {
1490-
return fmt.Errorf("error getting ACM PCA Certificate Authority (%s) CSR: %w", arn, err)
1490+
return fmt.Errorf("getting ACM PCA Certificate Authority (%s) CSR: %w", arn, err)
14911491
}
14921492

14931493
rootCertificateAuthorityArn := aws.StringValue(rootCertificateAuthority.Arn)
@@ -1505,7 +1505,7 @@ func CheckACMPCACertificateAuthorityActivateSubordinateCA(rootCertificateAuthori
15051505
})
15061506

15071507
if err != nil {
1508-
return fmt.Errorf("error issuing ACM PCA Certificate Authority (%s) Subordinate CA certificate from CSR: %w", arn, err)
1508+
return fmt.Errorf("issuing ACM PCA Certificate Authority (%s) Subordinate CA certificate from CSR: %w", arn, err)
15091509
}
15101510

15111511
// Wait for certificate status to become ISSUED.
@@ -1515,7 +1515,7 @@ func CheckACMPCACertificateAuthorityActivateSubordinateCA(rootCertificateAuthori
15151515
})
15161516

15171517
if err != nil {
1518-
return fmt.Errorf("error waiting for ACM PCA Certificate Authority (%s) Subordinate CA certificate to become ISSUED: %w", arn, err)
1518+
return fmt.Errorf("waiting for ACM PCA Certificate Authority (%s) Subordinate CA certificate to become ISSUED: %w", arn, err)
15191519
}
15201520

15211521
getCertOutput, err := conn.GetCertificate(&acmpca.GetCertificateInput{
@@ -1524,7 +1524,7 @@ func CheckACMPCACertificateAuthorityActivateSubordinateCA(rootCertificateAuthori
15241524
})
15251525

15261526
if err != nil {
1527-
return fmt.Errorf("error getting ACM PCA Certificate Authority (%s) issued Subordinate CA certificate: %w", arn, err)
1527+
return fmt.Errorf("getting ACM PCA Certificate Authority (%s) issued Subordinate CA certificate: %w", arn, err)
15281528
}
15291529

15301530
_, err = conn.ImportCertificateAuthorityCertificate(&acmpca.ImportCertificateAuthorityCertificateInput{
@@ -1534,7 +1534,7 @@ func CheckACMPCACertificateAuthorityActivateSubordinateCA(rootCertificateAuthori
15341534
})
15351535

15361536
if err != nil {
1537-
return fmt.Errorf("error importing ACM PCA Certificate Authority (%s) Subordinate CA certificate: %w", arn, err)
1537+
return fmt.Errorf("importing ACM PCA Certificate Authority (%s) Subordinate CA certificate: %w", arn, err)
15381538
}
15391539

15401540
return err

internal/service/ec2/find.go

+37-23
Original file line numberDiff line numberDiff line change
@@ -3358,36 +3358,26 @@ func FindVPNGateway(conn *ec2.EC2, input *ec2.DescribeVpnGatewaysInput) (*ec2.Vp
33583358
return output.VpnGateways[0], nil
33593359
}
33603360

3361-
func FindCustomerGatewayByID(conn *ec2.EC2, id string) (*ec2.CustomerGateway, error) {
3362-
input := &ec2.DescribeCustomerGatewaysInput{
3363-
CustomerGatewayIds: aws.StringSlice([]string{id}),
3364-
}
3365-
3366-
output, err := FindCustomerGateway(conn, input)
3361+
func FindCustomerGateway(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeCustomerGatewaysInput) (*ec2.CustomerGateway, error) {
3362+
output, err := FindCustomerGateways(ctx, conn, input)
33673363

33683364
if err != nil {
33693365
return nil, err
33703366
}
33713367

3372-
if state := aws.StringValue(output.State); state == CustomerGatewayStateDeleted {
3373-
return nil, &resource.NotFoundError{
3374-
Message: state,
3375-
LastRequest: input,
3376-
}
3368+
if len(output) == 0 || output[0] == nil {
3369+
return nil, tfresource.NewEmptyResultError(input)
33773370
}
33783371

3379-
// Eventual consistency check.
3380-
if aws.StringValue(output.CustomerGatewayId) != id {
3381-
return nil, &resource.NotFoundError{
3382-
LastRequest: input,
3383-
}
3372+
if count := len(output); count > 1 {
3373+
return nil, tfresource.NewTooManyResultsError(count, input)
33843374
}
33853375

3386-
return output, nil
3376+
return output[0], nil
33873377
}
33883378

3389-
func FindCustomerGateway(conn *ec2.EC2, input *ec2.DescribeCustomerGatewaysInput) (*ec2.CustomerGateway, error) {
3390-
output, err := conn.DescribeCustomerGateways(input)
3379+
func FindCustomerGateways(ctx context.Context, conn *ec2.EC2, input *ec2.DescribeCustomerGatewaysInput) ([]*ec2.CustomerGateway, error) {
3380+
output, err := conn.DescribeCustomerGatewaysWithContext(ctx, input)
33913381

33923382
if tfawserr.ErrCodeEquals(err, errCodeInvalidCustomerGatewayIDNotFound) {
33933383
return nil, &resource.NotFoundError{
@@ -3400,15 +3390,39 @@ func FindCustomerGateway(conn *ec2.EC2, input *ec2.DescribeCustomerGatewaysInput
34003390
return nil, err
34013391
}
34023392

3403-
if output == nil || len(output.CustomerGateways) == 0 || output.CustomerGateways[0] == nil {
3393+
if output == nil {
34043394
return nil, tfresource.NewEmptyResultError(input)
34053395
}
34063396

3407-
if count := len(output.CustomerGateways); count > 1 {
3408-
return nil, tfresource.NewTooManyResultsError(count, input)
3397+
return output.CustomerGateways, nil
3398+
}
3399+
3400+
func FindCustomerGatewayByID(ctx context.Context, conn *ec2.EC2, id string) (*ec2.CustomerGateway, error) {
3401+
input := &ec2.DescribeCustomerGatewaysInput{
3402+
CustomerGatewayIds: aws.StringSlice([]string{id}),
3403+
}
3404+
3405+
output, err := FindCustomerGateway(ctx, conn, input)
3406+
3407+
if err != nil {
3408+
return nil, err
34093409
}
34103410

3411-
return output.CustomerGateways[0], nil
3411+
if state := aws.StringValue(output.State); state == CustomerGatewayStateDeleted {
3412+
return nil, &resource.NotFoundError{
3413+
Message: state,
3414+
LastRequest: input,
3415+
}
3416+
}
3417+
3418+
// Eventual consistency check.
3419+
if aws.StringValue(output.CustomerGatewayId) != id {
3420+
return nil, &resource.NotFoundError{
3421+
LastRequest: input,
3422+
}
3423+
}
3424+
3425+
return output, nil
34123426
}
34133427

34143428
func FindVPNConnectionByID(conn *ec2.EC2, id string) (*ec2.VpnConnection, error) {

internal/service/ec2/status.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1006,9 +1006,9 @@ func StatusVPNGatewayVPCAttachmentState(conn *ec2.EC2, vpnGatewayID, vpcID strin
10061006
}
10071007
}
10081008

1009-
func StatusCustomerGatewayState(conn *ec2.EC2, id string) resource.StateRefreshFunc {
1009+
func StatusCustomerGatewayState(ctx context.Context, conn *ec2.EC2, id string) resource.StateRefreshFunc {
10101010
return func() (interface{}, string, error) {
1011-
output, err := FindCustomerGatewayByID(conn, id)
1011+
output, err := FindCustomerGatewayByID(ctx, conn, id)
10121012

10131013
if tfresource.NotFound(err) {
10141014
return nil, "", nil

internal/service/ec2/vpnsite_customer_gateway.go

+32-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ec2
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"strconv"
@@ -9,6 +10,7 @@ import (
910
"github.com/aws/aws-sdk-go/aws/arn"
1011
"github.com/aws/aws-sdk-go/service/ec2"
1112
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1416
"github.com/hashicorp/terraform-provider-aws/internal/conns"
@@ -19,10 +21,10 @@ import (
1921

2022
func ResourceCustomerGateway() *schema.Resource {
2123
return &schema.Resource{
22-
Create: resourceCustomerGatewayCreate,
23-
Read: resourceCustomerGatewayRead,
24-
Update: resourceCustomerGatewayUpdate,
25-
Delete: resourceCustomerGatewayDelete,
24+
CreateWithoutTimeout: resourceCustomerGatewayCreate,
25+
ReadWithoutTimeout: resourceCustomerGatewayRead,
26+
UpdateWithoutTimeout: resourceCustomerGatewayUpdate,
27+
DeleteWithoutTimeout: resourceCustomerGatewayDelete,
2628

2729
Importer: &schema.ResourceImporter{
2830
State: schema.ImportStatePassthrough,
@@ -53,7 +55,7 @@ func ResourceCustomerGateway() *schema.Resource {
5355
},
5456
"ip_address": {
5557
Type: schema.TypeString,
56-
Required: true,
58+
Optional: true,
5759
ForceNew: true,
5860
ValidateFunc: validation.IsIPv4Address,
5961
},
@@ -71,13 +73,12 @@ func ResourceCustomerGateway() *schema.Resource {
7173
}
7274
}
7375

74-
func resourceCustomerGatewayCreate(d *schema.ResourceData, meta interface{}) error {
76+
func resourceCustomerGatewayCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
7577
conn := meta.(*conns.AWSClient).EC2Conn
7678
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
7779
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))
7880

7981
input := &ec2.CreateCustomerGatewayInput{
80-
IpAddress: aws.String(d.Get("ip_address").(string)),
8182
TagSpecifications: tagSpecificationsFromKeyValueTags(tags, ec2.ResourceTypeCustomerGateway),
8283
Type: aws.String(d.Get("type").(string)),
8384
}
@@ -86,7 +87,7 @@ func resourceCustomerGatewayCreate(d *schema.ResourceData, meta interface{}) err
8687
v, err := strconv.ParseInt(v.(string), 10, 64)
8788

8889
if err != nil {
89-
return err
90+
return diag.FromErr(err)
9091
}
9192

9293
input.BgpAsn = aws.Int64(v)
@@ -100,28 +101,31 @@ func resourceCustomerGatewayCreate(d *schema.ResourceData, meta interface{}) err
100101
input.DeviceName = aws.String(v.(string))
101102
}
102103

103-
log.Printf("[DEBUG] Creating EC2 Customer Gateway: %s", input)
104-
output, err := conn.CreateCustomerGateway(input)
104+
if v, ok := d.GetOk("ip_address"); ok {
105+
input.IpAddress = aws.String(v.(string))
106+
}
107+
108+
output, err := conn.CreateCustomerGatewayWithContext(ctx, input)
105109

106110
if err != nil {
107-
return fmt.Errorf("error creating EC2 Customer Gateway: %w", err)
111+
return diag.Errorf("creating EC2 Customer Gateway: %s", err)
108112
}
109113

110114
d.SetId(aws.StringValue(output.CustomerGateway.CustomerGatewayId))
111115

112-
if _, err := WaitCustomerGatewayCreated(conn, d.Id()); err != nil {
113-
return fmt.Errorf("error waiting for EC2 Customer Gateway (%s) create: %w", d.Id(), err)
116+
if _, err := WaitCustomerGatewayCreated(ctx, conn, d.Id()); err != nil {
117+
return diag.Errorf("waiting for EC2 Customer Gateway (%s) create: %s", d.Id(), err)
114118
}
115119

116-
return resourceCustomerGatewayRead(d, meta)
120+
return resourceCustomerGatewayRead(ctx, d, meta)
117121
}
118122

119-
func resourceCustomerGatewayRead(d *schema.ResourceData, meta interface{}) error {
123+
func resourceCustomerGatewayRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
120124
conn := meta.(*conns.AWSClient).EC2Conn
121125
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
122126
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig
123127

124-
customerGateway, err := FindCustomerGatewayByID(conn, d.Id())
128+
customerGateway, err := FindCustomerGatewayByID(ctx, conn, d.Id())
125129

126130
if !d.IsNewResource() && tfresource.NotFound(err) {
127131
log.Printf("[WARN] EC2 Customer Gateway (%s) not found, removing from state", d.Id())
@@ -130,7 +134,7 @@ func resourceCustomerGatewayRead(d *schema.ResourceData, meta interface{}) error
130134
}
131135

132136
if err != nil {
133-
return fmt.Errorf("error reading EC2 Customer Gateway (%s): %w", d.Id(), err)
137+
return diag.Errorf("reading EC2 Customer Gateway (%s): %s", d.Id(), err)
134138
}
135139

136140
arn := arn.ARN{
@@ -151,35 +155,35 @@ func resourceCustomerGatewayRead(d *schema.ResourceData, meta interface{}) error
151155

152156
//lintignore:AWSR002
153157
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
154-
return fmt.Errorf("error setting tags: %w", err)
158+
return diag.Errorf("setting tags: %s", err)
155159
}
156160

157161
if err := d.Set("tags_all", tags.Map()); err != nil {
158-
return fmt.Errorf("error setting tags_all: %w", err)
162+
return diag.Errorf("setting tags_all: %s", err)
159163
}
160164

161165
return nil
162166
}
163167

164-
func resourceCustomerGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
168+
func resourceCustomerGatewayUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
165169
conn := meta.(*conns.AWSClient).EC2Conn
166170

167171
if d.HasChange("tags_all") {
168172
o, n := d.GetChange("tags_all")
169173

170-
if err := UpdateTags(conn, d.Id(), o, n); err != nil {
171-
return fmt.Errorf("error updating EC2 Customer Gateway (%s) tags: %w", d.Id(), err)
174+
if err := UpdateTagsWithContext(ctx, conn, d.Id(), o, n); err != nil {
175+
return diag.Errorf("updating EC2 Customer Gateway (%s) tags: %s", d.Id(), err)
172176
}
173177
}
174178

175-
return resourceCustomerGatewayRead(d, meta)
179+
return resourceCustomerGatewayRead(ctx, d, meta)
176180
}
177181

178-
func resourceCustomerGatewayDelete(d *schema.ResourceData, meta interface{}) error {
182+
func resourceCustomerGatewayDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
179183
conn := meta.(*conns.AWSClient).EC2Conn
180184

181185
log.Printf("[INFO] Deleting EC2 Customer Gateway: %s", d.Id())
182-
_, err := conn.DeleteCustomerGateway(&ec2.DeleteCustomerGatewayInput{
186+
_, err := conn.DeleteCustomerGatewayWithContext(ctx, &ec2.DeleteCustomerGatewayInput{
183187
CustomerGatewayId: aws.String(d.Id()),
184188
})
185189

@@ -188,11 +192,11 @@ func resourceCustomerGatewayDelete(d *schema.ResourceData, meta interface{}) err
188192
}
189193

190194
if err != nil {
191-
return fmt.Errorf("error deleting EC2 Customer Gateway (%s): %w", d.Id(), err)
195+
return diag.Errorf("deleting EC2 Customer Gateway (%s): %s", d.Id(), err)
192196
}
193197

194-
if _, err := WaitCustomerGatewayDeleted(conn, d.Id()); err != nil {
195-
return fmt.Errorf("error waiting for EC2 Customer Gateway (%s) delete: %w", d.Id(), err)
198+
if _, err := WaitCustomerGatewayDeleted(ctx, conn, d.Id()); err != nil {
199+
return diag.Errorf("waiting for EC2 Customer Gateway (%s) delete: %s", d.Id(), err)
196200
}
197201

198202
return nil

0 commit comments

Comments
 (0)