Skip to content

Commit

Permalink
✨ disable_provenance for grafana_contact_point
Browse files Browse the repository at this point in the history
  • Loading branch information
afreyermuth98 authored and julienduchesne committed Jan 25, 2024
1 parent c2152fb commit 2e7ec81
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
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 @@ -70,6 +70,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 +173,10 @@ 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) {
disabled := "disabled"
params.SetXDisableProvenance(&disabled)
}
if _, err := client.Provisioning.PutContactpoint(params); err != nil {
return diag.FromErr(err)
}
Expand All @@ -174,7 +185,12 @@ 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) {
disabled := "disabled"
params.SetXDisableProvenance(&disabled)
}
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

0 comments on commit 2e7ec81

Please sign in to comment.