Skip to content

Commit 068f24e

Browse files
committed
fix: added wait and polling logic to the tgw
1 parent 6d525c5 commit 068f24e

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

provider/datasource_rediscloud_active_active_transit_gateway.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,13 @@ func dataSourceActiveActiveTransitGatewayRead(ctx context.Context, d *schema.Res
8080
return diag.FromErr(err)
8181
}
8282
regionId := d.Get("region_id").(int)
83-
tgwTask, err := api.Client.TransitGatewayAttachments.GetActiveActive(ctx, subId, regionId)
83+
84+
// Wait for Transit Gateway resource to become available (handles subscription provisioning delays)
85+
tgwTask, err := utils.WaitForActiveActiveTransitGatewayResourceToBeAvailable(ctx, subId, regionId, api)
8486
if err != nil {
8587
return diag.FromErr(err)
8688
}
8789

88-
// Check for nil response structure
89-
if tgwTask == nil {
90-
return diag.Errorf("Transit Gateway API returned nil task for subscription %d, region %d", subId, regionId)
91-
}
92-
if tgwTask.Response == nil {
93-
return diag.Errorf("Transit Gateway API returned nil response for subscription %d, region %d", subId, regionId)
94-
}
95-
if tgwTask.Response.Resource == nil {
96-
return diag.Errorf("Transit Gateway API returned nil resource for subscription %d, region %d - subscription may not be fully provisioned yet", subId, regionId)
97-
}
98-
9990
var filters []func(db *attachments.TransitGatewayAttachment) bool
10091

10192
if v, ok := d.GetOk("tgw_id"); ok {

provider/resource_rediscloud_active_active_transit_gateway_attachment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ func resourceRedisCloudActiveActiveTransitGatewayAttachmentRead(ctx context.Cont
123123
}
124124
tgwId := d.Get("tgw_id").(int)
125125

126-
tgwTask, err := api.Client.TransitGatewayAttachments.GetActiveActive(ctx, subId, regionId)
126+
// Wait for Transit Gateway resource to become available (handles subscription provisioning delays)
127+
tgwTask, err := utils.WaitForActiveActiveTransitGatewayResourceToBeAvailable(ctx, subId, regionId, api)
127128
if err != nil {
128129
return diag.FromErr(err)
129130
}

provider/utils/get_set.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
// Unfortunately there's no "time-remaining-before-timeout" utility, or we could use that in the wait blocks.
1313
const SafetyTimeout = 6 * time.Hour
1414

15+
// TransitGatewayProvisioningTimeout is used when waiting for Transit Gateway resources to become available during
16+
// subscription provisioning. This is shorter than SafetyTimeout as tests typically complete within 45 minutes.
17+
const TransitGatewayProvisioningTimeout = 40 * time.Minute
18+
1519
// GetString safely retrieves a string value from schema.ResourceData.
1620
func GetString(d *schema.ResourceData, key string) *string {
1721
if v, ok := d.GetOk(key); ok {

provider/utils/wait.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package utils
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/RedisLabs/rediscloud-go-api/service/databases"
7+
"github.com/RedisLabs/rediscloud-go-api/service/transit_gateway/attachments"
68
"log"
79
"time"
810

@@ -77,3 +79,40 @@ func WaitForDatabaseToBeActive(ctx context.Context, subId, id int, api *client.A
7779

7880
return nil
7981
}
82+
83+
// WaitForActiveActiveTransitGatewayResourceToBeAvailable waits for Active-Active Transit Gateway API resources
84+
// to become available. This handles the case where Response.Resource is nil during initial subscription provisioning.
85+
func WaitForActiveActiveTransitGatewayResourceToBeAvailable(ctx context.Context, subId int, regionId int, api *client.ApiClient) (*attachments.GetAttachmentsTask, error) {
86+
wait := &retry.StateChangeConf{
87+
Pending: []string{"provisioning"},
88+
Target: []string{"available"},
89+
Timeout: TransitGatewayProvisioningTimeout,
90+
Delay: 10 * time.Second,
91+
PollInterval: 30 * time.Second,
92+
93+
Refresh: func() (result interface{}, state string, err error) {
94+
log.Printf("[DEBUG] Waiting for Active-Active Transit Gateway resource to be available for subscription %d, region %d", subId, regionId)
95+
96+
tgwTask, err := api.Client.TransitGatewayAttachments.GetActiveActive(ctx, subId, regionId)
97+
if err != nil {
98+
return nil, "", err
99+
}
100+
101+
// Check for nil response structure during provisioning
102+
if tgwTask == nil || tgwTask.Response == nil || tgwTask.Response.Resource == nil {
103+
return nil, "provisioning", nil
104+
}
105+
106+
return tgwTask, "available", nil
107+
},
108+
}
109+
110+
result, err := wait.WaitForStateContext(ctx)
111+
if err != nil {
112+
return nil, fmt.Errorf("timeout waiting for Active-Active Transit Gateway resource to become available for subscription %d, region %d. "+
113+
"This may indicate the subscription is still provisioning or there's an issue with the subscription setup. "+
114+
"Original error: %w", subId, regionId, err)
115+
}
116+
117+
return result.(*attachments.GetAttachmentsTask), nil
118+
}

0 commit comments

Comments
 (0)