-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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 Resource: azure_consumption_budget_*
#9201
Merged
Merged
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
c627fab
Add consumption budgets client
marc-sensenich 4ff0ac7
Add expand and flatten for UUID slices
marc-sensenich 9336775
Add consumption budget name validation and ID parsing
marc-sensenich 67d5d27
Add consumption budget expand and flatten funcs
marc-sensenich b1b08e4
Add DiffSuppressFunc for Azure Consumption time_period.end_date
marc-sensenich bfaa9a6
Add validation for time_period.start_date
marc-sensenich c86db60
fixup! Add expand and flatten for UUID slices
marc-sensenich 42d33bb
Add resource azurerm_consumption_budget_subscription
marc-sensenich d11a4b7
Add resource azurerm_consumption_budget_subscription documentation
marc-sensenich a33d4d8
fixup! Add resource azurerm_consumption_budget_subscription documenta…
marc-sensenich 3e19ab8
fixup! Add resource azurerm_consumption_budget_subscription
marc-sensenich e4db287
fixup! Add resource azurerm_consumption_budget_subscription documenta…
marc-sensenich 0f68fd7
Add resource azurerm_consumption_budget_resource_group
marc-sensenich f658b89
fixup! Add resource azurerm_consumption_budget_subscription
marc-sensenich cde0853
fixup! Add resource azurerm_consumption_budget_resource_group
marc-sensenich d782c9d
Add Computed to end_date
marc-sensenich cf64e1e
Move Consumption Budget tests
marc-sensenich 5d647f7
Convert consumption budget tests to testshim
marc-sensenich e72ef0d
Move consumption budget helpers out of azure/helpers
marc-sensenich 8eecbfe
fixup! Convert consumption budget tests to testshim
marc-sensenich 7236af6
run go mod vendor
marc-sensenich 385259e
run make goimports
marc-sensenich de482fb
fixup! Add expand and flatten for UUID slices
marc-sensenich bafdd31
Run go mod tidy and go mod vendor
marc-sensenich 9177685
Update to Consumption API version 2019-10-01
marc-sensenich 9d3b291
Add not block to consumption budgets
marc-sensenich c4c48fb
fixup! Add not block to consumption budgets
marc-sensenich 8338591
Return []interface{} in Consumption Flatten functions
marc-sensenich 4938439
Fix typo in error for expanding time_period in common
marc-sensenich 0e2a63d
Add nil checks in Flattens/Expands for Consuption helpers
marc-sensenich fa6b63a
Clean up and sort imports for azurerm/internal/services/consumption
marc-sensenich 1ddea1c
Use validating importers for Consumption resources
marc-sensenich 3412d12
Add ID method to ConsumptionBudgetId
marc-sensenich b41cbc6
Use ConsumptionBudgetId.ID for setting the resource ID on read
marc-sensenich 241fee5
Use Resource Group ID instead of name and sub for Resource Group Cons…
marc-sensenich 25f650d
Remove website/azurerm.erb as this is no longer used
marc-sensenich fd6074e
fixup! Use Resource Group ID instead of name and sub for Resource Gro…
marc-sensenich 7d6da36
fixup! Add ID method to ConsumptionBudgetId
marc-sensenich 884f53b
Run go mod vendor
marc-sensenich fab7a12
Run terrafmt fmt on consumption_budget_resource_group_resource_test.go
marc-sensenich 8eb5683
Remove UUID slice expand and flatten functions
marc-sensenich 61d4529
fixup! Use Resource Group ID instead of name and sub for Resource Gro…
marc-sensenich 7da9f12
Remove DiffSuppressFuncConsumptionBudgetTimePeriodEndDate as the end_…
marc-sensenich 2a6d556
Add comments around why Consumption Budget resource names are consts
marc-sensenich 926f618
Check that resp.Amount is not nil before setting Consumption Budget a…
marc-sensenich 25e2561
Remove category from Consumption Budget schema as it has only 1 possi…
marc-sensenich 21c34bc
fixup! Use Resource Group ID instead of name and sub for Resource Gro…
marc-sensenich 60f9fd3
Use generated resource IDs for Consumption Budget resources
marc-sensenich cae3cdb
Change id generation in delete for resource group
mbfrahry 98f948e
Update id generation for delete with subscription
mbfrahry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/Azure/azure-sdk-for-go/services/consumption/mgmt/2019-10-01/consumption" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common" | ||
) | ||
|
||
type Client struct { | ||
BudgetsClient *consumption.BudgetsClient | ||
} | ||
|
||
func NewClient(o *common.ClientOptions) *Client { | ||
budgetsClient := consumption.NewBudgetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) | ||
o.ConfigureClient(&budgetsClient.Client, o.ResourceManagerAuthorizer) | ||
|
||
return &Client{ | ||
BudgetsClient: &budgetsClient, | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
azurerm/internal/services/consumption/consumption_budget_common.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package consumption | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/consumption/mgmt/2019-10-01/consumption" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/shopspring/decimal" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmConsumptionBudgetRead(d *schema.ResourceData, meta interface{}, scope, name string) error { | ||
client := meta.(*clients.Client).Consumption.BudgetsClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
resp, err := client.Get(ctx, scope, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("error making read request on Azure Consumption Budget %q for scope %q: %+v", name, scope, 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", schema.NewSet(schema.HashResource(SchemaConsumptionBudgetNotificationElement()), FlattenConsumptionBudgetNotifications(resp.Notifications))) | ||
d.Set("filter", FlattenConsumptionBudgetFilter(resp.Filter)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmConsumptionBudgetDelete(d *schema.ResourceData, meta interface{}, scope string) error { | ||
client := meta.(*clients.Client).Consumption.BudgetsClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resp, err := client.Delete(ctx, scope, name) | ||
|
||
if err != nil { | ||
if !utils.ResponseWasNotFound(resp) { | ||
return fmt.Errorf("error issuing delete request on Azure Consumption Budget %q for scope %q: %+v", name, scope, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmConsumptionBudgetCreateUpdate(d *schema.ResourceData, meta interface{}, resourceName, scope string) error { | ||
client := meta.(*clients.Client).Consumption.BudgetsClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
|
||
if d.IsNewResource() { | ||
existing, err := client.Get(ctx, scope, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("error checking for presence of existing Consumption Budget %q for scope %q: %s", name, scope, err) | ||
} | ||
} | ||
|
||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError(resourceName, *existing.ID) | ||
} | ||
} | ||
|
||
amount := decimal.NewFromFloat(d.Get("amount").(float64)) | ||
timePeriod, err := ExpandConsumptionBudgetTimePeriod(d.Get("time_period").([]interface{})) | ||
if err != nil { | ||
return fmt.Errorf("error expanding `time_period`: %+v", err) | ||
} | ||
|
||
// The Consumption Budget API requires the category type field to be set in a budget's properties. | ||
// 'Cost' is the only valid Budget type today according to the API spec. | ||
category := "Cost" | ||
parameters := consumption.Budget{ | ||
Name: utils.String(name), | ||
BudgetProperties: &consumption.BudgetProperties{ | ||
Amount: &amount, | ||
Category: &category, | ||
Filter: ExpandConsumptionBudgetFilter(d.Get("filter").([]interface{})), | ||
Notifications: ExpandConsumptionBudgetNotifications(d.Get("notification").(*schema.Set).List()), | ||
TimeGrain: consumption.TimeGrainType(d.Get("time_grain").(string)), | ||
TimePeriod: timePeriod, | ||
}, | ||
} | ||
|
||
read, err := client.CreateOrUpdate(ctx, scope, name, parameters) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if read.ID == nil { | ||
return fmt.Errorf("cannot read Azure Consumption Budget %q for scope %q", name, scope) | ||
} | ||
|
||
return nil | ||
} |
79 changes: 79 additions & 0 deletions
79
azurerm/internal/services/consumption/consumption_budget_resource_group_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package consumption | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/consumption/parse" | ||
resourceParse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/resource/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk" | ||
) | ||
|
||
func resourceArmConsumptionBudgetResourceGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmConsumptionBudgetResourceGroupCreateUpdate, | ||
Read: resourceArmConsumptionBudgetResourceGroupRead, | ||
Update: resourceArmConsumptionBudgetResourceGroupCreateUpdate, | ||
Delete: resourceArmConsumptionBudgetResourceGroupDelete, | ||
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { | ||
_, err := parse.ConsumptionBudgetResourceGroupID(id) | ||
return err | ||
}), | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(30 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: SchemaConsumptionBudgetResourceGroupResource(), | ||
} | ||
} | ||
|
||
func resourceArmConsumptionBudgetResourceGroupCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
name := d.Get("name").(string) | ||
resourceGroupId, err := resourceParse.ResourceGroupID(d.Get("resource_group_id").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = resourceArmConsumptionBudgetCreateUpdate(d, meta, consumptionBudgetResourceGroupName, resourceGroupId.ID()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(parse.NewConsumptionBudgetResourceGroupID(resourceGroupId.SubscriptionId, resourceGroupId.ResourceGroup, name).ID()) | ||
|
||
return resourceArmConsumptionBudgetResourceGroupRead(d, meta) | ||
} | ||
|
||
func resourceArmConsumptionBudgetResourceGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
consumptionBudgetId, err := parse.ConsumptionBudgetResourceGroupID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceGroupId := resourceParse.NewResourceGroupID(consumptionBudgetId.SubscriptionId, consumptionBudgetId.ResourceGroup) | ||
|
||
err = resourceArmConsumptionBudgetRead(d, meta, resourceGroupId.ID(), consumptionBudgetId.BudgetName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// The scope of a Resource Group consumption budget is the Resource Group ID | ||
d.Set("resource_group_id", resourceGroupId.ID()) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmConsumptionBudgetResourceGroupDelete(d *schema.ResourceData, meta interface{}) error { | ||
consumptionBudgetId, err := parse.ConsumptionBudgetResourceGroupID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resourceGroupId := resourceParse.NewResourceGroupID(consumptionBudgetId.SubscriptionId, consumptionBudgetId.ResourceGroup) | ||
|
||
return resourceArmConsumptionBudgetDelete(d, meta, resourceGroupId.ID()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than setting the ID through this check. We've been generating the ID and then setting that. That lets us avoid issues where ID is nil.