diff --git a/internal/services/costmanagement/anomaly_alert_resource.go b/internal/services/costmanagement/anomaly_alert_resource.go index 3fb65b2e76b8..a40addf9998d 100644 --- a/internal/services/costmanagement/anomaly_alert_resource.go +++ b/internal/services/costmanagement/anomaly_alert_resource.go @@ -11,8 +11,8 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-06-01-preview/scheduledactions" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/views" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/scheduledactions" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/views" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/services/costmanagement/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" @@ -48,6 +48,13 @@ func (AnomalyAlertResource) Arguments() map[string]*pluginsdk.Schema { ValidateFunc: commonids.ValidateSubscriptionID, }, + "notification_email": { + Type: pluginsdk.TypeString, + Optional: true, + Computed: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + "email_subject": { Type: pluginsdk.TypeString, Required: true, @@ -117,6 +124,10 @@ func (r AnomalyAlertResource) Create() sdk.ResourceFunc { schedule.SetEndDateAsTime(time.Now().AddDate(1, 0, 0)) schedule.SetStartDateAsTime(time.Now()) + notificationEmail := (*emailAddresses)[0] + if v, ok := metadata.ResourceData.GetOk("notification_email"); ok { + notificationEmail = v.(string) + } param := scheduledactions.ScheduledAction{ Kind: pointer.To(scheduledactions.ScheduledActionKindInsightAlert), Properties: &scheduledactions.ScheduledActionProperties{ @@ -126,6 +137,7 @@ func (r AnomalyAlertResource) Create() sdk.ResourceFunc { FileDestination: &scheduledactions.FileDestination{ FileFormats: &[]scheduledactions.FileFormat{}, }, + NotificationEmail: ¬ificationEmail, Notification: scheduledactions.NotificationProperties{ Subject: metadata.ResourceData.Get("email_subject").(string), Message: utils.String(metadata.ResourceData.Get("message").(string)), @@ -134,7 +146,7 @@ func (r AnomalyAlertResource) Create() sdk.ResourceFunc { Schedule: schedule, }, } - if _, err := client.CreateOrUpdateByScope(ctx, id, param); err != nil { + if _, err := client.CreateOrUpdateByScope(ctx, id, param, scheduledactions.DefaultCreateOrUpdateByScopeOperationOptions()); err != nil { return fmt.Errorf("creating %s: %+v", id, err) } @@ -183,13 +195,18 @@ func (r AnomalyAlertResource) Update() sdk.ResourceFunc { schedule.SetEndDateAsTime(time.Now().AddDate(1, 0, 0)) schedule.SetStartDateAsTime(time.Now()) + notificationEmail := (*emailAddresses)[0] + if v, ok := metadata.ResourceData.GetOk("notification_email"); ok { + notificationEmail = v.(string) + } param := scheduledactions.ScheduledAction{ Kind: pointer.To(scheduledactions.ScheduledActionKindInsightAlert), ETag: resp.Model.ETag, Properties: &scheduledactions.ScheduledActionProperties{ - DisplayName: metadata.ResourceData.Get("display_name").(string), - Status: scheduledactions.ScheduledActionStatusEnabled, - ViewId: viewId.ID(), + DisplayName: metadata.ResourceData.Get("display_name").(string), + Status: scheduledactions.ScheduledActionStatusEnabled, + ViewId: viewId.ID(), + NotificationEmail: ¬ificationEmail, Notification: scheduledactions.NotificationProperties{ Subject: metadata.ResourceData.Get("email_subject").(string), Message: utils.String(metadata.ResourceData.Get("message").(string)), @@ -198,7 +215,7 @@ func (r AnomalyAlertResource) Update() sdk.ResourceFunc { Schedule: schedule, }, } - if _, err := client.CreateOrUpdateByScope(ctx, *id, param); err != nil { + if _, err := client.CreateOrUpdateByScope(ctx, *id, param, scheduledactions.DefaultCreateOrUpdateByScopeOperationOptions()); err != nil { return fmt.Errorf("creating %s: %+v", id, err) } @@ -234,6 +251,7 @@ func (AnomalyAlertResource) Read() sdk.ResourceFunc { metadata.ResourceData.Set("display_name", props.DisplayName) metadata.ResourceData.Set("subscription_id", fmt.Sprint("/", *props.Scope)) metadata.ResourceData.Set("email_subject", props.Notification.Subject) + metadata.ResourceData.Set("notification_email", props.NotificationEmail) metadata.ResourceData.Set("email_addresses", props.Notification.To) metadata.ResourceData.Set("message", props.Notification.Message) } diff --git a/internal/services/costmanagement/anomaly_alert_resource_test.go b/internal/services/costmanagement/anomaly_alert_resource_test.go index a33a92db6153..fdc6a2f09ddd 100644 --- a/internal/services/costmanagement/anomaly_alert_resource_test.go +++ b/internal/services/costmanagement/anomaly_alert_resource_test.go @@ -8,7 +8,7 @@ import ( "fmt" "testing" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-06-01-preview/scheduledactions" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/scheduledactions" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" @@ -50,6 +50,17 @@ func TestAccResourceAnomalyAlert_requiresImport(t *testing.T) { }) } +func TestAccResourceAnomalyAlert_emailAddressSender(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cost_anomaly_alert", "test") + testResource := AnomalyAlertResource{} + data.ResourceTest(t, testResource, []acceptance.TestStep{ + data.ApplyStep(testResource.notificationEmailConfig, testResource), + data.ImportStep(), + data.ApplyStep(testResource.updateConfig, testResource), + data.ImportStep(), + }) +} + func (AnomalyAlertResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := scheduledactions.ParseScopedScheduledActionID(state.ID) if err != nil { @@ -129,3 +140,20 @@ resource "azurerm_cost_anomaly_alert" "test" { } `, data.RandomInteger, data.RandomInteger) } + +func (AnomalyAlertResource) notificationEmailConfig(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_cost_anomaly_alert" "test" { + name = "-acctest-%d" + display_name = "acctest %d" + email_subject = "Hi" + email_addresses = ["test@test.com", "test@hashicorp.developer"] + notification_email = "othertest@hashicorp.developer" + message = "Custom sender email configured" +} +`, data.RandomInteger, data.RandomInteger) +} diff --git a/internal/services/costmanagement/client/client.go b/internal/services/costmanagement/client/client.go index bc38632c4a24..ef06e10770f7 100644 --- a/internal/services/costmanagement/client/client.go +++ b/internal/services/costmanagement/client/client.go @@ -6,18 +6,16 @@ package client import ( "fmt" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-06-01-preview/scheduledactions" - scheduledactions_v2022_10_01 "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/scheduledactions" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/views" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/exports" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/scheduledactions" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/views" "github.com/hashicorp/terraform-provider-azurerm/internal/common" ) type Client struct { - ExportClient *exports.ExportsClient - ScheduledActionsClient *scheduledactions.ScheduledActionsClient - ScheduledActionsClient_v2022_10_01 *scheduledactions_v2022_10_01.ScheduledActionsClient - ViewsClient *views.ViewsClient + ExportClient *exports.ExportsClient + ScheduledActionsClient *scheduledactions.ScheduledActionsClient + ViewsClient *views.ViewsClient } func NewClient(o *common.ClientOptions) (*Client, error) { @@ -33,12 +31,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) { } o.Configure(scheduledActionsClient.Client, o.Authorizers.ResourceManager) - scheduledActionsClient_v2022_10_01, err := scheduledactions_v2022_10_01.NewScheduledActionsClientWithBaseURI(o.Environment.ResourceManager) - if err != nil { - return nil, fmt.Errorf("building ScheduledActions client: %+v", err) - } - o.Configure(scheduledActionsClient_v2022_10_01.Client, o.Authorizers.ResourceManager) - viewsClient, err := views.NewViewsClientWithBaseURI(o.Environment.ResourceManager) if err != nil { return nil, fmt.Errorf("building Views client: %+v", err) @@ -46,9 +38,8 @@ func NewClient(o *common.ClientOptions) (*Client, error) { o.Configure(viewsClient.Client, o.Authorizers.ResourceManager) return &Client{ - ExportClient: exportClient, - ScheduledActionsClient: scheduledActionsClient, - ScheduledActionsClient_v2022_10_01: scheduledActionsClient_v2022_10_01, - ViewsClient: viewsClient, + ExportClient: exportClient, + ScheduledActionsClient: scheduledActionsClient, + ViewsClient: viewsClient, }, nil } diff --git a/internal/services/costmanagement/export_billing_account_resource_test.go b/internal/services/costmanagement/export_billing_account_resource_test.go index c52efd369772..fb011bc0b6c3 100644 --- a/internal/services/costmanagement/export_billing_account_resource_test.go +++ b/internal/services/costmanagement/export_billing_account_resource_test.go @@ -10,7 +10,7 @@ import ( "testing" "time" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/exports" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" diff --git a/internal/services/costmanagement/export_resource_base.go b/internal/services/costmanagement/export_resource_base.go index ce3fc3e4b496..a9563b05eca9 100644 --- a/internal/services/costmanagement/export_resource_base.go +++ b/internal/services/costmanagement/export_resource_base.go @@ -11,7 +11,7 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/exports" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" diff --git a/internal/services/costmanagement/export_resource_group_resource_test.go b/internal/services/costmanagement/export_resource_group_resource_test.go index e6ddd4a90e4f..a2b83de76e57 100644 --- a/internal/services/costmanagement/export_resource_group_resource_test.go +++ b/internal/services/costmanagement/export_resource_group_resource_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/exports" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" diff --git a/internal/services/costmanagement/export_subscription_resource_test.go b/internal/services/costmanagement/export_subscription_resource_test.go index 8d467100f38a..f992e069ff32 100644 --- a/internal/services/costmanagement/export_subscription_resource_test.go +++ b/internal/services/costmanagement/export_subscription_resource_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/exports" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" diff --git a/internal/services/costmanagement/scheduled_action_resource.go b/internal/services/costmanagement/scheduled_action_resource.go index 9c66404c9491..bd09e2a92b5a 100644 --- a/internal/services/costmanagement/scheduled_action_resource.go +++ b/internal/services/costmanagement/scheduled_action_resource.go @@ -10,8 +10,8 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-helpers/lang/response" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/scheduledactions" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/views" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/scheduledactions" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/views" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" @@ -146,7 +146,7 @@ func (r CostManagementScheduledActionResource) Create() sdk.ResourceFunc { return sdk.ResourceFunc{ Timeout: 30 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { - client := metadata.Client.CostManagement.ScheduledActionsClient_v2022_10_01 + client := metadata.Client.CostManagement.ScheduledActionsClient viewId, err := views.ParseScopedViewID(metadata.ResourceData.Get("view_id").(string)) if err != nil { @@ -223,7 +223,7 @@ func (r CostManagementScheduledActionResource) Read() sdk.ResourceFunc { return sdk.ResourceFunc{ Timeout: 5 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { - client := metadata.Client.CostManagement.ScheduledActionsClient_v2022_10_01 + client := metadata.Client.CostManagement.ScheduledActionsClient id, err := scheduledactions.ParseScopedScheduledActionID(metadata.ResourceData.Id()) if err != nil { @@ -273,7 +273,7 @@ func (r CostManagementScheduledActionResource) Delete() sdk.ResourceFunc { return sdk.ResourceFunc{ Timeout: 30 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { - client := metadata.Client.CostManagement.ScheduledActionsClient_v2022_10_01 + client := metadata.Client.CostManagement.ScheduledActionsClient id, err := scheduledactions.ParseScopedScheduledActionID(metadata.ResourceData.Id()) if err != nil { @@ -293,7 +293,7 @@ func (r CostManagementScheduledActionResource) Update() sdk.ResourceFunc { return sdk.ResourceFunc{ Timeout: 30 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { - client := metadata.Client.CostManagement.ScheduledActionsClient_v2022_10_01 + client := metadata.Client.CostManagement.ScheduledActionsClient id, err := scheduledactions.ParseScopedScheduledActionID(metadata.ResourceData.Id()) if err != nil { diff --git a/internal/services/costmanagement/scheduled_action_resource_test.go b/internal/services/costmanagement/scheduled_action_resource_test.go index cb825873abd8..b148bd78598f 100644 --- a/internal/services/costmanagement/scheduled_action_resource_test.go +++ b/internal/services/costmanagement/scheduled_action_resource_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/scheduledactions" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/scheduledactions" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" @@ -116,7 +116,7 @@ func (t CostManagementScheduledAction) Exists(ctx context.Context, clients *clie return nil, err } - resp, err := clients.CostManagement.ScheduledActionsClient_v2022_10_01.GetByScope(ctx, *id) + resp, err := clients.CostManagement.ScheduledActionsClient.GetByScope(ctx, *id) if err != nil { return nil, fmt.Errorf("retrieving (%s): %+v", *id, err) } diff --git a/internal/services/costmanagement/view_resource_base.go b/internal/services/costmanagement/view_resource_base.go index 9bbdcb28f6ac..cfd14e1f3f8b 100644 --- a/internal/services/costmanagement/view_resource_base.go +++ b/internal/services/costmanagement/view_resource_base.go @@ -10,7 +10,7 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-helpers/lang/response" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/views" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/views" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" diff --git a/internal/services/costmanagement/view_resource_group_resource_test.go b/internal/services/costmanagement/view_resource_group_resource_test.go index 3c7e73394448..0b455db4a3f6 100644 --- a/internal/services/costmanagement/view_resource_group_resource_test.go +++ b/internal/services/costmanagement/view_resource_group_resource_test.go @@ -8,7 +8,7 @@ import ( "fmt" "testing" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/views" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/views" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" diff --git a/internal/services/costmanagement/view_subscription_resource_test.go b/internal/services/costmanagement/view_subscription_resource_test.go index 550debc6cab5..c23482fceaff 100644 --- a/internal/services/costmanagement/view_subscription_resource_test.go +++ b/internal/services/costmanagement/view_subscription_resource_test.go @@ -8,7 +8,7 @@ import ( "fmt" "testing" - "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2022-10-01/views" + "github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2023-08-01/views" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" diff --git a/website/docs/r/cost_anomaly_alert.html.markdown b/website/docs/r/cost_anomaly_alert.html.markdown index 2ae36b1f3d82..ae285d4d2cf1 100644 --- a/website/docs/r/cost_anomaly_alert.html.markdown +++ b/website/docs/r/cost_anomaly_alert.html.markdown @@ -38,7 +38,7 @@ The following arguments are supported: * `email_subject` - (Required) The email subject of the Cost Anomaly Alerts. Maximum length of the subject is 70. - +* `notification_email` - (Optional) The email address of the point of contact that should get the unsubscribe requests and notification emails. ---