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 of disable_provenance for grafana_contact_point #1255

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions docs/resources/contact_point.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ resource "grafana_contact_point" "my_contact_point" {

- `alertmanager` (Block Set) A contact point that sends notifications to other Alertmanager instances. (see [below for nested schema](#nestedblock--alertmanager))
- `dingding` (Block Set) A contact point that sends notifications to DingDing. (see [below for nested schema](#nestedblock--dingding))
- `disable_provenance` (Boolean) Allow modifying the contact point from other sources than Terraform or the Grafana API. Defaults to `false`.
- `discord` (Block Set) A contact point that sends notifications as Discord messages (see [below for nested schema](#nestedblock--discord))
- `email` (Block Set) A contact point that sends notifications to an email address. (see [below for nested schema](#nestedblock--email))
- `googlechat` (Block Set) A contact point that sends notifications to Google Chat. (see [below for nested schema](#nestedblock--googlechat))
Expand Down
23 changes: 22 additions & 1 deletion internal/resources/grafana/resource_alerting_contact_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/grafana/terraform-provider-grafana/internal/common"
)

var provenanceDisabled = "disabled"

var notifiers = []notifier{
alertmanagerNotifier{},
dingDingNotifier{},
Expand Down Expand Up @@ -70,6 +72,13 @@ This resource requires Grafana 9.1.0 or later.
Required: true,
Description: "The name of the contact point.",
},
"disable_provenance": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true, // Can't modify provenance on contact points
Description: "Allow modifying the contact point from other sources than Terraform or the Grafana API.",
},
},
}

Expand Down Expand Up @@ -166,6 +175,9 @@ func updateContactPoint(ctx context.Context, data *schema.ResourceData, meta int
if uid = p.tfState["uid"].(string); uid != "" {
// If the contact point already has a UID, update it.
params := provisioning.NewPutContactpointParams().WithUID(uid).WithBody(p.gfState)
if data.Get("disable_provenance").(bool) {
params.SetXDisableProvenance(&provenanceDisabled)
}
if _, err := client.Provisioning.PutContactpoint(params); err != nil {
return diag.FromErr(err)
}
Expand All @@ -174,7 +186,11 @@ func updateContactPoint(ctx context.Context, data *schema.ResourceData, meta int
// Retry if the API returns 500 because it may be that the alertmanager is not ready in the org yet.
// The alertmanager is provisioned asynchronously when the org is created.
err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
resp, err := client.Provisioning.PostContactpoints(provisioning.NewPostContactpointsParams().WithBody(p.gfState))
params := provisioning.NewPostContactpointsParams().WithBody(p.gfState)
if data.Get("disable_provenance").(bool) {
params.SetXDisableProvenance(&provenanceDisabled)
}
resp, err := client.Provisioning.PostContactpoints(params)
if orgID > 1 && err != nil && err.(*runtime.APIError).IsCode(500) {
return retry.RetryableError(err)
} else if err != nil {
Expand Down Expand Up @@ -285,8 +301,12 @@ func unpackPointConfig(n notifier, data interface{}, name string) *models.Embedd

func packContactPoints(ps []*models.EmbeddedContactPoint, data *schema.ResourceData) error {
pointsPerNotifier := map[notifier][]interface{}{}
disableProvenance := true
for _, p := range ps {
data.Set("name", p.Name)
if p.Provenance != "" {
disableProvenance = false
}

for _, n := range notifiers {
if *p.Type == n.meta().typeStr {
Expand All @@ -299,6 +319,7 @@ func packContactPoints(ps []*models.EmbeddedContactPoint, data *schema.ResourceD
}
}
}
data.Set("disable_provenance", disableProvenance)

for n, pts := range pointsPerNotifier {
data.Set(n.meta().field, pts)
Expand Down
64 changes: 64 additions & 0 deletions internal/resources/grafana/resource_alerting_contact_point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,58 @@ func TestAccContactPoint_empty(t *testing.T) {
})
}

func TestAccContactPoint_disableProvenance(t *testing.T) {
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

var points models.ContactPoints
name := acctest.RandString(10)

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
CheckDestroy: alertingContactPointCheckExists.destroyed(&points, nil),
Steps: []resource.TestStep{
// Create
{
Config: testContactPointDisableProvenance(name, false),
Check: resource.ComposeTestCheckFunc(
checkAlertingContactPointExistsWithLength("grafana_contact_point.my_contact_point", &points, 1),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "name", name),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "disable_provenance", "false"),
),
},
// Import (tests that disable_provenance is fetched from API)
{
ResourceName: "grafana_contact_point.my_contact_point",
ImportState: true,
ImportStateId: name,
ImportStateVerify: true,
},
// Disable provenance
{
Config: testContactPointDisableProvenance(name, true),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "name", name),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "disable_provenance", "true"),
),
},
// Import (tests that disable_provenance is fetched from API)
{
ResourceName: "grafana_contact_point.my_contact_point",
ImportState: true,
ImportStateVerify: true,
},
// Re-enable provenance
{
Config: testContactPointDisableProvenance(name, false),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "name", name),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "disable_provenance", "false"),
),
},
},
})
}

func checkAlertingContactPointExistsWithLength(rn string, v *models.ContactPoints, expectedLength int) resource.TestCheckFunc {
return resource.ComposeTestCheckFunc(
alertingContactPointCheckExists.exists(rn, v),
Expand All @@ -526,6 +578,18 @@ func checkAlertingContactPointExistsWithLength(rn string, v *models.ContactPoint
)
}

func testContactPointDisableProvenance(name string, disableProvenance bool) string {
return fmt.Sprintf(`
resource "grafana_contact_point" "my_contact_point" {
name = "%s"
disable_provenance = %t
email {
addresses = [ "hello@example.com" ]
}
}
`, name, disableProvenance)
}

func testAccContactPointInOrg(name string) string {
return fmt.Sprintf(`
resource "grafana_organization" "test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func putMessageTemplate(ctx context.Context, data *schema.ResourceData, meta int
Template: content,
})
if v, ok := data.GetOk("disable_provenance"); ok && v.(bool) {
disabled := "disabled"
params.SetXDisableProvenance(&disabled)
params.SetXDisableProvenance(&provenanceDisabled)
}
if _, err := client.Provisioning.PutTemplate(params); err != nil {
if orgID > 1 && err.(*runtime.APIError).IsCode(500) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ func putNotificationPolicy(ctx context.Context, data *schema.ResourceData, meta

putParams := provisioning.NewPutPolicyTreeParams().WithBody(npt)
if data.Get("disable_provenance").(bool) {
disabled := "disabled"
putParams.SetXDisableProvenance(&disabled)
putParams.SetXDisableProvenance(&provenanceDisabled)
}

err = retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
Expand Down
3 changes: 1 addition & 2 deletions internal/resources/grafana/resource_alerting_rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ func putAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta inte
})

if data.Get("disable_provenance").(bool) {
disableProvenance := "disabled" // This can be any non-empty string.
putParams.SetXDisableProvenance(&disableProvenance)
putParams.SetXDisableProvenance(&provenanceDisabled)
}

resp, err := client.Provisioning.PutAlertRuleGroup(putParams)
Expand Down
Loading