Skip to content

Commit

Permalink
fix improperly detecting url changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrconley committed Jun 11, 2024
1 parent 1e6f3c5 commit aed6269
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
5 changes: 4 additions & 1 deletion docs/resources/changefeed.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ Optional:
- `avro_schema_prefix` (String) Avro schema prefix
- `compression` (String) Compression
- `confluent_schema_registry` (String) Confluent schema registry address for avro
- `cursor` (String) Cursor
- `diff` (Boolean) Diff
- `end_time` (String) End time
- `envelope` (String) Envelope
Expand Down Expand Up @@ -74,3 +73,7 @@ Optional:
- `virtual_columns` (String) Virtual columns
- `webhook_auth_header` (String) Webhook auth header
- `webhook_sink_config` (String) Webhook sink config

Read-Only:

- `cursor` (String) Cursor
15 changes: 12 additions & 3 deletions internal/provider/resources/changefeed_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,11 @@ Documentation for the options can be found [here](https://www.cockroachlabs.com/
"cursor": schema.StringAttribute{
MarkdownDescription: "Cursor",
Required: false,
Optional: true,
Optional: false,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"diff": schema.BoolAttribute{
MarkdownDescription: "Diff",
Expand Down Expand Up @@ -630,7 +633,9 @@ func (r *ChangefeedResource) Read(ctx context.Context, req resource.ReadRequest,
//data.Select = types.StringValue(strings.TrimSpace(match[2]))
}

data.SinkUri = types.StringValue(changefeedInfo.uri)
if !CompareURLs(data.SinkUri.ValueString(), changefeedInfo.uri) {
data.SinkUri = types.StringValue(changefeedInfo.uri)
}

// Parse the options
options := strings.Split(strings.Trim(strings.Trim(optionsRaw, "("), ")"), ",")
Expand Down Expand Up @@ -742,7 +747,6 @@ func stringListDelta(source []string, target []string) (added []string, removed

func (r *ChangefeedResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
bannedOptionUpdates := []string{
"cursor",
"end_time",
"full_table_name",
"initial_scan",
Expand Down Expand Up @@ -818,6 +822,11 @@ func (r *ChangefeedResource) Update(ctx context.Context, req resource.UpdateRequ
continue
}

if tag == "cursor" {
data.Options.Cursor = stateData.Options.Cursor
continue
}

if value.Equal(stateValue) {
continue
}
Expand Down
79 changes: 79 additions & 0 deletions internal/provider/resources/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package resources

import (
"net/url"
"sort"
"strings"
)

func CompareURLs(url1, url2 string) bool {
parsedUrl1, err1 := url.Parse(url1)
parsedUrl2, err2 := url.Parse(url2)

if err1 != nil || err2 != nil {
return false
}

if parsedUrl1.Scheme != parsedUrl2.Scheme ||
parsedUrl1.Host != parsedUrl2.Host ||
parsedUrl1.Path != parsedUrl2.Path {
return false
}

params1 := parsedUrl1.Query()
params2 := parsedUrl2.Query()

var redactedSet []string

// remove 'redacted' query params
for key, value := range params1 {
if strings.ToLower(value[0]) == "redacted" {
redactedSet = append(redactedSet, key)
}
}

for key, value := range params2 {
if strings.ToLower(value[0]) == "redacted" {
redactedSet = append(redactedSet, key)
}
}

for _, key := range redactedSet {
params1.Del(key)
params2.Del(key)
}

// sort and compare query params
if len(params1) != len(params2) {
return false
}

keys1 := make([]string, len(params1))
keys2 := make([]string, len(params2))

i := 0
for key := range params1 {
keys1[i] = key
i++
}

i = 0
for key := range params2 {
keys2[i] = key
i++
}

sort.Strings(keys1)
sort.Strings(keys2)

for i := range keys1 {
if keys1[i] != keys2[i] {
return false
}
if params1.Get(keys1[i]) != params2.Get(keys2[i]) {
return false
}
}

return true
}

0 comments on commit aed6269

Please sign in to comment.