Skip to content

Commit ef52330

Browse files
authored
delete pdc fields from json_data when generating updated state (#2439)
* delete pdc fields from json_data when generating updated state * explicitly set PDC fields as reserved in jsondata
1 parent ad60f1b commit ef52330

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

internal/resources/grafana/resource_data_source.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import (
1919
"github.com/grafana/terraform-provider-grafana/v4/internal/common"
2020
)
2121

22+
const (
23+
pdcEnableSecureSocksProxy = "enableSecureSocksProxy"
24+
pdcSecureSocksProxyUsername = "secureSocksProxyUsername"
25+
)
26+
2227
func resourceDataSource() *common.Resource {
2328
schema := &schema.Resource{
2429

@@ -167,6 +172,17 @@ func datasourceJSONDataAttribute() *schema.Schema {
167172
errors.New("teamHttpHeaders is a reserved key and cannot be used in JSON data. Use the data_source_config_lbac_rules resource instead"),
168173
}
169174
}
175+
176+
if strings.Contains(i.(string), pdcEnableSecureSocksProxy) {
177+
return nil, []error{
178+
errors.New(pdcEnableSecureSocksProxy + " is a reserved key and cannot be used in JSON data"),
179+
}
180+
}
181+
if strings.Contains(i.(string), pdcSecureSocksProxyUsername) {
182+
return nil, []error{
183+
errors.New(pdcSecureSocksProxyUsername + " is a reserved key and cannot be used in JSON data"),
184+
}
185+
}
170186
return validation.StringIsJSON(i, s)
171187
},
172188
StateFunc: func(v any) string {
@@ -182,8 +198,8 @@ func datasourceJSONDataAttribute() *schema.Schema {
182198
json.Unmarshal([]byte(newValue), &newValueUnmarshalled)
183199
pdcNetworkID := d.Get("private_data_source_connect_network_id")
184200
if pdcNetworkID != "" {
185-
newValueUnmarshalled["enableSecureSocksProxy"] = true
186-
newValueUnmarshalled["secureSocksProxyUsername"] = pdcNetworkID
201+
newValueUnmarshalled[pdcEnableSecureSocksProxy] = true
202+
newValueUnmarshalled[pdcSecureSocksProxyUsername] = pdcNetworkID
187203
}
188204
newValue, _ = structure.FlattenJsonToString(newValueUnmarshalled)
189205

@@ -333,6 +349,14 @@ func datasourceToState(d *schema.ResourceData, dataSource *models.DataSource) di
333349

334350
func datasourceConfigToState(d *schema.ResourceData, dataSource *models.DataSource) diag.Diagnostics {
335351
gottenJSONData, gottenHeaders := removeHeadersFromJSONData(dataSource.JSONData.(map[string]any))
352+
353+
// These PDC fields are added by the provider, so we should remove them from
354+
// the state so that the state matches the user config. The consequence of
355+
// having a diff here is suppressed in the DiffSuppressFunc, but there is
356+
// a risk that the encoding of the map provides an inconsistent result.
357+
delete(gottenJSONData, pdcEnableSecureSocksProxy)
358+
delete(gottenJSONData, pdcSecureSocksProxyUsername)
359+
336360
encodedJSONData, err := json.Marshal(gottenJSONData)
337361
if err != nil {
338362
return diag.Errorf("Failed to marshal JSON data: %s", err)
@@ -390,8 +414,8 @@ func stateToDatasourceConfig(d *schema.ResourceData) (map[string]any, map[string
390414
pdcNetworkID := d.Get("private_data_source_connect_network_id")
391415
if pdcNetworkID != nil {
392416
if id := pdcNetworkID.(string); id != "" {
393-
jd["enableSecureSocksProxy"] = true
394-
jd["secureSocksProxyUsername"] = pdcNetworkID
417+
jd[pdcEnableSecureSocksProxy] = true
418+
jd[pdcSecureSocksProxyUsername] = pdcNetworkID
395419
}
396420
}
397421

internal/resources/grafana/resource_data_source_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,83 @@ func TestAccDataSource_ValidateHttpHeaders(t *testing.T) {
341341
})
342342
}
343343

344+
func TestAccDataSource_PDCReservedProperties(t *testing.T) {
345+
testutils.CheckOSSTestsEnabled(t)
346+
347+
t.Run("enableSecureSocksProxy", func(t *testing.T) {
348+
resource.ParallelTest(t, resource.TestCase{
349+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
350+
Steps: []resource.TestStep{
351+
{
352+
Config: `
353+
resource "grafana_data_source" "influx" {
354+
type = "influxdb"
355+
name = "anything"
356+
url = "http://acc-test.invalid/"
357+
json_data_encoded = jsonencode({
358+
enableSecureSocksProxy = true
359+
})
360+
}`,
361+
ExpectError: regexp.MustCompile(`enableSecureSocksProxy is a reserved key and cannot be used in JSON data`),
362+
},
363+
},
364+
})
365+
})
366+
367+
t.Run("secureSocksProxyUsername", func(t *testing.T) {
368+
resource.ParallelTest(t, resource.TestCase{
369+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
370+
Steps: []resource.TestStep{
371+
{
372+
Config: `
373+
resource "grafana_data_source" "influx" {
374+
type = "influxdb"
375+
name = "anything"
376+
url = "http://acc-test.invalid/"
377+
json_data_encoded = jsonencode({
378+
secureSocksProxyUsername = "pdc-network-id"
379+
})
380+
}`,
381+
ExpectError: regexp.MustCompile(`secureSocksProxyUsername is a reserved key and cannot be used in JSON data`),
382+
},
383+
},
384+
})
385+
})
386+
}
387+
388+
func TestAccDatasource_PDCPropertiesRemovedFromState(t *testing.T) {
389+
testutils.CheckOSSTestsEnabled(t)
390+
391+
var dataSource models.DataSource
392+
var org models.OrgDetailsDTO
393+
394+
resource.ParallelTest(t, resource.TestCase{
395+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
396+
CheckDestroy: datasourceCheckExists.destroyed(&dataSource, &org),
397+
Steps: []resource.TestStep{
398+
{
399+
Config: `
400+
resource "grafana_data_source" "pdc" {
401+
type = "influxdb"
402+
name = "pdc"
403+
url = "http://acc-test.invalid/"
404+
json_data_encoded = jsonencode({
405+
organization = "organization"
406+
tlsAuth = false
407+
})
408+
private_data_source_connect_network_id = "pdc-network-id"
409+
}`,
410+
Check: resource.ComposeTestCheckFunc(
411+
// Check that the datasource is in the correct organization
412+
datasourceCheckExists.exists("grafana_data_source.pdc", &dataSource),
413+
// Check that the PDC-related fields are not in the state json data
414+
resource.TestCheckResourceAttr("grafana_data_source.pdc", "json_data_encoded", "{\"organization\":\"organization\",\"tlsAuth\":false}"),
415+
),
416+
},
417+
},
418+
})
419+
}
420+
344421
func TestAccDataSource_SeparateConfig(t *testing.T) {
345422
testutils.CheckOSSTestsEnabled(t, ">=v9.0.0")
346423

0 commit comments

Comments
 (0)