Skip to content

Commit

Permalink
Update costmanagement API to 2023-08-01
Browse files Browse the repository at this point in the history
This change updates the costmanagement API to version 2023-08-01, ensuring
compatibility with both the public Azure Cloud and AzureChinaCloud. It's
important to note that AzureChinaCloud has specific API version requirements,
and version 2023-08-01 meets these.

In addition, the `azurerm_cost_anomaly_alert` resource now includes a new
optional parameter, `email_address_sender`, introduced in this API version.
Should this parameter be omitted, the the TF provider will default to using the
first email address listed in `email_addresses`, aligning with the default
behavior observed in the Azure Portal.

The ScheduledActionsClient_v2022_10_01, which previously existed alongside the
ScheduledActionsClient, has been discontinued. This is because the API version
2023-08-01 seems to be compatible with all uses.
  • Loading branch information
s4heid committed Jan 13, 2025
1 parent c76a3d7 commit 1ba74a2
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 42 deletions.
32 changes: 25 additions & 7 deletions internal/services/costmanagement/anomaly_alert_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand All @@ -126,6 +137,7 @@ func (r AnomalyAlertResource) Create() sdk.ResourceFunc {
FileDestination: &scheduledactions.FileDestination{
FileFormats: &[]scheduledactions.FileFormat{},
},
NotificationEmail: &notificationEmail,
Notification: scheduledactions.NotificationProperties{
Subject: metadata.ResourceData.Get("email_subject").(string),
Message: utils.String(metadata.ResourceData.Get("message").(string)),
Expand All @@ -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)
}

Expand Down Expand Up @@ -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: &notificationEmail,
Notification: scheduledactions.NotificationProperties{
Subject: metadata.ResourceData.Get("email_subject").(string),
Message: utils.String(metadata.ResourceData.Get("message").(string)),
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down
30 changes: 29 additions & 1 deletion internal/services/costmanagement/anomaly_alert_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
27 changes: 9 additions & 18 deletions internal/services/costmanagement/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -33,22 +31,15 @@ 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)
}
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion internal/services/costmanagement/export_resource_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions internal/services/costmanagement/scheduled_action_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/services/costmanagement/view_resource_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/cost_anomaly_alert.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---

Expand Down

0 comments on commit 1ba74a2

Please sign in to comment.