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

New data source: azurerm_consumption_budget_resource_group #12538

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
package consumption

import (
"fmt"
"time"

"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/consumption/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/consumption/validate"
resourceParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceArmConsumptionBudgetResourceGroupDataSource() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: resourceArmConsumptionBudgetResourceGroupDataSourceRead,
Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.ConsumptionBudgetName(),
},

"resource_group_id": {
Type: pluginsdk.TypeString,
Required: true,
},

"amount": {
Type: pluginsdk.TypeFloat,
Computed: true,
},

"filter": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"dimension": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"operator": {
Type: pluginsdk.TypeString,
Computed: true,
},
"values": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
"tag": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"operator": {
Type: pluginsdk.TypeString,
Computed: true,
},
"values": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
"not": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"dimension": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"operator": {
Type: pluginsdk.TypeString,
Computed: true,
},
"values": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
"tag": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"operator": {
Type: pluginsdk.TypeString,
Computed: true,
},
"values": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
},
},
},
},
},
},

"notification": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},
"threshold": {
Type: pluginsdk.TypeInt,
Computed: true,
},
"operator": {
Type: pluginsdk.TypeString,
Computed: true,
},

"contact_emails": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"contact_groups": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"contact_roles": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},

"time_grain": {
Type: pluginsdk.TypeString,
Computed: true,
},

"time_period": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"start_date": {
Type: pluginsdk.TypeString,
Computed: true,
},
"end_date": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
},
}
}

func resourceArmConsumptionBudgetResourceGroupDataSourceRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Consumption.BudgetsClient
subscriptionID := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

resourceGroupId, err := resourceParse.ResourceGroupID(d.Get("resource_group_id").(string))
if err != nil {
return err
}

id := parse.NewConsumptionBudgetResourceGroupID(subscriptionID, resourceGroupId.ResourceGroup, d.Get("name").(string))
d.SetId(id.ID())

resp, err := client.Get(ctx, resourceGroupId.ID(), id.BudgetName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("making read request on %s: %+v", id, err)
}

d.Set("name", resp.Name)
if resp.Amount != nil {
amount, _ := resp.Amount.Float64()
d.Set("amount", amount)
}

d.Set("time_grain", string(resp.TimeGrain))
d.Set("time_period", FlattenConsumptionBudgetTimePeriod(resp.TimePeriod))
d.Set("notification", FlattenConsumptionBudgetNotifications(resp.Notifications))
d.Set("filter", FlattenConsumptionBudgetFilter(resp.Filter))

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package consumption_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type ConsumptionBudgetResourceGroupDataSource struct{}

func TestAccDataSourceConsumptionBudgetResourceGroup_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_consumption_budget_resource_group", "test")
r := ConsumptionBudgetResourceGroupDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("name").Exists(),
check.That(data.ResourceName).Key("resource_group_id").Exists(),
gro1m marked this conversation as resolved.
Show resolved Hide resolved
check.That(data.ResourceName).Key("amount").HasValue("1000"),
check.That(data.ResourceName).Key("time_grain").HasValue("Monthly"),
check.That(data.ResourceName).Key("time_period.#").Exists(),
check.That(data.ResourceName).Key("time_period.0.start_date").Exists(),
check.That(data.ResourceName).Key("filter.#").Exists(),
check.That(data.ResourceName).Key("filter.0.tag.0.name").HasValue("foo"),
check.That(data.ResourceName).Key("filter.0.tag.0.values.#").Exists(),
check.That(data.ResourceName).Key("notification.#").Exists(),
check.That(data.ResourceName).Key("notification.0.threshold").HasValue("90"),
check.That(data.ResourceName).Key("notification.0.operator").HasValue("EqualTo"),
check.That(data.ResourceName).Key("notification.0.enabled").Exists(),
check.That(data.ResourceName).Key("notification.0.contact_emails.0").HasValue("[email protected]"),
check.That(data.ResourceName).Key("notification.0.contact_emails.1").HasValue("[email protected]"),
),
},
})
}

func (d ConsumptionBudgetResourceGroupDataSource) basic(data acceptance.TestData) string {
config := ConsumptionBudgetResourceGroupResource{}.basic(data)
return fmt.Sprintf(`
%s

data "azurerm_consumption_budget_resource_group" "test" {
name = azurerm_consumption_budget_resource_group.test.name
resource_group_id = azurerm_consumption_budget_resource_group.test.resource_group_id
}
`, config)
}
10 changes: 6 additions & 4 deletions internal/services/consumption/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const (
// as the core logic for the Consumption Budget resources is generic and has been
// extracted out of the specific Consumption Budget resources. These constants are
// used when the generic Consumption Budget functions require a resource name.
consumptionBudgetResourceGroupName = "azurerm_consumption_budget_resource_group"
consumptionBudgetSubscriptionName = "azurerm_consumption_budget_subscription"
consumptionBudgetSubscriptionDataSourceName = "azurerm_consumption_budget_subscription"
consumptionBudgetResourceGroupName = "azurerm_consumption_budget_resource_group"
consumptionBudgetSubscriptionName = "azurerm_consumption_budget_subscription"
consumptionBudgetResourceGroupDataSourceName = "azurerm_consumption_budget_resource_group"
consumptionBudgetSubscriptionDataSourceName = "azurerm_consumption_budget_subscription"
)

type Registration struct{}
Expand All @@ -31,7 +32,8 @@ func (r Registration) WebsiteCategories() []string {
// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{
consumptionBudgetSubscriptionDataSourceName: resourceArmConsumptionBudgetSubscriptionDataSource(),
consumptionBudgetResourceGroupDataSourceName: resourceArmConsumptionBudgetResourceGroupDataSource(),
consumptionBudgetSubscriptionDataSourceName: resourceArmConsumptionBudgetSubscriptionDataSource(),
}
}

Expand Down
Loading