Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update costmanagement API to 2023-08-01 #27680

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 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,13 @@ func (r AnomalyAlertResource) Create() sdk.ResourceFunc {
schedule.SetEndDateAsTime(time.Now().AddDate(1, 0, 0))
schedule.SetStartDateAsTime(time.Now())

var senderEmail string
if v, ok := metadata.ResourceData.GetOk("notification_email"); ok {
senderEmail = v.(string)
} else {
senderEmail = (*emailAddresses)[0]
}

param := scheduledactions.ScheduledAction{
Kind: pointer.To(scheduledactions.ScheduledActionKindInsightAlert),
Properties: &scheduledactions.ScheduledActionProperties{
Expand All @@ -126,6 +140,7 @@ func (r AnomalyAlertResource) Create() sdk.ResourceFunc {
FileDestination: &scheduledactions.FileDestination{
FileFormats: &[]scheduledactions.FileFormat{},
},
NotificationEmail: &senderEmail,
Notification: scheduledactions.NotificationProperties{
Subject: metadata.ResourceData.Get("email_subject").(string),
Message: utils.String(metadata.ResourceData.Get("message").(string)),
Expand All @@ -134,7 +149,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.CreateOrUpdateByScopeOperationOptions{}); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

Expand Down Expand Up @@ -183,13 +198,21 @@ func (r AnomalyAlertResource) Update() sdk.ResourceFunc {
schedule.SetEndDateAsTime(time.Now().AddDate(1, 0, 0))
schedule.SetStartDateAsTime(time.Now())

var senderEmail string
if v, ok := metadata.ResourceData.GetOk("notification_email"); ok {
senderEmail = v.(string)
} else {
senderEmail = (*emailAddresses)[0]
}

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: &senderEmail,
Notification: scheduledactions.NotificationProperties{
Subject: metadata.ResourceData.Get("email_subject").(string),
Message: utils.String(metadata.ResourceData.Get("message").(string)),
Expand All @@ -198,7 +221,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.CreateOrUpdateByScopeOperationOptions{}); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

Expand Down Expand Up @@ -234,6 +257,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
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 = ["[email protected]", "[email protected]"]
notification_email = "[email protected]"
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

This file was deleted.

This file was deleted.

Loading
Loading