-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new resource:
azurerm_billing_account_cost_management_export
(#19723)
Fixes #14726
- Loading branch information
Showing
8 changed files
with
650 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
internal/services/costmanagement/export_billing_account_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,65 @@ | ||
package costmanagement | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/services/costmanagement/validate" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" | ||
) | ||
|
||
type BillingAccountCostManagementExportResource struct { | ||
base costManagementExportBaseResource | ||
} | ||
|
||
var _ sdk.Resource = BillingAccountCostManagementExportResource{} | ||
|
||
func (r BillingAccountCostManagementExportResource) Arguments() map[string]*pluginsdk.Schema { | ||
schema := map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringIsNotWhiteSpace, | ||
}, | ||
|
||
"billing_account_id": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringIsNotWhiteSpace, | ||
}, | ||
} | ||
return r.base.arguments(schema) | ||
} | ||
|
||
func (r BillingAccountCostManagementExportResource) Attributes() map[string]*pluginsdk.Schema { | ||
return r.base.attributes() | ||
} | ||
|
||
func (r BillingAccountCostManagementExportResource) ModelObject() interface{} { | ||
return nil | ||
} | ||
|
||
func (r BillingAccountCostManagementExportResource) ResourceType() string { | ||
return "azurerm_billing_account_cost_management_export" | ||
} | ||
|
||
func (r BillingAccountCostManagementExportResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return validate.BillingAccountCostManagementExportID | ||
} | ||
|
||
func (r BillingAccountCostManagementExportResource) Create() sdk.ResourceFunc { | ||
return r.base.createFunc(r.ResourceType(), "billing_account_id") | ||
} | ||
|
||
func (r BillingAccountCostManagementExportResource) Read() sdk.ResourceFunc { | ||
return r.base.readFunc("billing_account_id") | ||
} | ||
|
||
func (r BillingAccountCostManagementExportResource) Delete() sdk.ResourceFunc { | ||
return r.base.deleteFunc() | ||
} | ||
|
||
func (r BillingAccountCostManagementExportResource) Update() sdk.ResourceFunc { | ||
return r.base.updateFunc() | ||
} |
228 changes: 228 additions & 0 deletions
228
internal/services/costmanagement/export_billing_account_resource_test.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,228 @@ | ||
package costmanagement_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/go-azure-sdk/resource-manager/costmanagement/2021-10-01/exports" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/clients" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/utils" | ||
) | ||
|
||
type BillingAccountCostManagementExport struct{} | ||
|
||
func TestAccBillingAccountCostManagementExport_basic(t *testing.T) { | ||
if os.Getenv("ARM_BILLING_ACCOUNT") == "" { | ||
t.Skip("skipping tests - no billing account data provided") | ||
} | ||
|
||
data := acceptance.BuildTestData(t, "azurerm_billing_account_cost_management_export", "test") | ||
r := BillingAccountCostManagementExport{} | ||
|
||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}) | ||
} | ||
|
||
func TestAccBillingAccountCostManagementExport_update(t *testing.T) { | ||
if os.Getenv("ARM_BILLING_ACCOUNT") == "" { | ||
t.Skip("skipping tests - no billing account data provided") | ||
} | ||
|
||
data := acceptance.BuildTestData(t, "azurerm_billing_account_cost_management_export", "test") | ||
r := BillingAccountCostManagementExport{} | ||
|
||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
{ | ||
Config: r.update(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
data.ImportStep(), | ||
}) | ||
} | ||
|
||
func TestAccBillingAccountCostManagementExport_requiresImport(t *testing.T) { | ||
if os.Getenv("ARM_BILLING_ACCOUNT") == "" { | ||
t.Skip("skipping tests - no billing account data provided") | ||
} | ||
|
||
data := acceptance.BuildTestData(t, "azurerm_billing_account_cost_management_export", "test") | ||
r := BillingAccountCostManagementExport{} | ||
|
||
data.ResourceTest(t, r, []acceptance.TestStep{ | ||
{ | ||
Config: r.basic(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).ExistsInAzure(r), | ||
), | ||
}, | ||
{ | ||
Config: r.requiresImport(data), | ||
ExpectError: acceptance.RequiresImportError("azurerm_billing_account_cost_management_export"), | ||
}, | ||
}) | ||
} | ||
|
||
func (t BillingAccountCostManagementExport) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { | ||
id, err := exports.ParseScopedExportID(state.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var opts exports.GetOperationOptions | ||
resp, err := clients.CostManagement.ExportClient.Get(ctx, *id, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("retrieving (%s): %+v", *id, err) | ||
} | ||
|
||
return utils.Bool(resp.Model != nil), nil | ||
} | ||
|
||
func (BillingAccountCostManagementExport) basic(data acceptance.TestData) string { | ||
start := time.Now().AddDate(0, 0, 1).Format("2006-01-02") | ||
end := time.Now().AddDate(0, 0, 2).Format("2006-01-02") | ||
billingAccount := os.Getenv("ARM_BILLING_ACCOUNT") | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-cm-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_storage_account" "test" { | ||
name = "unlikely23exst2acct%s" | ||
resource_group_name = azurerm_resource_group.test.name | ||
location = azurerm_resource_group.test.location | ||
account_tier = "Standard" | ||
account_replication_type = "LRS" | ||
} | ||
resource "azurerm_storage_container" "test" { | ||
name = "acctestcontainer%s" | ||
storage_account_name = azurerm_storage_account.test.name | ||
} | ||
resource "azurerm_billing_account_cost_management_export" "test" { | ||
name = "accs%d" | ||
billing_account_id = "%s" | ||
recurrence_type = "Monthly" | ||
recurrence_period_start_date = "%sT00:00:00Z" | ||
recurrence_period_end_date = "%sT00:00:00Z" | ||
export_data_storage_location { | ||
container_id = azurerm_storage_container.test.resource_manager_id | ||
root_folder_path = "/root" | ||
} | ||
export_data_options { | ||
type = "Usage" | ||
time_frame = "TheLastMonth" | ||
} | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, billingAccount, start, end) | ||
} | ||
|
||
func (BillingAccountCostManagementExport) update(data acceptance.TestData) string { | ||
start := time.Now().AddDate(0, 3, 0).Format("2006-01-02") | ||
end := time.Now().AddDate(0, 4, 0).Format("2006-01-02") | ||
billingAccount := os.Getenv("ARM_BILLING_ACCOUNT") | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-cm-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_storage_account" "test" { | ||
name = "unlikely23exst2acct%s" | ||
resource_group_name = azurerm_resource_group.test.name | ||
location = azurerm_resource_group.test.location | ||
account_tier = "Standard" | ||
account_replication_type = "LRS" | ||
} | ||
resource "azurerm_storage_container" "test" { | ||
name = "acctestcontainer%s" | ||
storage_account_name = azurerm_storage_account.test.name | ||
} | ||
resource "azurerm_billing_account_cost_management_export" "test" { | ||
name = "accrg%d" | ||
billing_account_id = "%s" | ||
recurrence_type = "Monthly" | ||
recurrence_period_start_date = "%sT00:00:00Z" | ||
recurrence_period_end_date = "%sT00:00:00Z" | ||
export_data_storage_location { | ||
container_id = azurerm_storage_container.test.resource_manager_id | ||
root_folder_path = "/root/updated" | ||
} | ||
export_data_options { | ||
type = "Usage" | ||
time_frame = "WeekToDate" | ||
} | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomInteger, billingAccount, start, end) | ||
} | ||
|
||
func (BillingAccountCostManagementExport) requiresImport(data acceptance.TestData) string { | ||
template := BillingAccountCostManagementExport{}.basic(data) | ||
return fmt.Sprintf(` | ||
%s | ||
resource "azurerm_billing_account_cost_management_export" "import" { | ||
name = azurerm_billing_account_cost_management_export.test.name | ||
billing_account_id = azurerm_billing_account_cost_management_export.test.billing_account_id | ||
recurrence_type = azurerm_billing_account_cost_management_export.test.recurrence_type | ||
recurrence_period_start_date = azurerm_billing_account_cost_management_export.test.recurrence_period_start_date | ||
recurrence_period_end_date = azurerm_billing_account_cost_management_export.test.recurrence_period_start_date | ||
export_data_storage_location { | ||
container_id = azurerm_storage_container.test.resource_manager_id | ||
root_folder_path = "/root" | ||
} | ||
export_data_options { | ||
type = "Usage" | ||
time_frame = "TheLastMonth" | ||
} | ||
} | ||
`, template) | ||
} |
59 changes: 59 additions & 0 deletions
59
internal/services/costmanagement/parse/billing_account_cost_management_export.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,59 @@ | ||
package parse | ||
|
||
// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure" | ||
) | ||
|
||
type BillingAccountCostManagementExportId struct { | ||
BillingAccountName string | ||
ExportName string | ||
} | ||
|
||
func NewBillingAccountCostManagementExportID(billingAccountName, exportName string) BillingAccountCostManagementExportId { | ||
return BillingAccountCostManagementExportId{ | ||
BillingAccountName: billingAccountName, | ||
ExportName: exportName, | ||
} | ||
} | ||
|
||
func (id BillingAccountCostManagementExportId) String() string { | ||
segments := []string{ | ||
fmt.Sprintf("Export Name %q", id.ExportName), | ||
fmt.Sprintf("Billing Account Name %q", id.BillingAccountName), | ||
} | ||
segmentsStr := strings.Join(segments, " / ") | ||
return fmt.Sprintf("%s: (%s)", "Billing Account Cost Management Export", segmentsStr) | ||
} | ||
|
||
func (id BillingAccountCostManagementExportId) ID() string { | ||
fmtString := "/providers/Microsoft.Billing/billingAccounts/%s/providers/Microsoft.CostManagement/exports/%s" | ||
return fmt.Sprintf(fmtString, id.BillingAccountName, id.ExportName) | ||
} | ||
|
||
// BillingAccountCostManagementExportID parses a BillingAccountCostManagementExport ID into an BillingAccountCostManagementExportId struct | ||
func BillingAccountCostManagementExportID(input string) (*BillingAccountCostManagementExportId, error) { | ||
id, err := azure.ParseAzureResourceIDWithoutSubscription(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resourceId := BillingAccountCostManagementExportId{} | ||
|
||
if resourceId.BillingAccountName, err = id.PopSegment("billingAccounts"); err != nil { | ||
return nil, err | ||
} | ||
if resourceId.ExportName, err = id.PopSegment("exports"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resourceId, nil | ||
} |
Oops, something went wrong.