From 0f7d8a610a580499888f26eba87e51a0ed4435cb Mon Sep 17 00:00:00 2001 From: Eric Leijonmarck Date: Tue, 17 Sep 2024 10:07:30 +0100 Subject: [PATCH] revert one file --- .../resource_alerting_message_template.go | 67 +++++-------------- 1 file changed, 15 insertions(+), 52 deletions(-) diff --git a/internal/resources/grafana/resource_alerting_message_template.go b/internal/resources/grafana/resource_alerting_message_template.go index a3bb335ff..bc7d4c881 100644 --- a/internal/resources/grafana/resource_alerting_message_template.go +++ b/internal/resources/grafana/resource_alerting_message_template.go @@ -7,29 +7,28 @@ import ( "time" "github.com/go-openapi/runtime" - goapi "github.com/grafana/grafana-openapi-client-go/client" "github.com/grafana/grafana-openapi-client-go/client/provisioning" "github.com/grafana/grafana-openapi-client-go/models" - "github.com/grafana/terraform-provider-grafana/v3/internal/common" + "github.com/grafana/terraform-provider-grafana/internal/common" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func resourceMessageTemplate() *common.Resource { - schema := &schema.Resource{ +func ResourceMessageTemplate() *schema.Resource { + return &schema.Resource{ Description: ` Manages Grafana Alerting message templates. -* [Official documentation](https://grafana.com/docs/grafana/latest/alerting/set-up/provision-alerting-resources/terraform-provisioning/) -* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/alerting_provisioning/#templates) +* [Official documentation](https://grafana.com/docs/grafana/latest/alerting/manage-notifications/template-notifications/create-notification-templates/) +* [HTTP API](https://grafana.com/docs/grafana/next/developers/http_api/alerting_provisioning/#templates) This resource requires Grafana 9.1.0 or later. `, - CreateContext: common.WithAlertingMutex[schema.CreateContextFunc](putMessageTemplate), + CreateContext: putMessageTemplate, ReadContext: readMessageTemplate, - UpdateContext: common.WithAlertingMutex[schema.UpdateContextFunc](putMessageTemplate), - DeleteContext: common.WithAlertingMutex[schema.DeleteContextFunc](deleteMessageTemplate), + UpdateContext: putMessageTemplate, + DeleteContext: deleteMessageTemplate, Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, @@ -51,46 +50,8 @@ This resource requires Grafana 9.1.0 or later. return strings.TrimSpace(v.(string)) }, }, - "disable_provenance": { - Type: schema.TypeBool, - Optional: true, - Default: false, - ForceNew: true, // TODO: The API doesn't return provenance, so we have to force new for now. - Description: "Allow modifying the message template from other sources than Terraform or the Grafana API.", - }, }, } - - return common.NewLegacySDKResource( - common.CategoryAlerting, - "grafana_message_template", - orgResourceIDString("name"), - schema, - ).WithLister(listerFunctionOrgResource(listMessageTemplate)) -} - -func listMessageTemplate(ctx context.Context, client *goapi.GrafanaHTTPAPI, orgID int64) ([]string, error) { - var ids []string - // 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. - if err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError { - resp, err := client.Provisioning.GetTemplates() - if err != nil { - if orgID > 1 && (err.(*runtime.APIError).IsCode(500) || err.(*runtime.APIError).IsCode(403)) { - return retry.RetryableError(err) - } - return retry.NonRetryableError(err) - } - - for _, template := range resp.Payload { - ids = append(ids, MakeOrgResourceID(orgID, template.Name)) - } - return nil - }); err != nil { - return nil, err - } - - return ids, nil } func readMessageTemplate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { @@ -110,6 +71,9 @@ func readMessageTemplate(ctx context.Context, data *schema.ResourceData, meta in } func putMessageTemplate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + lock := &meta.(*common.Client).AlertingMutex + lock.Lock() + defer lock.Unlock() client, orgID := OAPIClientFromNewOrgResource(meta, data) name := data.Get("name").(string) @@ -123,9 +87,6 @@ func putMessageTemplate(ctx context.Context, data *schema.ResourceData, meta int WithBody(&models.NotificationTemplateContent{ Template: content, }) - if v, ok := data.GetOk("disable_provenance"); ok && v.(bool) { - params.SetXDisableProvenance(&provenanceDisabled) - } if _, err := client.Provisioning.PutTemplate(params); err != nil { if orgID > 1 && err.(*runtime.APIError).IsCode(500) { return retry.RetryableError(err) @@ -143,10 +104,12 @@ func putMessageTemplate(ctx context.Context, data *schema.ResourceData, meta int } func deleteMessageTemplate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { + lock := &meta.(*common.Client).AlertingMutex + lock.Lock() + defer lock.Unlock() client, _, name := OAPIClientFromExistingOrgResource(meta, data.Id()) - params := provisioning.NewDeleteTemplateParams().WithName(name) - _, err := client.Provisioning.DeleteTemplate(params) + _, err := client.Provisioning.DeleteTemplate(name) diag, _ := common.CheckReadError("message template", data, err) return diag }