Skip to content

Commit 66ef898

Browse files
✨ Add of disable_provenance for grafana_contact_point (#1255)
* ✨ disable_provenance for grafana_contact_point * Use a const for provenance disabled string --------- Co-authored-by: Julien Duchesne <[email protected]>
1 parent c6089d9 commit 66ef898

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

docs/resources/contact_point.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ resource "grafana_contact_point" "my_contact_point" {
4444

4545
- `alertmanager` (Block Set) A contact point that sends notifications to other Alertmanager instances. (see [below for nested schema](#nestedblock--alertmanager))
4646
- `dingding` (Block Set) A contact point that sends notifications to DingDing. (see [below for nested schema](#nestedblock--dingding))
47+
- `disable_provenance` (Boolean) Allow modifying the contact point from other sources than Terraform or the Grafana API. Defaults to `false`.
4748
- `discord` (Block Set) A contact point that sends notifications as Discord messages (see [below for nested schema](#nestedblock--discord))
4849
- `email` (Block Set) A contact point that sends notifications to an email address. (see [below for nested schema](#nestedblock--email))
4950
- `googlechat` (Block Set) A contact point that sends notifications to Google Chat. (see [below for nested schema](#nestedblock--googlechat))

internal/resources/grafana/resource_alerting_contact_point.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/grafana/terraform-provider-grafana/internal/common"
1919
)
2020

21+
var provenanceDisabled = "disabled"
22+
2123
var notifiers = []notifier{
2224
alertmanagerNotifier{},
2325
dingDingNotifier{},
@@ -70,6 +72,13 @@ This resource requires Grafana 9.1.0 or later.
7072
Required: true,
7173
Description: "The name of the contact point.",
7274
},
75+
"disable_provenance": {
76+
Type: schema.TypeBool,
77+
Optional: true,
78+
Default: false,
79+
ForceNew: true, // Can't modify provenance on contact points
80+
Description: "Allow modifying the contact point from other sources than Terraform or the Grafana API.",
81+
},
7382
},
7483
}
7584

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

286302
func packContactPoints(ps []*models.EmbeddedContactPoint, data *schema.ResourceData) error {
287303
pointsPerNotifier := map[notifier][]interface{}{}
304+
disableProvenance := true
288305
for _, p := range ps {
289306
data.Set("name", p.Name)
307+
if p.Provenance != "" {
308+
disableProvenance = false
309+
}
290310

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

303324
for n, pts := range pointsPerNotifier {
304325
data.Set(n.meta().field, pts)

internal/resources/grafana/resource_alerting_contact_point_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,58 @@ func TestAccContactPoint_empty(t *testing.T) {
510510
})
511511
}
512512

513+
func TestAccContactPoint_disableProvenance(t *testing.T) {
514+
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")
515+
516+
var points models.ContactPoints
517+
name := acctest.RandString(10)
518+
519+
resource.ParallelTest(t, resource.TestCase{
520+
ProviderFactories: testutils.ProviderFactories,
521+
CheckDestroy: alertingContactPointCheckExists.destroyed(&points, nil),
522+
Steps: []resource.TestStep{
523+
// Create
524+
{
525+
Config: testContactPointDisableProvenance(name, false),
526+
Check: resource.ComposeTestCheckFunc(
527+
checkAlertingContactPointExistsWithLength("grafana_contact_point.my_contact_point", &points, 1),
528+
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "name", name),
529+
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "disable_provenance", "false"),
530+
),
531+
},
532+
// Import (tests that disable_provenance is fetched from API)
533+
{
534+
ResourceName: "grafana_contact_point.my_contact_point",
535+
ImportState: true,
536+
ImportStateId: name,
537+
ImportStateVerify: true,
538+
},
539+
// Disable provenance
540+
{
541+
Config: testContactPointDisableProvenance(name, true),
542+
Check: resource.ComposeTestCheckFunc(
543+
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "name", name),
544+
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "disable_provenance", "true"),
545+
),
546+
},
547+
// Import (tests that disable_provenance is fetched from API)
548+
{
549+
ResourceName: "grafana_contact_point.my_contact_point",
550+
ImportState: true,
551+
ImportStateVerify: true,
552+
},
553+
// Re-enable provenance
554+
{
555+
Config: testContactPointDisableProvenance(name, false),
556+
Check: resource.ComposeTestCheckFunc(
557+
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "name", name),
558+
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "disable_provenance", "false"),
559+
),
560+
},
561+
},
562+
})
563+
}
564+
513565
func checkAlertingContactPointExistsWithLength(rn string, v *models.ContactPoints, expectedLength int) resource.TestCheckFunc {
514566
return resource.ComposeTestCheckFunc(
515567
alertingContactPointCheckExists.exists(rn, v),
@@ -526,6 +578,18 @@ func checkAlertingContactPointExistsWithLength(rn string, v *models.ContactPoint
526578
)
527579
}
528580

581+
func testContactPointDisableProvenance(name string, disableProvenance bool) string {
582+
return fmt.Sprintf(`
583+
resource "grafana_contact_point" "my_contact_point" {
584+
name = "%s"
585+
disable_provenance = %t
586+
email {
587+
addresses = [ "[email protected]" ]
588+
}
589+
}
590+
`, name, disableProvenance)
591+
}
592+
529593
func testAccContactPointInOrg(name string) string {
530594
return fmt.Sprintf(`
531595
resource "grafana_organization" "test" {

internal/resources/grafana/resource_alerting_message_template.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ func putMessageTemplate(ctx context.Context, data *schema.ResourceData, meta int
9292
Template: content,
9393
})
9494
if v, ok := data.GetOk("disable_provenance"); ok && v.(bool) {
95-
disabled := "disabled"
96-
params.SetXDisableProvenance(&disabled)
95+
params.SetXDisableProvenance(&provenanceDisabled)
9796
}
9897
if _, err := client.Provisioning.PutTemplate(params); err != nil {
9998
if orgID > 1 && err.(*runtime.APIError).IsCode(500) {

internal/resources/grafana/resource_alerting_notification_policy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ func putNotificationPolicy(ctx context.Context, data *schema.ResourceData, meta
207207

208208
putParams := provisioning.NewPutPolicyTreeParams().WithBody(npt)
209209
if data.Get("disable_provenance").(bool) {
210-
disabled := "disabled"
211-
putParams.SetXDisableProvenance(&disabled)
210+
putParams.SetXDisableProvenance(&provenanceDisabled)
212211
}
213212

214213
err = retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {

internal/resources/grafana/resource_alerting_rule_group.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ func putAlertRuleGroup(ctx context.Context, data *schema.ResourceData, meta inte
259259
})
260260

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

266265
resp, err := client.Provisioning.PutAlertRuleGroup(putParams)

0 commit comments

Comments
 (0)