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

Add terraform-provider support for Notification policies #1138

Merged
merged 7 commits into from Aug 4, 2021

Conversation

revathyrams
Copy link
Contributor

Adding support to perform CRUD operations on Notification policies using the Cloudflare terraform provider.

@jacobbednarz
Copy link
Member

i tried fixing a couple of these for you but it looks like you haven't allowed maintainers the ability to push to your branch so here is a patch instead :)

two fixes:

  • acceptance test names need to start with TestAcc
  • d.Set calls are fine to do inline in the Read with error handling for the complex types
diff --git cloudflare/resource_cloudflare_notification_policy.go cloudflare/resource_cloudflare_notification_policy.go
index f7ce74c..1a85ea3 100644
--- cloudflare/resource_cloudflare_notification_policy.go
+++ cloudflare/resource_cloudflare_notification_policy.go
@@ -107,10 +107,12 @@ func resourceCloudflareNotificationPolicyCreate(d *schema.ResourceData, meta int
 	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()
@@ -123,10 +125,28 @@ func resourceCloudflareNotificationPolicyRead(d *schema.ResourceData, meta inter
 		return fmt.Errorf("error retrieving notification policy %s: %s", name, err)
 	}
 
-	err = setNotificationPolicy(d, policy)
-	if err != nil {
-		return fmt.Errorf("error setting notification policy object for %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
 }
 
@@ -139,13 +159,13 @@ func resourceCloudflareNotificationPolicyUpdate(d *schema.ResourceData, meta int
 	notificationPolicy.ID = policyID
 
 	_, err := client.UpdateNotificationPolicy(context.Background(), accountID, &notificationPolicy)
-
 	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()
@@ -158,6 +178,7 @@ func resourceCloudflareNotificationPolicyDelete(d *schema.ResourceData, meta int
 	}
 	return nil
 }
+
 func resourceNotificationPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
 	attributes := strings.SplitN(d.Id(), "/", 2)
 
@@ -183,50 +204,41 @@ func buildNotificationPolicy(d *schema.ResourceData) cloudflare.NotificationPoli
 	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
-	e = d.Set("id", policy.Result.ID)
-	e = d.Set("name", policy.Result.Name)
-	e = d.Set("enabled", policy.Result.Enabled)
-	e = d.Set("alert_type", policy.Result.AlertType)
-	e = d.Set("description", policy.Result.Description)
-	e = d.Set("filters", policy.Result.Filters)
-	e = d.Set("conditions", policy.Result.Conditions)
-	e = d.Set("created", policy.Result.Created.Format(time.RFC3339))
-	e = 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() {
@@ -248,5 +260,6 @@ func setNotificationMechanisms(md []cloudflare.NotificationMechanismData) *schem
 		data["name"] = m.ID
 		mechanisms = append(mechanisms, data)
 	}
+
 	return schema.NewSet(schema.HashResource(mechanismData), mechanisms)
 }
diff --git cloudflare/resource_cloudflare_notification_policy_test.go cloudflare/resource_cloudflare_notification_policy_test.go
index a6931e3..8a4e233 100644
--- cloudflare/resource_cloudflare_notification_policy_test.go
+++ cloudflare/resource_cloudflare_notification_policy_test.go
@@ -7,7 +7,7 @@ import (
 	"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
 )
 
-func TestCloudflareCreateNotificationPolicy(t *testing.T) {
+func TestAccCloudflareCreateNotificationPolicy(t *testing.T) {
 	rnd := generateRandomResourceName()
 	resourceName := "cloudflare_notification_policy." + rnd
 
@@ -31,7 +31,7 @@ func TestCloudflareCreateNotificationPolicy(t *testing.T) {
 	})
 }
 
-func TestCloudflareCreateNotificationPolicyUpdate(t *testing.T) {
+func TestAccCloudflareCreateNotificationPolicyUpdate(t *testing.T) {
 	rnd := generateRandomResourceName()
 	resourceName := "cloudflare_notification_policy." + rnd
 	updatedPolicyName := "*updated* test SSL policy from terraform provider"

the second test covers creation + update so we don't need the first one.
@jacobbednarz
Copy link
Member

Code and acceptance tests look great @revathyrams! Only thing left is to add the website docs (example in #1113) and we can ship this one for the next release!

@revathyrams
Copy link
Contributor Author

Code and acceptance tests look great @revathyrams! Only thing left is to add the website docs (example in #1113) and we can ship this one for the next release!

Appreciate your review and the enhancements. I'll work on adding the website docs.

@jacobbednarz
Copy link
Member

@jacobbednarz jacobbednarz merged commit be4e043 into cloudflare:master Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants