From 71df7e5b5d09b44d046b75392ea14994ad726476 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Mon, 22 Apr 2024 12:11:41 +0000 Subject: [PATCH 1/3] diagnostic setting wait resource to be ready --- .../monitor_diagnostic_setting_resource.go | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/services/monitor/monitor_diagnostic_setting_resource.go b/internal/services/monitor/monitor_diagnostic_setting_resource.go index 75ce969144eb..74672f01820a 100644 --- a/internal/services/monitor/monitor_diagnostic_setting_resource.go +++ b/internal/services/monitor/monitor_diagnostic_setting_resource.go @@ -361,6 +361,20 @@ func resourceMonitorDiagnosticSettingCreate(d *pluginsdk.ResourceData, meta inte return fmt.Errorf("creating Monitor Diagnostics Setting %q for Resource %q: %+v", id.DiagnosticSettingName, id.ResourceUri, err) } + log.Printf("[DEBUG] Waiting for Monitor Diagnostic Setting %q for Resource %q to become ready", id.DiagnosticSettingName, id.ResourceUri) + stateConf := &pluginsdk.StateChangeConf{ + Pending: []string{"NotFound"}, + Target: []string{"Exists"}, + Refresh: monitorDiagnosticSettingRefreshFunc(ctx, client, id), + MinTimeout: 5 * time.Second, + ContinuousTargetOccurence: 3, + Timeout: d.Timeout(pluginsdk.TimeoutCreate), + } + + if _, err = stateConf.WaitForStateContext(ctx); err != nil { + return fmt.Errorf("waiting for Monitor Diagnostic Setting %q for Resource %q to become ready: %s", id.DiagnosticSettingName, id.ResourceUri, err) + } + d.SetId(resourceId) return resourceMonitorDiagnosticSettingRead(d, meta) @@ -600,27 +614,27 @@ func resourceMonitorDiagnosticSettingDelete(d *pluginsdk.ResourceData, meta inte stateConf := &pluginsdk.StateChangeConf{ Pending: []string{"Exists"}, Target: []string{"NotFound"}, - Refresh: monitorDiagnosticSettingDeletedRefreshFunc(ctx, client, *id), + Refresh: monitorDiagnosticSettingRefreshFunc(ctx, client, *id), MinTimeout: 15 * time.Second, ContinuousTargetOccurence: 5, Timeout: d.Timeout(pluginsdk.TimeoutDelete), } if _, err = stateConf.WaitForStateContext(ctx); err != nil { - return fmt.Errorf("waiting for Monitor Diagnostic Setting %q for Resource %q to become available: %s", id.DiagnosticSettingName, id.ResourceUri, err) + return fmt.Errorf("waiting for Monitor Diagnostic Setting %q for Resource %q to disappear: %s", id.DiagnosticSettingName, id.ResourceUri, err) } return nil } -func monitorDiagnosticSettingDeletedRefreshFunc(ctx context.Context, client *diagnosticsettings.DiagnosticSettingsClient, targetResourceId diagnosticsettings.ScopedDiagnosticSettingId) pluginsdk.StateRefreshFunc { +func monitorDiagnosticSettingRefreshFunc(ctx context.Context, client *diagnosticsettings.DiagnosticSettingsClient, targetResourceId diagnosticsettings.ScopedDiagnosticSettingId) pluginsdk.StateRefreshFunc { return func() (interface{}, string, error) { res, err := client.Get(ctx, targetResourceId) if err != nil { if response.WasNotFound(res.HttpResponse) { return "NotFound", "NotFound", nil } - return nil, "", fmt.Errorf("issuing read request in monitorDiagnosticSettingDeletedRefreshFunc: %s", err) + return nil, "", fmt.Errorf("issuing read request in monitorDiagnosticSettingRefreshFunc: %s", err) } return res, "Exists", nil From babc8bc2068969a0e1623d6288af7176d4f36338 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Fri, 26 Apr 2024 08:33:52 +0000 Subject: [PATCH 2/3] use the deadline from context --- .../monitor/monitor_diagnostic_setting_resource.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/services/monitor/monitor_diagnostic_setting_resource.go b/internal/services/monitor/monitor_diagnostic_setting_resource.go index 74672f01820a..18fd322bdbb0 100644 --- a/internal/services/monitor/monitor_diagnostic_setting_resource.go +++ b/internal/services/monitor/monitor_diagnostic_setting_resource.go @@ -361,6 +361,11 @@ func resourceMonitorDiagnosticSettingCreate(d *pluginsdk.ResourceData, meta inte return fmt.Errorf("creating Monitor Diagnostics Setting %q for Resource %q: %+v", id.DiagnosticSettingName, id.ResourceUri, err) } + deadline, ok := ctx.Deadline() + if !ok { + return fmt.Errorf("internal error: could not retrieve context deadline for %s", id.ID()) + } + log.Printf("[DEBUG] Waiting for Monitor Diagnostic Setting %q for Resource %q to become ready", id.DiagnosticSettingName, id.ResourceUri) stateConf := &pluginsdk.StateChangeConf{ Pending: []string{"NotFound"}, @@ -368,7 +373,7 @@ func resourceMonitorDiagnosticSettingCreate(d *pluginsdk.ResourceData, meta inte Refresh: monitorDiagnosticSettingRefreshFunc(ctx, client, id), MinTimeout: 5 * time.Second, ContinuousTargetOccurence: 3, - Timeout: d.Timeout(pluginsdk.TimeoutCreate), + Timeout: time.Until(deadline), } if _, err = stateConf.WaitForStateContext(ctx); err != nil { @@ -609,6 +614,11 @@ func resourceMonitorDiagnosticSettingDelete(d *pluginsdk.ResourceData, meta inte } } + deadline, ok := ctx.Deadline() + if !ok { + return fmt.Errorf("internal error: could not retrieve context deadline for %s", id.ID()) + } + // API appears to be eventually consistent (identified during tainting this resource) log.Printf("[DEBUG] Waiting for Monitor Diagnostic Setting %q for Resource %q to disappear", id.DiagnosticSettingName, id.ResourceUri) stateConf := &pluginsdk.StateChangeConf{ @@ -617,7 +627,7 @@ func resourceMonitorDiagnosticSettingDelete(d *pluginsdk.ResourceData, meta inte Refresh: monitorDiagnosticSettingRefreshFunc(ctx, client, *id), MinTimeout: 15 * time.Second, ContinuousTargetOccurence: 5, - Timeout: d.Timeout(pluginsdk.TimeoutDelete), + Timeout: time.Until(deadline), } if _, err = stateConf.WaitForStateContext(ctx); err != nil { From 47c6fb147f9f16b50efa0f7bdaaff3a900f2c708 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Mon, 19 Aug 2024 03:17:41 +0000 Subject: [PATCH 3/3] link issue --- internal/services/monitor/monitor_diagnostic_setting_resource.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/services/monitor/monitor_diagnostic_setting_resource.go b/internal/services/monitor/monitor_diagnostic_setting_resource.go index 18fd322bdbb0..e4b469d51000 100644 --- a/internal/services/monitor/monitor_diagnostic_setting_resource.go +++ b/internal/services/monitor/monitor_diagnostic_setting_resource.go @@ -366,6 +366,7 @@ func resourceMonitorDiagnosticSettingCreate(d *pluginsdk.ResourceData, meta inte return fmt.Errorf("internal error: could not retrieve context deadline for %s", id.ID()) } + // https://github.com/Azure/azure-rest-api-specs/issues/30249 log.Printf("[DEBUG] Waiting for Monitor Diagnostic Setting %q for Resource %q to become ready", id.DiagnosticSettingName, id.ResourceUri) stateConf := &pluginsdk.StateChangeConf{ Pending: []string{"NotFound"},