Skip to content

Commit 4884bd7

Browse files
Handle 404s in Synthetic Monitoring (#508)
Getting a 404 on a resource means it should be cleared from state and recreated Closes #172
1 parent 5f191c7 commit 4884bd7

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

grafana/resource_synthetic_monitoring_check.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package grafana
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"strconv"
8+
"strings"
79

810
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -555,6 +557,11 @@ func resourceSyntheticMonitoringCheckRead(ctx context.Context, d *schema.Resourc
555557
}
556558
chk, err := c.GetCheck(ctx, id)
557559
if err != nil {
560+
if strings.Contains(err.Error(), "404 Not Found") {
561+
log.Printf("[WARN] removing check %s from state because it no longer exists", d.Id())
562+
d.SetId("")
563+
return nil
564+
}
558565
return diag.FromErr(err)
559566
}
560567

grafana/resource_synthetic_monitoring_check_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package grafana
22

33
import (
4+
"context"
45
"regexp"
6+
"strconv"
57
"testing"
68

79
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
811
)
912

1013
func TestAccResourceSyntheticMonitoringCheck_dns(t *testing.T) {
@@ -235,6 +238,40 @@ func TestAccResourceSyntheticMonitoringCheck_traceroute(t *testing.T) {
235238
})
236239
}
237240

241+
// Test that a check is recreated if deleted outside the Terraform process
242+
func TestAccResourceSyntheticMonitoringCheck_recreate(t *testing.T) {
243+
CheckCloudInstanceTestsEnabled(t)
244+
245+
resource.Test(t, resource.TestCase{
246+
ProviderFactories: testAccProviderFactories,
247+
Steps: []resource.TestStep{
248+
{
249+
Config: testAccExample(t, "resources/grafana_synthetic_monitoring_check/http_basic.tf"),
250+
Check: func(s *terraform.State) error {
251+
rs := s.RootModule().Resources["grafana_synthetic_monitoring_check.http"]
252+
id, _ := strconv.ParseInt(rs.Primary.ID, 10, 64)
253+
return testAccProvider.Meta().(*client).smapi.DeleteCheck(context.Background(), id)
254+
},
255+
ExpectNonEmptyPlan: true,
256+
},
257+
{
258+
Config: testAccExample(t, "resources/grafana_synthetic_monitoring_check/http_basic.tf"),
259+
Check: resource.ComposeTestCheckFunc(
260+
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_check.http", "id"),
261+
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_check.http", "tenant_id"),
262+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "job", "HTTP Defaults"),
263+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "target", "https://grafana.com"),
264+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "probes.0", "1"),
265+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "labels.foo", "bar"),
266+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.ip_version", "V4"),
267+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.method", "GET"),
268+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.no_follow_redirects", "false"),
269+
),
270+
},
271+
},
272+
})
273+
}
274+
238275
func TestAccResourceSyntheticMonitoringCheck_noSettings(t *testing.T) {
239276
CheckCloudInstanceTestsEnabled(t)
240277

grafana/resource_synthetic_monitoring_probe.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/base64"
66
"fmt"
7+
"log"
78
"strconv"
89
"strings"
910

@@ -111,6 +112,11 @@ func resourceSyntheticMonitoringProbeRead(ctx context.Context, d *schema.Resourc
111112
}
112113
prb, err := c.GetProbe(ctx, id)
113114
if err != nil {
115+
if strings.Contains(err.Error(), "404 Not Found") {
116+
log.Printf("[WARN] removing probe %s from state because it no longer exists", d.Id())
117+
d.SetId("")
118+
return nil
119+
}
114120
return diag.FromErr(err)
115121
}
116122

grafana/resource_synthetic_monitoring_probe_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package grafana
22

33
import (
4+
"context"
5+
"strconv"
46
"testing"
57

68
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
710
)
811

912
func TestAccResourceSyntheticMonitoringProbe(t *testing.T) {
@@ -41,3 +44,36 @@ func TestAccResourceSyntheticMonitoringProbe(t *testing.T) {
4144
},
4245
})
4346
}
47+
48+
// Test that a probe is recreated if deleted outside the Terraform process
49+
func TestAccResourceSyntheticMonitoringProbe_recreate(t *testing.T) {
50+
CheckCloudInstanceTestsEnabled(t)
51+
52+
resource.Test(t, resource.TestCase{
53+
ProviderFactories: testAccProviderFactories,
54+
Steps: []resource.TestStep{
55+
{
56+
Config: testAccExample(t, "resources/grafana_synthetic_monitoring_probe/resource.tf"),
57+
Check: func(s *terraform.State) error {
58+
rs := s.RootModule().Resources["grafana_synthetic_monitoring_probe.main"]
59+
id, _ := strconv.ParseInt(rs.Primary.ID, 10, 64)
60+
return testAccProvider.Meta().(*client).smapi.DeleteProbe(context.Background(), id)
61+
},
62+
ExpectNonEmptyPlan: true,
63+
},
64+
{
65+
Config: testAccExample(t, "resources/grafana_synthetic_monitoring_probe/resource.tf"),
66+
Check: resource.ComposeTestCheckFunc(
67+
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_probe.main", "id"),
68+
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_probe.main", "auth_token"),
69+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_probe.main", "name", "Mount Everest"),
70+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_probe.main", "latitude", "27.986059188842773"),
71+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_probe.main", "longitude", "86.92262268066406"),
72+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_probe.main", "region", "APAC"),
73+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_probe.main", "public", "false"),
74+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_probe.main", "labels.type", "mountain"),
75+
),
76+
},
77+
},
78+
})
79+
}

0 commit comments

Comments
 (0)