diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a0fcaf4c..94a3eac1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ NEW FEATURES: * Add support for listing Consul peers [NET-6966](https://hashicorp.atlassian.net/browse/NET-6966) +BUG FIXES: +* Fetch services query not overriding opts correctly [NET-7571](https://hashicorp.atlassian.net/browse/NET-7571) + ## v0.36.0 (January 3, 2024) IMPROVEMENTS: diff --git a/dependency/catalog_services.go b/dependency/catalog_services.go index af62e2c1c..c4376b8c3 100644 --- a/dependency/catalog_services.go +++ b/dependency/catalog_services.go @@ -71,11 +71,17 @@ func (d *CatalogServicesQuery) Fetch(clients *ClientSet, opts *QueryOptions) (in default: } - opts = opts.Merge(&QueryOptions{ + // this overrides the query params present in the query with ones present while creating the query + // see bug [https://github.com/hashicorp/consul-template/pull/1842#issuecomment-1915723565] + // default to the query params present while creating NewCatalogServicesQuery + // and then merge with the query params present in the query + defaultOpts := &QueryOptions{ Datacenter: d.dc, ConsulPartition: d.partition, ConsulNamespace: d.namespace, - }) + } + + opts = defaultOpts.Merge(opts) log.Printf("[TRACE] %s: GET %s", d, &url.URL{ Path: "/v1/catalog/services", diff --git a/dependency/catalog_services_test.go b/dependency/catalog_services_test.go index 928601e8c..d37376569 100644 --- a/dependency/catalog_services_test.go +++ b/dependency/catalog_services_test.go @@ -100,11 +100,14 @@ func TestCatalogServicesQuery_Fetch(t *testing.T) { cases := []struct { name string i string + opts *QueryOptions exp []*CatalogSnippet + err bool }{ { "all", "", + nil, []*CatalogSnippet{ { Name: "consul", @@ -123,21 +126,27 @@ func TestCatalogServicesQuery_Fetch(t *testing.T) { Tags: ServiceTags([]string{}), }, }, + false, }, } for i, tc := range cases { t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { + d, err := NewCatalogServicesQuery(tc.i) if err != nil { t.Fatal(err) } - act, _, err := d.Fetch(testClients, nil) - if err != nil { + act, _, err := d.Fetch(testClients, tc.opts) + if (err != nil) != tc.err { t.Fatal(err) } + if act == nil && tc.err { + return + } + assert.Equal(t, tc.exp, act) }) }