Skip to content

Commit 4d5fc06

Browse files
committed
fix: avoiding drift for the default public endpoint responses
1 parent f8e4927 commit 4d5fc06

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

provider/pro/resource_rediscloud_pro_database.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,28 @@ func resourceRedisCloudProDatabaseRead(ctx context.Context, d *schema.ResourceDa
600600
return diag.FromErr(err)
601601
}
602602

603+
// Handle source_ips defaults to avoid Terraform drift:
604+
// - When public_endpoint_access=true and source_ips empty: API returns ["0.0.0.0/0"]
605+
// - When public_endpoint_access=false and source_ips empty: API returns RFC1918 private ranges
606+
// Only set source_ips in state if explicitly configured by user (not provider defaults)
603607
var sourceIPs []string
604-
if !(len(db.Security.SourceIPs) == 1 && redis.StringValue(db.Security.SourceIPs[0]) == "0.0.0.0/0") {
605-
// The API handles an empty list as ["0.0.0.0/0"] but need to be careful to match the input to avoid Terraform detecting drift
608+
609+
// Check if returned source_ips matches default public access ["0.0.0.0/0"]
610+
isDefaultPublicAccess := len(db.Security.SourceIPs) == 1 && redis.StringValue(db.Security.SourceIPs[0]) == "0.0.0.0/0"
611+
612+
// Check if returned source_ips matches default RFC1918 private ranges
613+
isDefaultPrivateRanges := len(db.Security.SourceIPs) == len(defaultPrivateIPRanges)
614+
if isDefaultPrivateRanges {
615+
for i, ip := range db.Security.SourceIPs {
616+
if redis.StringValue(ip) != defaultPrivateIPRanges[i] {
617+
isDefaultPrivateRanges = false
618+
break
619+
}
620+
}
621+
}
622+
623+
// Only set source_ips in state if explicitly configured by user (not defaults)
624+
if !isDefaultPublicAccess && !isDefaultPrivateRanges {
606625
sourceIPs = redis.StringSliceValue(db.Security.SourceIPs...)
607626
}
608627

@@ -729,8 +748,9 @@ func resourceRedisCloudProDatabaseUpdate(ctx context.Context, d *schema.Resource
729748
}
730749
}
731750

732-
// The below fields are optional and will only be sent in the request if they are present in the Terraform configuration
733-
// When source_ips is not specified, default based on subscription's public_endpoint_access setting
751+
// Handle source_ips defaults based on subscription's public_endpoint_access setting:
752+
// - When public_endpoint_access=true and source_ips empty: default to ["0.0.0.0/0"]
753+
// - When public_endpoint_access=false and source_ips empty: default to RFC1918 private ranges
734754
if len(utils.SetToStringSlice(d.Get("source_ips").(*schema.Set))) == 0 {
735755
// Fetch subscription to check public_endpoint_access setting
736756
subscription, err := api.Client.Subscription.Get(ctx, subId)
@@ -739,16 +759,15 @@ func resourceRedisCloudProDatabaseUpdate(ctx context.Context, d *schema.Resource
739759
return diag.FromErr(err)
740760
}
741761

742-
// If public endpoint access is blocked, default to RFC1918 private IP ranges
762+
// Set defaults based on public_endpoint_access
743763
if subscription.PublicEndpointAccess != nil && !*subscription.PublicEndpointAccess {
744-
update.SourceIP = []*string{
745-
redis.String("10.0.0.0/8"),
746-
redis.String("172.16.0.0/12"),
747-
redis.String("192.168.0.0/16"),
748-
redis.String("100.64.0.0/10"),
764+
// Public access blocked: default to RFC1918 private ranges
765+
update.SourceIP = make([]*string, len(defaultPrivateIPRanges))
766+
for i, cidr := range defaultPrivateIPRanges {
767+
update.SourceIP[i] = redis.String(cidr)
749768
}
750769
} else {
751-
// Default to public access
770+
// Public access allowed: default to public access
752771
update.SourceIP = []*string{redis.String("0.0.0.0/0")}
753772
}
754773
}

0 commit comments

Comments
 (0)