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_subscription #12540

Merged
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f8d8399
Add support for azurerm_consumption_budget data source
gro1m Jun 9, 2021
69ae612
update interface
gro1m Jun 10, 2021
50c6e43
update docs
gro1m Jun 10, 2021
f5c157e
make fmt
gro1m Jun 10, 2021
6e1d9d2
fix website lint
gro1m Jun 10, 2021
437eb36
make fmt and fix schema bugs
gro1m Jun 10, 2021
3769218
update
gro1m Jun 10, 2021
c54069a
fix linter errors
gro1m Jun 10, 2021
0c062c7
fix more linter errors
gro1m Jun 10, 2021
3bce3f0
test passes
gro1m Jun 13, 2021
b30b665
make fmt
gro1m Jun 13, 2021
fc6b934
terrafmt
gro1m Jun 13, 2021
6554237
Update .gitignore to ignore any shell scripts
gro1m Jul 9, 2021
5702ae4
Add support for Consumption Budget Subscription level data source
gro1m Jul 9, 2021
c84a7d8
Remove changes in .gitignore again
gro1m Jul 9, 2021
ef9890d
make fmt
gro1m Jul 11, 2021
0f561e2
address review
gro1m Aug 5, 2021
6411a71
Add schema for consumption budget subscription
marc-sensenich Aug 21, 2021
f6bdeea
Merge pull request #1 from marc-sensenich/d/azurerm_consumption_budge…
gro1m Aug 25, 2021
aa18d64
Merge branch 'master' into d/azurerm_consumption_budget_subscription
gro1m Aug 25, 2021
4c972d1
adjust for change of directory structure
gro1m Aug 25, 2021
016d256
make fmt; make terrafmt
gro1m Aug 25, 2021
3d6cf21
remove min and max items from data source elements where Compute is true
gro1m Aug 25, 2021
8e1792c
remove AtLeastOneOf, ExactlyOneOf from data source schemas
gro1m Aug 25, 2021
ee8060a
make fmt
gro1m Aug 25, 2021
1760c5f
fix schema and make independent from schema.go
gro1m Sep 21, 2021
dab62af
fix Computed:true blocks
gro1m Sep 21, 2021
4e6fcf4
use name and resource group name instead of subscription id
gro1m Sep 21, 2021
5a5bbd4
fix: remove Required:true for start_date.
gro1m Sep 21, 2021
1f28f71
remove superfluous general data source schema items from common schem…
gro1m Sep 21, 2021
3faffc9
use data source elements functions in schema
gro1m Sep 21, 2021
08ff285
make fmt ./internal/services/consumption/schema.go
gro1m Sep 21, 2021
bf8af2a
remove Optional: True
gro1m Oct 9, 2021
50eb963
simplify resourceArmConsumptionBudgetSubscriptionDataSourceRead
gro1m Oct 9, 2021
8418e30
simplify tests
gro1m Oct 11, 2021
ac2214d
adjust schema
gro1m Oct 11, 2021
0c8e469
make fmt
gro1m Oct 11, 2021
8aca942
remove Set as elements are specified directly in the schema
gro1m Oct 13, 2021
dc53970
remove Set as elements are specified directly in the schema
gro1m Oct 13, 2021
aece5c5
Apply suggestions from code review
gro1m Oct 13, 2021
a39afb9
make fmt
gro1m Oct 13, 2021
b4d14e2
update checks and make terrafmt
gro1m Oct 13, 2021
b7bf1f3
replace typeset with typelist and fix test
stephybun Oct 14, 2021
bf04a7e
update test and doc for order inconsistency in API
stephybun Oct 14, 2021
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,254 @@
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"
subscriptionParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/subscription/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 resourceArmConsumptionBudgetSubscriptionDataSource() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: resourceArmConsumptionBudgetSubscriptionDataSourceRead,
Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.ConsumptionBudgetName(),
},

"subscription_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 resourceArmConsumptionBudgetSubscriptionDataSourceRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Consumption.BudgetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

subscriptionId, err := subscriptionParse.SubscriptionID(d.Get("subscription_id").(string))
if err != nil {
return err
}

id := parse.NewConsumptionBudgetSubscriptionID(subscriptionId.SubscriptionID, d.Get("name").(string))
d.SetId(id.ID())

resp, err := client.Get(ctx, subscriptionId.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))

// The scope of a Subscription budget resource is the Subscription budget ID
d.Set("subscription_id", d.Get("subscription_id").(string))

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

import (
"fmt"
"testing"

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

type ConsumptionBudgetSubscriptionDataSource struct{}

func TestAccDataSourceConsumptionBudgetSubscription_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_consumption_budget_subscription", "test")
r := ConsumptionBudgetSubscriptionDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("subscription_id").HasValue(data.Client().SubscriptionID),
check.That(data.ResourceName).Key("name").Exists(),
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("time_period.0.end_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("filter.0.dimension.0.name").HasValue("ResourceGroupName"),
check.That(data.ResourceName).Key("filter.0.dimension.1.name").HasValue("ResourceId"),
check.That(data.ResourceName).Key("filter.0.not.0.tag.0.name").HasValue("zip"),
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]"),
check.That(data.ResourceName).Key("notification.0.contact_groups.#").Exists(),
check.That(data.ResourceName).Key("notification.0.contact_roles.0").HasValue("Owner"),
),
},
})
}

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

data "azurerm_consumption_budget_subscription" "test" {
name = azurerm_consumption_budget_subscription.test.name
subscription_id = azurerm_consumption_budget_subscription.test.subscription_id
}
`, config)
}
9 changes: 6 additions & 3 deletions internal/services/consumption/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ 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"
consumptionBudgetResourceGroupName = "azurerm_consumption_budget_resource_group"
consumptionBudgetSubscriptionName = "azurerm_consumption_budget_subscription"
consumptionBudgetSubscriptionDataSourceName = "azurerm_consumption_budget_subscription"
)

type Registration struct{}
Expand All @@ -29,7 +30,9 @@ 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{}
return map[string]*pluginsdk.Resource{
consumptionBudgetSubscriptionDataSourceName: resourceArmConsumptionBudgetSubscriptionDataSource(),
}
}

// SupportedResources returns the supported Resources supported by this Service
Expand Down
Loading