-
Notifications
You must be signed in to change notification settings - Fork 63
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
Add support for the v1beta1 alerts api. #335
Open
hhellyer
wants to merge
2
commits into
master
Choose a base branch
from
PENG-5496/add_alerts_api_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
resource "ns1_alert" "email_on_zone_transfer_failure" { | ||
name = "Zone transfer failed" | ||
type = "zone" | ||
subtype = "transfer_failed" | ||
notification_lists = [ns1_notifylist.email_list.id] | ||
zone_names = [ns1_zone.alert_example_one.zone, ns1_zone.alert_example_two.zone] | ||
} | ||
|
||
# Nofitication list | ||
resource "ns1_notifylist" "email_list" { | ||
name = "email list" | ||
notifications { | ||
type = "email" | ||
config = { | ||
email = "[email protected]" | ||
} | ||
} | ||
} | ||
|
||
# Secondary zones | ||
resource "ns1_zone" "alert_example_one" { | ||
zone = "alert1.example" | ||
primary = "192.0.2.1" | ||
additional_primaries = ["192.0.2.2"] | ||
} | ||
|
||
resource "ns1_zone" "alert_example_two" { | ||
zone = "alert2.example" | ||
primary = "192.0.2.1" | ||
additional_primaries = ["192.0.2.2"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
resource "ns1_alert" "example" { | ||
#required | ||
name = "Example Alert" | ||
type = "zone" | ||
subtype = "transfer_failed" | ||
|
||
#optional | ||
notification_lists = [] | ||
zone_names = [] | ||
record_ids = [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package ns1 | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
ns1 "gopkg.in/ns1/ns1-go.v2/rest" | ||
"gopkg.in/ns1/ns1-go.v2/rest/model/alerting" | ||
) | ||
|
||
func alertResource() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
// Required | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
// ValidateFunc: validatePath, | ||
// DiffSuppressFunc: caseSensitivityDiffSuppress, | ||
}, | ||
"subtype": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
// ValidateFunc: validateURL, | ||
// DiffSuppressFunc: caseSensitivityDiffSuppress, | ||
}, | ||
// Read-only | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_at": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"updated_at": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"created_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"updated_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
// Optional | ||
"notification_lists": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"zone_names": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
ConflictsWith: []string{"record_ids"}, | ||
}, | ||
"record_ids": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
ConflictsWith: []string{"zone_names"}, | ||
}, | ||
}, | ||
Create: AlertConfigCreate, | ||
Read: AlertConfigRead, | ||
Update: AlertConfigUpdate, | ||
Delete: AlertConfigDelete, | ||
Importer: &schema.ResourceImporter{}, | ||
} | ||
} | ||
|
||
func alertToResourceData(d *schema.ResourceData, alert *alerting.Alert) error { | ||
d.SetId(*alert.ID) | ||
d.Set("name", alert.Name) | ||
d.Set("type", alert.Type) | ||
d.Set("subtype", alert.Subtype) | ||
d.Set("created_at", alert.CreatedAt) | ||
d.Set("updated_at", alert.UpdatedAt) | ||
d.Set("created_by", alert.CreatedBy) | ||
d.Set("updated_by", alert.UpdatedBy) | ||
d.Set("notification_lists", alert.NotifierListIds) | ||
d.Set("zone_names", alert.ZoneNames) | ||
d.Set("record_ids", alert.RecordIds) | ||
return nil | ||
} | ||
|
||
func strPtr(str string) *string { | ||
return &str | ||
} | ||
|
||
func int64Ptr(n int) *int64 { | ||
n64 := int64(n) | ||
return &n64 | ||
} | ||
|
||
func resourceDataToAlert(d *schema.ResourceData) (*alerting.Alert, error) { | ||
alert := alerting.Alert{ | ||
ID: strPtr(d.Id()), | ||
} | ||
if v, ok := d.GetOk("name"); ok { | ||
alert.Name = strPtr(v.(string)) | ||
} | ||
if v, ok := d.GetOk("type"); ok { | ||
alert.Type = strPtr(v.(string)) | ||
} | ||
if v, ok := d.GetOk("subtype"); ok { | ||
alert.Subtype = strPtr(v.(string)) | ||
} | ||
if v, ok := d.GetOk("created_at"); ok { | ||
alert.CreatedAt = int64Ptr(v.(int)) | ||
} | ||
if v, ok := d.GetOk("updated_at"); ok { | ||
alert.UpdatedAt = int64Ptr(v.(int)) | ||
} | ||
if v, ok := d.GetOk("created_by"); ok { | ||
alert.CreatedBy = strPtr(v.(string)) | ||
} | ||
if v, ok := d.GetOk("updated_by"); ok { | ||
alert.UpdatedBy = strPtr(v.(string)) | ||
} | ||
if v, ok := d.GetOk("notification_lists"); ok { | ||
listIds := v.(*schema.Set) | ||
alert.NotifierListIds = make([]string, 0, listIds.Len()) | ||
for _, id := range listIds.List() { | ||
alert.NotifierListIds = append(alert.NotifierListIds, id.(string)) | ||
} | ||
} else { | ||
alert.NotifierListIds = []string{} | ||
} | ||
if v, ok := d.GetOk("zone_names"); ok { | ||
zoneNames := v.(*schema.Set) | ||
alert.ZoneNames = make([]string, 0, zoneNames.Len()) | ||
for _, zone := range zoneNames.List() { | ||
alert.ZoneNames = append(alert.ZoneNames, zone.(string)) | ||
} | ||
} else { | ||
alert.ZoneNames = []string{} | ||
} | ||
if v, ok := d.GetOk("record_ids"); ok { | ||
recordIds := v.(*schema.Set) | ||
alert.RecordIds = make([]string, 0, recordIds.Len()) | ||
for _, id := range recordIds.List() { | ||
alert.RecordIds = append(alert.RecordIds, id.(string)) | ||
} | ||
} else { | ||
alert.RecordIds = []string{} | ||
} | ||
return &alert, nil | ||
} | ||
|
||
func AlertConfigCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ns1.Client) | ||
|
||
var alert *alerting.Alert = nil | ||
alert, err := resourceDataToAlert(d) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp, err := client.Alerts.Create(alert); err != nil { | ||
return ConvertToNs1Error(resp, err) | ||
} | ||
|
||
return alertToResourceData(d, alert) | ||
} | ||
|
||
func AlertConfigRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ns1.Client) | ||
|
||
alert, resp, err := client.Alerts.Get(d.Id()) | ||
if err != nil { | ||
if err == ns1.ErrAlertMissing { | ||
log.Printf("[DEBUG] NS1 alert (%s) not found", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return ConvertToNs1Error(resp, err) | ||
} | ||
|
||
return alertToResourceData(d, alert) | ||
} | ||
|
||
func AlertConfigUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ns1.Client) | ||
|
||
alert, err := resourceDataToAlert(d) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp, err := client.Alerts.Update(alert); err != nil { | ||
return ConvertToNs1Error(resp, err) | ||
} | ||
|
||
return alertToResourceData(d, alert) | ||
} | ||
|
||
func AlertConfigDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ns1.Client) | ||
|
||
resp, err := client.Alerts.Delete(d.Id()) | ||
d.SetId("") | ||
return ConvertToNs1Error(resp, err) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo 😆