-
Notifications
You must be signed in to change notification settings - Fork 475
[Exporter] Added support for databricks_budget_policy resource
#5217
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
Merged
+155
−48
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,114 @@ | ||
| package exporter | ||
|
|
||
| import ( | ||
| "log" | ||
| "strconv" | ||
|
|
||
| "github.com/databricks/databricks-sdk-go/service/billing" | ||
| "github.com/databricks/terraform-provider-databricks/common" | ||
| ) | ||
|
|
||
| func listBudgetPolicies(ic *importContext) error { | ||
| if ic.accountClient == nil { | ||
| return nil | ||
| } | ||
| policies, err := ic.accountClient.BudgetPolicy.ListAll(ic.Context, billing.ListBudgetPoliciesRequest{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, policy := range policies { | ||
| if policy.PolicyId == "" { | ||
| continue | ||
| } | ||
| if !ic.MatchesName(policy.PolicyName) { | ||
| continue | ||
| } | ||
| ic.Emit(&resource{ | ||
| Resource: "databricks_budget_policy", | ||
| ID: policy.PolicyId, | ||
| }) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func importBudgetPolicy(ic *importContext, r *resource) error { | ||
| // Get binding_workspace_ids directly from DataWrapper | ||
| if r.DataWrapper == nil { | ||
| log.Printf("[WARN] DataWrapper is nil for budget policy %s", r.ID) | ||
| return nil | ||
| } | ||
|
|
||
| accountID := ic.Client.Config.AccountID | ||
| // Emit access control rule set for the budget policy | ||
| ic.Emit(&resource{ | ||
| Resource: "databricks_access_control_rule_set", | ||
| ID: "accounts/" + accountID + "/budgetPolicies/" + r.ID + "/ruleSets/default", | ||
| }) | ||
|
|
||
| bindingWorkspaceIdsRaw := r.DataWrapper.Get("binding_workspace_ids") | ||
| if bindingWorkspaceIdsRaw != nil { | ||
| // Convert to slice of int64 | ||
| var bindingWorkspaceIds []int64 | ||
| if workspaceIdsList, ok := bindingWorkspaceIdsRaw.([]int64); ok { | ||
| bindingWorkspaceIds = workspaceIdsList | ||
| } | ||
| // Emit workspace resources for each binding_workspace_id | ||
| if !ic.Client.Config.IsAzure() { | ||
| for _, workspaceId := range bindingWorkspaceIds { | ||
| ic.Emit(&resource{ | ||
| Resource: "databricks_mws_workspaces", | ||
| ID: accountID + "/" + strconv.FormatInt(workspaceId, 10), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func listBudgets(ic *importContext) error { | ||
| updatedSinceMs := ic.getUpdatedSinceMs() | ||
| budgets, err := ic.accountClient.Budgets.ListAll(ic.Context, billing.ListBudgetConfigurationsRequest{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, budget := range budgets { | ||
| if ic.incremental && budget.CreateTime < updatedSinceMs { | ||
| log.Printf("[DEBUG] skipping budget '%s' that was updated at %d (last active=%d)", | ||
| budget.DisplayName, budget.UpdateTime, updatedSinceMs) | ||
| continue | ||
| } | ||
| ic.Emit(&resource{ | ||
| Resource: "databricks_budget", | ||
| ID: ic.accountClient.Config.AccountID + "|" + budget.BudgetConfigurationId, | ||
| Name: budget.DisplayName, | ||
| }) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func importBudget(ic *importContext, r *resource) error { | ||
| var budget billing.BudgetConfiguration | ||
| s := ic.Resources["databricks_budget"].Schema | ||
| common.DataToStructPointer(r.Data, s, &budget) | ||
| if budget.Filter != nil && budget.Filter.WorkspaceId != nil && !ic.accountClient.Config.IsAzure() { | ||
| for _, workspaceId := range budget.Filter.WorkspaceId.Values { | ||
| ic.Emit(&resource{ | ||
| Resource: "databricks_mws_workspaces", | ||
| ID: ic.accountClient.Config.AccountID + "/" + strconv.FormatInt(workspaceId, 10), | ||
| }) | ||
| } | ||
| } | ||
| for _, alert := range budget.AlertConfigurations { | ||
| for _, action := range alert.ActionConfigurations { | ||
| if action.ActionType == billing.ActionConfigurationTypeEmailNotification { | ||
| ic.Emit(&resource{ | ||
| Resource: "databricks_user", | ||
| Attribute: "user_name", | ||
| Value: action.Target, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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
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.
Maybe worth erroring if you encounter something which is not
[]int64?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.
Our API returns []int64