Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uptime alerts: fix importing existing alerts #1119

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion digitalocean/uptime/resource_uptime_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package uptime

import (
"context"
"errors"
"log"
"strings"

"github.com/digitalocean/godo"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
Expand All @@ -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{
Expand Down Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions docs/resources/uptime_alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
andrewsomething marked this conversation as resolved.
Show resolved Hide resolved
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
```
Loading