Skip to content

Commit 03f896e

Browse files
committed
[resource_monitor_json] Avoid restricted role diffs with restricton_policy
This hacks around this (already hacky) resource having conflicts with the restricted_roles attribute when the restriction_policy resource is used to manage permissions on monitors. The general strategy is to ignore roles sent back from the API if roles are not explicitly defined in the monitor. Note: This resource should be convered to a framework provider resource, that should provide much easier access to what the user configured and make this handling consistent and esaier to reason about. Scenarios tested: * create monitor and restriction_policy (with and without roles), there should be no diff when running subsequent plans * modify the monitor and restricton policy, e.g. add/remove a role, there should be no diffs when running subsequent plans Open questions: * what does this do to existing monitor_json resources when a user updates?
1 parent d706ac3 commit 03f896e

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

datadog/resource_datadog_monitor_json.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,19 @@ func resourceDatadogMonitorJSONRead(_ context.Context, d *schema.ResourceData, m
117117
auth := providerConf.Auth
118118

119119
id := d.Id()
120-
respByte, httpResp, err := utils.SendRequest(auth, apiInstances.HttpClient, "GET", monitorPath+"/"+id, nil)
120+
url := monitorPath + "/" + id
121+
122+
// Check if restricted_roles is defined in the JSON, if not explicitly
123+
// defined, we tell the API to not return it so there is no diff. Get
124+
// ("monitor") shouldn't be trusted as it's not the raw values, but we
125+
// try to keep restricted_roles from mixing into it from API responses
126+
monitor := d.Get("monitor").(string)
127+
attrMap, _ := structure.ExpandJsonFromString(monitor)
128+
if _, ok := attrMap["restricted_roles"]; !ok {
129+
url += "?with_restricted_roles=false"
130+
}
131+
132+
respByte, httpResp, err := utils.SendRequest(auth, apiInstances.HttpClient, "GET", url, nil)
121133
if err != nil {
122134
if httpResp != nil && httpResp.StatusCode == 404 {
123135
d.SetId("")
@@ -217,6 +229,17 @@ func updateMonitorJSONState(d *schema.ResourceData, monitor map[string]interface
217229
if val := reflect.ValueOf(monitor["restriction_policy"]); !val.IsValid() {
218230
utils.DeleteKeyInMap(monitor, []string{"restriction_policy"})
219231
}
232+
// In addition to checking the API response, we check to see if the user
233+
// specified restricted_roles in the config. Note: the value returned
234+
// from the ResourceData is not the raw value - it's mixed with state.
235+
// However, using GetRawConfig only returns null values here. If the user
236+
// did not specify restricted_roles, do not store them in the state -
237+
// treat them as a separately managed resource, likely in restriction
238+
// policy resource.
239+
attrMap, _ := structure.ExpandJsonFromString(d.Get("monitor").(string))
240+
if val := reflect.ValueOf(attrMap["restricted_roles"]); !val.IsValid() {
241+
utils.DeleteKeyInMap(monitor, []string{"restricted_roles"})
242+
}
220243

221244
monitorString, err := structure.FlattenJsonToString(monitor)
222245
if err != nil {

0 commit comments

Comments
 (0)