From 5cae4033c230c8b21728473e96ef05c01ad07aa1 Mon Sep 17 00:00:00 2001 From: Andrew Starr-Bochicchio Date: Fri, 1 Mar 2024 18:16:59 -0500 Subject: [PATCH 1/2] uptime alerts: fix importing existing alerts --- digitalocean/uptime/resource_uptime_alert.go | 17 ++++++++++++++++- docs/resources/uptime_alert.md | 5 +++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/digitalocean/uptime/resource_uptime_alert.go b/digitalocean/uptime/resource_uptime_alert.go index cfc9d4235..52003fa21 100644 --- a/digitalocean/uptime/resource_uptime_alert.go +++ b/digitalocean/uptime/resource_uptime_alert.go @@ -2,7 +2,9 @@ package uptime import ( "context" + "errors" "log" + "strings" "github.com/digitalocean/godo" "github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config" @@ -20,7 +22,7 @@ func ResourceDigitalOceanUptimeAlert() *schema.Resource { UpdateContext: resourceDigitalOceanUptimeAlertUpdate, DeleteContext: resourceDigitalOceanUptimeAlertDelete, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: resourceDigitalOceanUptimeAlertImport, }, Schema: map[string]*schema.Schema{ @@ -241,3 +243,16 @@ func flattenNotifications(alerts *godo.Notifications) []interface{} { }, } } + +func resourceDigitalOceanUptimeAlertImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + if strings.Contains(d.Id(), ",") { + s := strings.Split(d.Id(), ",") + + d.SetId(s[1]) + d.Set("check_id", s[0]) + } else { + return nil, errors.New("must use the IDs of the check and alert joined with a comma (e.g. `check_id,alert_id`)") + } + + return []*schema.ResourceData{d}, nil +} diff --git a/docs/resources/uptime_alert.md b/docs/resources/uptime_alert.md index 6746fa9e8..4bedb8c76 100644 --- a/docs/resources/uptime_alert.md +++ b/docs/resources/uptime_alert.md @@ -63,8 +63,9 @@ The following attributes are exported. ## Import -Uptime checks can be imported using the uptime alert's `id`, e.g. +Uptime alerts can be imported using the both IDs of the alert's parent check and its +own separated by a comma in the format: `check_id,alert_id`. For example: ```shell -terraform import digitalocean_uptime_alert.target 5a4981aa-9653-4bd1-bef5-d6bff52042e4 +terraform import digitalocean_uptime_alert.target 94a7d216-d821-11ee-a327-33d3239ffc4b,5a4981aa-9653-4bd1-bef5-d6bff52042e4 ``` From a3dc5aaeeb8283917a909ed877a1b04ba536554f Mon Sep 17 00:00:00 2001 From: Andrew Starr-Bochicchio Date: Mon, 4 Mar 2024 10:18:31 -0500 Subject: [PATCH 2/2] Fix typo --- docs/resources/uptime_alert.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/resources/uptime_alert.md b/docs/resources/uptime_alert.md index 4bedb8c76..1c90fdd38 100644 --- a/docs/resources/uptime_alert.md +++ b/docs/resources/uptime_alert.md @@ -63,8 +63,8 @@ The following attributes are exported. ## Import -Uptime alerts can be imported using the both IDs of the alert's parent check and its -own separated by a comma in the format: `check_id,alert_id`. For example: +Uptime alerts can be imported using both the ID of the alert's parent check and +its own separated by a comma in the format: `check_id,alert_id`. For example: ```shell terraform import digitalocean_uptime_alert.target 94a7d216-d821-11ee-a327-33d3239ffc4b,5a4981aa-9653-4bd1-bef5-d6bff52042e4