-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add terraform-provider support for Notification policies
- Loading branch information
Revathy Ramasundaram
committed
Jul 29, 2021
1 parent
9087090
commit 210623c
Showing
3 changed files
with
364 additions
and
0 deletions.
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
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,263 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourceCloudflareNotificationPolicy() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCloudflareNotificationPolicyCreate, | ||
Read: resourceCloudflareNotificationPolicyRead, | ||
Update: resourceCloudflareNotificationPolicyUpdate, | ||
Delete: resourceCloudflareNotificationPolicyDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: resourceNotificationPolicyImport, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"account_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"enabled": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
}, | ||
"alert_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"filters": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeList, | ||
Elem: schema.TypeString, | ||
}, | ||
}, | ||
"created": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"modified": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"conditions": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
}, | ||
"email_integration": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: mechanismData, | ||
}, | ||
"webhooks_integration": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: mechanismData, | ||
}, | ||
"pagerduty_integration": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: mechanismData, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
var mechanismData = &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
|
||
func resourceCloudflareNotificationPolicyCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
accountID := d.Get("account_id").(string) | ||
|
||
notificationPolicy := buildNotificationPolicy(d) | ||
|
||
policy, err := client.CreateNotificationPolicy(context.Background(), accountID, notificationPolicy) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error creating policy %s: %s", notificationPolicy.Name, err) | ||
} | ||
d.SetId(policy.Result.ID) | ||
|
||
return resourceCloudflareNotificationPolicyRead(d, meta) | ||
} | ||
func resourceCloudflareNotificationPolicyRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
policyID := d.Id() | ||
accountID := d.Get("account_id").(string) | ||
|
||
policy, err := client.GetNotificationPolicy(context.Background(), accountID, policyID) | ||
|
||
name := d.Get("name").(string) | ||
if err != nil { | ||
return fmt.Errorf("error retrieving notification policy %s: %s", name, err) | ||
} | ||
|
||
d.Set("id", policy.Result.ID) | ||
d.Set("name", policy.Result.Name) | ||
d.Set("enabled", policy.Result.Enabled) | ||
d.Set("alert_type", policy.Result.AlertType) | ||
d.Set("description", policy.Result.Description) | ||
d.Set("filters", policy.Result.Filters) | ||
d.Set("conditions", policy.Result.Conditions) | ||
d.Set("created", policy.Result.Created.Format(time.RFC3339)) | ||
d.Set("modified", policy.Result.Modified.Format(time.RFC3339)) | ||
if err := d.Set("email_integration", setNotificationMechanisms(policy.Result.Mechanisms["email"])); err != nil { | ||
return fmt.Errorf("failed to set email integration: %s", err) | ||
} | ||
if err := d.Set("pagerduty_integration", setNotificationMechanisms(policy.Result.Mechanisms["pagerduty"])); err != nil { | ||
return fmt.Errorf("failed to set pagerduty integration: %s", err) | ||
} | ||
if err := d.Set("webhooks_integration", setNotificationMechanisms(policy.Result.Mechanisms["webhooks"])); err != nil { | ||
return fmt.Errorf("failed to set webhooks integration: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
func resourceCloudflareNotificationPolicyUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
policyID := d.Id() | ||
accountID := d.Get("account_id").(string) | ||
|
||
notificationPolicy := buildNotificationPolicy(d) | ||
notificationPolicy.ID = policyID | ||
|
||
_, err := client.UpdateNotificationPolicy(context.Background(), accountID, ¬ificationPolicy) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error updating notification policy %s: %s", policyID, err) | ||
} | ||
|
||
return resourceCloudflareNotificationPolicyRead(d, meta) | ||
} | ||
func resourceCloudflareNotificationPolicyDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
policyID := d.Id() | ||
accountID := d.Get("account_id").(string) | ||
|
||
_, err := client.DeleteNotificationPolicy(context.Background(), accountID, policyID) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error deleting notification policy %s: %s", policyID, err) | ||
} | ||
return nil | ||
} | ||
func resourceNotificationPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
attributes := strings.SplitN(d.Id(), "/", 2) | ||
|
||
if len(attributes) != 2 { | ||
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"accountID/policyID\"", d.Id()) | ||
} | ||
|
||
accountID, policyID := attributes[0], attributes[1] | ||
d.SetId(policyID) | ||
d.Set("account_id", accountID) | ||
|
||
resourceCloudflareNotificationPolicyRead(d, meta) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
|
||
} | ||
func buildNotificationPolicy(d *schema.ResourceData) cloudflare.NotificationPolicy { | ||
notificationPolicy := cloudflare.NotificationPolicy{} | ||
notificationPolicy.Mechanisms = make(map[string]cloudflare.NotificationMechanismIntegrations) | ||
notificationPolicy.Conditions = make(map[string]interface{}) | ||
notificationPolicy.Filters = make(map[string][]string) | ||
|
||
if name, ok := d.GetOk("name"); ok { | ||
notificationPolicy.Name = name.(string) | ||
} | ||
if description, ok := d.GetOk("description"); ok { | ||
notificationPolicy.Description = description.(string) | ||
} | ||
if enabled, ok := d.GetOk("enabled"); ok { | ||
notificationPolicy.Enabled = enabled.(bool) | ||
} | ||
if alertType, ok := d.GetOk("alert_type"); ok { | ||
notificationPolicy.AlertType = alertType.(string) | ||
} | ||
if emails, ok := d.GetOk("email_integration"); ok { | ||
notificationPolicy.Mechanisms["email"] = getNotificationMechanisms(emails.(*schema.Set)) | ||
} | ||
if webhooks, ok := d.GetOk("webhooks_integration"); ok { | ||
notificationPolicy.Mechanisms["webhooks"] = getNotificationMechanisms(webhooks.(*schema.Set)) | ||
} | ||
if pagerduty, ok := d.GetOk("pagerduty_integration"); ok { | ||
notificationPolicy.Mechanisms["pagerduty"] = getNotificationMechanisms(pagerduty.(*schema.Set)) | ||
} | ||
if filters, ok := d.GetOk("filters"); ok { | ||
notificationPolicy.Filters = filters.(map[string][]string) | ||
} | ||
if conditions, ok := d.GetOk("conditions"); ok { | ||
notificationPolicy.Conditions = conditions.(map[string]interface{}) | ||
} | ||
return notificationPolicy | ||
} | ||
|
||
func setNotificationPolicy(d *schema.ResourceData, policy cloudflare.NotificationPolicyResponse) error { | ||
var e error | ||
d.Set("id", policy.Result.ID) | ||
d.Set("name", policy.Result.Name) | ||
d.Set("enabled", policy.Result.Enabled) | ||
d.Set("alert_type", policy.Result.AlertType) | ||
d.Set("description", policy.Result.Description) | ||
d.Set("filters", policy.Result.Filters) | ||
d.Set("conditions", policy.Result.Conditions) | ||
d.Set("created", policy.Result.Created.Format(time.RFC3339)) | ||
d.Set("modified", policy.Result.Modified.Format(time.RFC3339)) | ||
e = d.Set("email_integration", setNotificationMechanisms(policy.Result.Mechanisms["email"])) | ||
e = d.Set("pagerduty_integration", setNotificationMechanisms(policy.Result.Mechanisms["pagerduty"])) | ||
e = d.Set("webhooks_integration", setNotificationMechanisms(policy.Result.Mechanisms["webhooks"])) | ||
return e | ||
} | ||
|
||
func getNotificationMechanisms(s *schema.Set) []cloudflare.NotificationMechanismData { | ||
var notificationMechanisms []cloudflare.NotificationMechanismData | ||
for _, m := range s.List() { | ||
mechanism := m.(map[string]interface{}) | ||
data := cloudflare.NotificationMechanismData{ | ||
ID: mechanism["id"].(string), | ||
Name: mechanism["name"].(string), | ||
} | ||
notificationMechanisms = append(notificationMechanisms, data) | ||
} | ||
return notificationMechanisms | ||
} | ||
|
||
func setNotificationMechanisms(md []cloudflare.NotificationMechanismData) *schema.Set { | ||
mechanisms := make([]interface{}, 0) | ||
data := make(map[string]interface{}) | ||
for _, m := range md { | ||
data["name"] = m.Name | ||
data["id"] = m.ID | ||
mechanisms = append(mechanisms, data) | ||
} | ||
return schema.NewSet(schema.HashResource(mechanismData), mechanisms) | ||
} |
100 changes: 100 additions & 0 deletions
100
cloudflare/resource_cloudflare_notification_policy_test.go
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,100 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccCloudflareCreateNotificationPolicy(t *testing.T) { | ||
rnd := generateRandomResourceName() | ||
resourceName := "cloudflare_notification_policy." + rnd | ||
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheckAccount(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testCheckCloudflareNotificationPolicy(rnd, accountID), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "name", "test SSL policy from terraform provider"), | ||
resource.TestCheckResourceAttr(resourceName, "description", "test description"), | ||
resource.TestCheckResourceAttr(resourceName, "enabled", "true"), | ||
resource.TestCheckResourceAttr(resourceName, "alert_type", "universal_ssl_event_type"), | ||
resource.TestCheckResourceAttr(resourceName, "account_id", accountID), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccCloudflareNotificationPolicyUpdate(t *testing.T) { | ||
rnd := generateRandomResourceName() | ||
resourceName := "cloudflare_notification_policy." + rnd | ||
updatedPolicyName := "*updated* test SSL policy from terraform provider" | ||
updatedPolicyDesc := "*updated* description" | ||
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheckAccount(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testCheckCloudflareNotificationPolicy(rnd, accountID), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "name", "test SSL policy from terraform provider"), | ||
resource.TestCheckResourceAttr(resourceName, "description", "test description"), | ||
resource.TestCheckResourceAttr(resourceName, "enabled", "true"), | ||
resource.TestCheckResourceAttr(resourceName, "alert_type", "universal_ssl_event_type"), | ||
resource.TestCheckResourceAttr(resourceName, "account_id", accountID), | ||
), | ||
}, | ||
{ | ||
Config: testCheckCloudflareNotificationPolicyUpdated(rnd, updatedPolicyName, updatedPolicyDesc, accountID), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "name", updatedPolicyName), | ||
resource.TestCheckResourceAttr(resourceName, "description", updatedPolicyDesc), | ||
resource.TestCheckResourceAttr(resourceName, "enabled", "true"), | ||
resource.TestCheckResourceAttr(resourceName, "alert_type", "universal_ssl_event_type"), | ||
resource.TestCheckResourceAttr(resourceName, "account_id", accountID), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckCloudflareNotificationPolicy(name, accountID string) string { | ||
return fmt.Sprintf(` | ||
resource "cloudflare_notification_policy" "%[1]s" { | ||
name = "test SSL policy from terraform provider" | ||
account_id = "%[2]s" | ||
description = "test description" | ||
enabled = true | ||
alert_type = "universal_ssl_event_type" | ||
email_integration { | ||
name = "test@cloudflare.com" | ||
id = "" | ||
} | ||
}`, name, accountID) | ||
} | ||
|
||
func testCheckCloudflareNotificationPolicyUpdated(resName, policyName, policyDesc, accountID string) string { | ||
return fmt.Sprintf(` | ||
resource "cloudflare_notification_policy" "%[1]s" { | ||
name = "%[2]s" | ||
account_id = "%[4]s" | ||
description = "%[3]s" | ||
enabled = true | ||
alert_type = "universal_ssl_event_type" | ||
email_integration { | ||
name = "test@cloudflare.com" | ||
id = "" | ||
} | ||
}`, resName, policyName, policyDesc, accountID) | ||
} |