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

Add schema for consumption budget subscription #1

Merged
merged 1 commit into from
Aug 25, 2021
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
Expand Up @@ -15,7 +15,7 @@ func resourceArmConsumptionBudgetSubscriptionDataSource() *pluginsdk.Resource {
Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Schema: SchemaConsumptionBudgetSubscriptionResource(),
Schema: SchemaConsumptionBudgetSubscriptionDataSource(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ import (

type BudgetSubscriptionDataSource struct{}

func TestAccBudgetSubscriptionDataSource_current(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_consumption_budget", "current")
func TestAccBudgetSubscriptionDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_consumption_budget_subscription", "test")
r := BudgetSubscriptionDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.template(data),
},
{
Config: r.basic(),
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("subscription_id").HasValue(data.Client().SubscriptionID),
check.That(data.ResourceName).Key("name").HasValue("acctestconsumptionbudget-sub"),
check.That(data.ResourceName).Key("name").HasValue(fmt.Sprintf("acctestconsumptionbudget-%d", data.RandomInteger)),
),
},
})
}

func (d BudgetSubscriptionDataSource) basic() string {
return `
func (d BudgetSubscriptionDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

data "azurerm_subscription" "current" {}

data "azurerm_consumption_budget_subscription" "current" {
name = "acctestconsumptionbudget-sub"
data "azurerm_consumption_budget_subscription" "test" {
name = "acctestconsumptionbudget-%d"
subscription_id = data.azurerm_subscription.current.subscription_id
}
`
`, data.RandomInteger)
}

func (BudgetSubscriptionDataSource) template(data acceptance.TestData) string {
Expand All @@ -52,17 +52,9 @@ provider "azurerm" {

data "azurerm_subscription" "current" {}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
lifecycle {
ignore_changes = [tags]
}
}

resource "azurerm_consumption_budget_subscription" "test" {
name = "acctestconsumptionbudget-sub"
resource_group_id = azurerm_resource_group.test.id
name = "acctestconsumptionbudget-%d"
subscription_id = data.azurerm_subscription.current.subscription_id

amount = 1000
time_grain = "Monthly"
Expand Down Expand Up @@ -91,5 +83,5 @@ resource "azurerm_consumption_budget_subscription" "test" {
]
}
}
`, data.RandomInteger, data.Locations.Primary, consumptionBudgetTestStartDate().Format(time.RFC3339))
`, data.RandomInteger, consumptionBudgetTestStartDate().Format(time.RFC3339))
}
207 changes: 207 additions & 0 deletions azurerm/internal/services/consumption/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,210 @@ func SchemaConsumptionBudgetCommonResource() map[string]*pluginsdk.Schema {
},
}
}

func SchemaConsumptionBudgetSubscriptionDataSource() map[string]*pluginsdk.Schema {
subscriptionIDSchema := map[string]*pluginsdk.Schema{
"subscription_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},
}

return azure.MergeSchema(SchemaConsumptionBudgetCommonDataSource(), subscriptionIDSchema)
}

func SchemaConsumptionBudgetFilterDimensionElementDataSource() *pluginsdk.Resource {
return &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"operator": {
Type: pluginsdk.TypeString,
Computed: true,
},
"values": {
Type: pluginsdk.TypeList,
MinItems: 1,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
}
}

func SchemaConsumptionBudgetFilterTagElementDataSource() *pluginsdk.Resource {
return &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,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
}
}

func SchemaConsumptionBudgetNotificationElementDataSource() *pluginsdk.Resource {
return &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,
ValidateFunc: validation.StringIsNotEmpty,
},
},

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

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

func SchemaConsumptionBudgetCommonDataSource() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.ConsumptionBudgetName(),
},

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

"filter": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"dimension": {
Type: pluginsdk.TypeSet,
Optional: true,
Computed: true,
Set: pluginsdk.HashResource(SchemaConsumptionBudgetFilterDimensionElementDataSource()),
Elem: SchemaConsumptionBudgetFilterDimensionElementDataSource(),
AtLeastOneOf: []string{"filter.0.dimension", "filter.0.tag", "filter.0.not"},
},
"tag": {
Type: pluginsdk.TypeSet,
Optional: true,
Computed: true,
Set: pluginsdk.HashResource(SchemaConsumptionBudgetFilterTagElementDataSource()),
Elem: SchemaConsumptionBudgetFilterTagElementDataSource(),
AtLeastOneOf: []string{"filter.0.dimension", "filter.0.tag", "filter.0.not"},
},
"not": {
Type: pluginsdk.TypeList,
Computed: true,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"dimension": {
Type: pluginsdk.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"filter.0.not.0.tag"},
Elem: SchemaConsumptionBudgetFilterDimensionElementDataSource(),
},
"tag": {
Type: pluginsdk.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"filter.0.not.0.dimension"},
Elem: SchemaConsumptionBudgetFilterTagElementDataSource(),
},
},
},
AtLeastOneOf: []string{"filter.0.dimension", "filter.0.tag", "filter.0.not"},
},
},
},
},

"notification": {
Type: pluginsdk.TypeSet,
Computed: true,
MinItems: 1,
MaxItems: 5,
Set: pluginsdk.HashResource(SchemaConsumptionBudgetNotificationElementDataSource()),
Elem: SchemaConsumptionBudgetNotificationElementDataSource(),
},

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

"time_period": {
Type: pluginsdk.TypeList,
Computed: true,
MinItems: 1,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"start_date": {
Type: pluginsdk.TypeString,
Computed: true,
},
"end_date": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
}
}
4 changes: 0 additions & 4 deletions website/docs/d/consumption_budget_subscription.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ The following arguments are supported:

* `subscription_id` - (Required) The ID of the subscription.

---

* `resource_group_id` - (Optional) The ID of the resource group.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:
Expand Down