@@ -14,14 +14,29 @@ import (
1414 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1515 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1616 "github.com/hashicorp/terraform-plugin-framework/types"
17-
1817 "github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
1918)
2019
2120var (
2221 integrationGcpStsMutex sync.Mutex
2322 _ resource.ResourceWithConfigure = & integrationGcpStsResource {}
2423 _ resource.ResourceWithImportState = & integrationGcpStsResource {}
24+
25+ MetricNamespaceConfigSpec = types.ObjectType {
26+ AttrTypes : map [string ]attr.Type {
27+ "id" : types .StringType ,
28+ "disabled" : types .BoolType ,
29+ },
30+ }
31+
32+ MonitoredResourceConfigSpec = types.ObjectType {
33+ AttrTypes : map [string ]attr.Type {
34+ "type" : types .StringType ,
35+ "filters" : types.SetType {
36+ ElemType : types .StringType ,
37+ },
38+ },
39+ }
2540)
2641
2742type integrationGcpStsResource struct {
@@ -33,36 +48,26 @@ type MetricNamespaceConfigModel struct {
3348 ID types.String `tfsdk:"id"`
3449 Disabled types.Bool `tfsdk:"disabled"`
3550}
36-
37- var MonitoredResourceConfigSpec = types.ObjectType {
38- AttrTypes : map [string ]attr.Type {
39- "type" : types .StringType ,
40- "filters" : types.SetType {
41- ElemType : types .StringType ,
42- },
43- },
44- }
45-
4651type MonitoredResourceConfigModel struct {
4752 Type types.String `tfsdk:"type"`
4853 Filters types.Set `tfsdk:"filters"`
4954}
5055
5156type integrationGcpStsModel struct {
52- ID types.String `tfsdk:"id"`
53- AccountTags types.Set `tfsdk:"account_tags"`
54- Automute types.Bool `tfsdk:"automute"`
55- ClientEmail types.String `tfsdk:"client_email"`
56- DelegateAccountEmail types.String `tfsdk:"delegate_account_email"`
57- HostFilters types.Set `tfsdk:"host_filters"` // DEPRECATED: use MonitoredResourceConfigs["gce_instance"]
58- CloudRunRevisionFilters types.Set `tfsdk:"cloud_run_revision_filters"` // DEPRECATED: use MonitoredResourceConfigs["cloud_run_revision"]
59- IsCspmEnabled types.Bool `tfsdk:"is_cspm_enabled"`
60- IsSecurityCommandCenterEnabled types.Bool `tfsdk:"is_security_command_center_enabled"`
61- IsResourceChangeCollectionEnabled types.Bool `tfsdk:"is_resource_change_collection_enabled"`
62- IsPerProjectQuotaEnabled types.Bool `tfsdk:"is_per_project_quota_enabled"`
63- ResourceCollectionEnabled types.Bool `tfsdk:"resource_collection_enabled"`
64- MetricNamespaceConfigs [] * MetricNamespaceConfigModel `tfsdk:"metric_namespace_configs"`
65- MonitoredResourceConfigs types.Set `tfsdk:"monitored_resource_configs"`
57+ ID types.String `tfsdk:"id"`
58+ AccountTags types.Set `tfsdk:"account_tags"`
59+ Automute types.Bool `tfsdk:"automute"`
60+ ClientEmail types.String `tfsdk:"client_email"`
61+ DelegateAccountEmail types.String `tfsdk:"delegate_account_email"`
62+ HostFilters types.Set `tfsdk:"host_filters"` // DEPRECATED: use MonitoredResourceConfigs["gce_instance"]
63+ CloudRunRevisionFilters types.Set `tfsdk:"cloud_run_revision_filters"` // DEPRECATED: use MonitoredResourceConfigs["cloud_run_revision"]
64+ IsCspmEnabled types.Bool `tfsdk:"is_cspm_enabled"`
65+ IsSecurityCommandCenterEnabled types.Bool `tfsdk:"is_security_command_center_enabled"`
66+ IsResourceChangeCollectionEnabled types.Bool `tfsdk:"is_resource_change_collection_enabled"`
67+ IsPerProjectQuotaEnabled types.Bool `tfsdk:"is_per_project_quota_enabled"`
68+ ResourceCollectionEnabled types.Bool `tfsdk:"resource_collection_enabled"`
69+ MetricNamespaceConfigs types. Set `tfsdk:"metric_namespace_configs"`
70+ MonitoredResourceConfigs types.Set `tfsdk:"monitored_resource_configs"`
6671}
6772
6873func NewIntegrationGcpStsResource () resource.Resource {
@@ -152,14 +157,11 @@ func (r *integrationGcpStsResource) Schema(_ context.Context, _ resource.SchemaR
152157 Computed : true ,
153158 },
154159 "metric_namespace_configs" : schema.SetAttribute {
155- Optional : true ,
160+ Optional : true ,
161+ // if this field is not set by the user, then we will disable prometheus by default
162+ Computed : true ,
156163 Description : "Configurations for GCP metric namespaces." ,
157- ElementType : types.ObjectType {
158- AttrTypes : map [string ]attr.Type {
159- "id" : types .StringType ,
160- "disabled" : types .BoolType ,
161- },
162- },
164+ ElementType : MetricNamespaceConfigSpec ,
163165 },
164166 "monitored_resource_configs" : schema.SetAttribute {
165167 Optional : true ,
@@ -266,6 +268,7 @@ func (r *integrationGcpStsResource) Create(ctx context.Context, request resource
266268
267269func (r * integrationGcpStsResource ) Update (ctx context.Context , request resource.UpdateRequest , response * resource.UpdateResponse ) {
268270 var state integrationGcpStsModel
271+
269272 response .Diagnostics .Append (request .Plan .Get (ctx , & state )... )
270273 if response .Diagnostics .HasError () {
271274 return
@@ -353,15 +356,14 @@ func (r *integrationGcpStsResource) updateState(ctx context.Context, state *inte
353356 state .AccountTags , _ = types .SetValueFrom (ctx , types .StringType , accountTags )
354357 }
355358
356- if mncs := attributes .GetMetricNamespaceConfigs (); len (mncs ) > 0 {
357- state .MetricNamespaceConfigs = make ([]* MetricNamespaceConfigModel , 0 , len (mncs ))
358- for _ , mnc := range mncs {
359- state .MetricNamespaceConfigs = append (state .MetricNamespaceConfigs , & MetricNamespaceConfigModel {
360- ID : types .StringValue (mnc .GetId ()),
361- Disabled : types .BoolValue (mnc .GetDisabled ()),
362- })
363- }
359+ mncs := make ([]* MetricNamespaceConfigModel , 0 )
360+ for _ , mnc := range attributes .GetMetricNamespaceConfigs () {
361+ mncs = append (mncs , & MetricNamespaceConfigModel {
362+ ID : types .StringValue (mnc .GetId ()),
363+ Disabled : types .BoolValue (mnc .GetDisabled ()),
364+ })
364365 }
366+ state .MetricNamespaceConfigs , _ = types .SetValueFrom (ctx , MetricNamespaceConfigSpec , mncs )
365367
366368 state .HostFilters , _ = types .SetValueFrom (ctx , types .StringType , attributes .GetHostFilters ())
367369 state .CloudRunRevisionFilters , _ = types .SetValueFrom (ctx , types .StringType , attributes .GetCloudRunRevisionFilters ())
@@ -400,14 +402,18 @@ func (r *integrationGcpStsResource) buildIntegrationGcpStsRequestBody(ctx contex
400402
401403 attributes .SetAccountTags (tfCollectionToSlice [string ](ctx , diags , state .AccountTags ))
402404
403- mncs := make ([]datadogV2.GCPMetricNamespaceConfig , 0 )
404- for _ , mnc := range state .MetricNamespaceConfigs {
405- mncs = append (mncs , datadogV2.GCPMetricNamespaceConfig {
406- Id : mnc .ID .ValueStringPointer (),
407- Disabled : mnc .Disabled .ValueBoolPointer (),
408- })
405+ // only set this field if the user explicitly sets the field
406+ // otherwise we want to omit it so that the API server can populate defaults when applicable
407+ if cfgs := state .MetricNamespaceConfigs ; ! cfgs .IsUnknown () {
408+ mncs := make ([]datadogV2.GCPMetricNamespaceConfig , 0 )
409+ for _ , mnc := range tfCollectionToSlice [* MetricNamespaceConfigModel ](ctx , diags , cfgs ) {
410+ mncs = append (mncs , datadogV2.GCPMetricNamespaceConfig {
411+ Id : mnc .ID .ValueStringPointer (),
412+ Disabled : mnc .Disabled .ValueBoolPointer (),
413+ })
414+ }
415+ attributes .SetMetricNamespaceConfigs (mncs )
409416 }
410- attributes .SetMetricNamespaceConfigs (mncs )
411417
412418 attributes .SetHostFilters (tfCollectionToSlice [string ](ctx , diags , state .HostFilters ))
413419 attributes .SetCloudRunRevisionFilters (tfCollectionToSlice [string ](ctx , diags , state .CloudRunRevisionFilters ))
0 commit comments