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

Billing Snapshot Config APIs introduced in Usage Reports #4805

Merged
merged 3 commits into from
Oct 19, 2023
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
93 changes: 93 additions & 0 deletions examples/ibm-usage-reports/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Example for UsageReportsV4

This example illustrates how to use the UsageReportsV4

The following types of resources are supported:

* billing_report_snapshot

## Usage

To run this example, execute the following commands:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Run `terraform destroy` when you don't need these resources.


## UsageReportsV4 resources

billing_report_snapshot resource:

```hcl
resource "billing_report_snapshot" "billing_report_snapshot_instance" {
account_id = var.billing_report_snapshot_account_id
interval = var.billing_report_snapshot_interval
versioning = var.billing_report_snapshot_versioning
report_types = var.billing_report_snapshot_report_types
cos_reports_folder = var.billing_report_snapshot_cos_reports_folder
cos_bucket = var.billing_report_snapshot_cos_bucket
cos_location = var.billing_report_snapshot_cos_location
}
```

## UsageReportsV4 data sources

billing_snapshot_list data source:

```hcl
data "billing_snapshot_list" "billing_snapshot_list_instance" {
account_id = var.billing_snapshot_list_account_id
month = var.billing_snapshot_list_month
date_from = var.billing_snapshot_list_date_from
date_to = var.billing_snapshot_list_date_to
}
```

## Assumptions

1. TODO

## Notes

1. TODO

## Requirements

| Name | Version |
|------|---------|
| terraform | ~> 0.12 |

## Providers

| Name | Version |
|------|---------|
| ibm | 1.13.1 |

## Inputs

| Name | Description | Type | Required |
|------|-------------|------|---------|
| ibmcloud\_api\_key | IBM Cloud API key | `string` | true |
| account_id | Account ID for which billing report snapshot is configured. | `string` | true |
| interval | Frequency of taking the snapshot of the billing reports. | `string` | true |
| versioning | A new version of report is created or the existing report version is overwritten with every update. | `string` | false |
| report_types | The type of billing reports to take snapshot of. Possible values are [account_summary, enterprise_summary, account_resource_instance_usage]. | `list(string)` | false |
| cos_reports_folder | The billing reports root folder to store the billing reports snapshots. Defaults to "IBMCloud-Billing-Reports". | `string` | false |
| cos_bucket | The name of the COS bucket to store the snapshot of the billing reports. | `string` | true |
| cos_location | Region of the COS instance. | `string` | true |
| account_id | Account ID for which the billing report snapshot is requested. | `string` | true |
| month | The month for which billing report snapshot is requested. Format is yyyy-mm. | `string` | true |
| date_from | Timestamp in milliseconds for which billing report snapshot is requested. | `number` | false |
| date_to | Timestamp in milliseconds for which billing report snapshot is requested. | `number` | false |

## Outputs

| Name | Description |
|------|-------------|
| billing_report_snapshot | billing_report_snapshot object |
| billing_snapshot_list | billing_snapshot_list object |
20 changes: 20 additions & 0 deletions examples/ibm-usage-reports/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
}

// Provision billing_report_snapshot resource instance
resource "ibm_billing_report_snapshot" "billing_report_snapshot_instance" {
interval = var.billing_report_snapshot_interval
versioning = var.billing_report_snapshot_versioning
report_types = var.billing_report_snapshot_report_types
cos_reports_folder = var.billing_report_snapshot_cos_reports_folder
cos_bucket = var.billing_report_snapshot_cos_bucket
cos_location = var.billing_report_snapshot_cos_location
}

// Create billing_snapshot_list data source
data "ibm_billing_snapshot_list" "billing_snapshot_list_instance" {
month = var.billing_snapshot_list_month
date_from = var.billing_snapshot_list_date_from
date_to = var.billing_snapshot_list_date_to
}
6 changes: 6 additions & 0 deletions examples/ibm-usage-reports/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This output allows billing_report_snapshot data to be referenced by other resources and the terraform CLI
// Modify this output if only certain data should be exposed
output "ibm_billing_report_snapshot" {
value = ibm_billing_report_snapshot.billing_report_snapshot_instance
description = "billing_report_snapshot resource instance"
}
58 changes: 58 additions & 0 deletions examples/ibm-usage-reports/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
variable "ibmcloud_api_key" {
description = "IBM Cloud API key"
type = string
}

// Resource arguments for billing_report_snapshot
variable "billing_report_snapshot_interval" {
description = "Frequency of taking the snapshot of the billing reports."
type = string
default = "daily"
}
variable "billing_report_snapshot_versioning" {
description = "A new version of report is created or the existing report version is overwritten with every update."
type = string
default = "new"
}
variable "billing_report_snapshot_report_types" {
description = "The type of billing reports to take snapshot of. Possible values are [account_summary, enterprise_summary, account_resource_instance_usage]."
type = list(string)
default = ["account_summary","enterprise_summary","account_resource_instance_usage"]
}
variable "billing_report_snapshot_cos_reports_folder" {
description = "The billing reports root folder to store the billing reports snapshots. Defaults to \"IBMCloud-Billing-Reports\"."
type = string
default = "IBMCloud-Billing-Reports"
}
variable "billing_report_snapshot_cos_bucket" {
description = "The name of the COS bucket to store the snapshot of the billing reports."
type = string
default = "bucket_name"
}
variable "billing_report_snapshot_cos_location" {
description = "Region of the COS instance."
type = string
default = "us-south"
}

// Data source arguments for billing_snapshot_list
variable "billing_snapshot_list_account_id" {
description = "Account ID for which the billing report snapshot is requested."
type = string
default = "abc"
}
variable "billing_snapshot_list_month" {
description = "The month for which billing report snapshot is requested. Format is yyyy-mm."
type = string
default = "2023-02"
}
variable "billing_snapshot_list_date_from" {
description = "Timestamp in milliseconds for which billing report snapshot is requested."
type = number
default = 1675209600000
}
variable "billing_snapshot_list_date_to" {
description = "Timestamp in milliseconds for which billing report snapshot is requested."
type = number
default = 1675987200000
}
9 changes: 9 additions & 0 deletions examples/ibm-usage-reports/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.0"
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.52.0-beta0"
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/IBM/ibm-hpcs-uko-sdk v0.0.20-beta
github.com/IBM/keyprotect-go-client v0.12.2
github.com/IBM/networking-go-sdk v0.42.2
github.com/IBM/platform-services-go-sdk v0.50.4
github.com/IBM/platform-services-go-sdk v0.52.0
github.com/IBM/project-go-sdk v0.0.10
github.com/IBM/push-notifications-go-sdk v0.0.0-20210310100607-5790b96c47f5
github.com/IBM/scc-go-sdk/v5 v5.1.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ github.com/IBM/keyprotect-go-client v0.12.2 h1:Cjxcqin9Pl0xz3MnxdiVd4v/eIa79xL3h
github.com/IBM/keyprotect-go-client v0.12.2/go.mod h1:yr8h2noNgU8vcbs+vhqoXp3Lmv73PI0zAc6VMgFvWwM=
github.com/IBM/networking-go-sdk v0.42.2 h1:caqjx4jyFHi10Vlf3skHvlL6K3YJRVstsmCBmvdyqkA=
github.com/IBM/networking-go-sdk v0.42.2/go.mod h1:lTUZwtUkMANMnrLHFIgRhHrkBfwASY/Iho1fabaPHxo=
github.com/IBM/platform-services-go-sdk v0.50.4 h1:YQcdFv5+WW+rH1lideBX4fJbPoS3NauUNqtJdxnnKyI=
github.com/IBM/platform-services-go-sdk v0.50.4/go.mod h1:6LxcUhIaSLP4SuQJXF9oLXBamSQogs5D9BcVwr4hmfU=
github.com/IBM/platform-services-go-sdk v0.52.0 h1:hbf640xE8T0Rwy2IUf5Pu4OATabGS4IDMnEInXUXs4o=
github.com/IBM/platform-services-go-sdk v0.52.0/go.mod h1:6LxcUhIaSLP4SuQJXF9oLXBamSQogs5D9BcVwr4hmfU=
github.com/IBM/project-go-sdk v0.0.10 h1:vHSuemwZ4S4c6BEb22tzsEcPTs/5LnZ0yKpP3GG/GL8=
github.com/IBM/project-go-sdk v0.0.10/go.mod h1:lqe0M4cKvABI1iHR1b+KfasVcxQL6nl2VJ8eOyQs8Ig=
github.com/IBM/push-notifications-go-sdk v0.0.0-20210310100607-5790b96c47f5 h1:NPUhkoOCRuv3OFWt19PmwjXGGTKlvmbuPg9fUrBUNe4=
Expand Down
48 changes: 48 additions & 0 deletions ibm/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ var (
// Enterprise Management
var Account_to_be_imported string

// Billing Snapshot Configuration
var Cos_bucket string
var Cos_location string
var Cos_bucket_update string
var Cos_location_update string
var Cos_reports_folder string
var Snapshot_date_from string
var Snapshot_date_to string
var Snapshot_month string

// Secuity and Complinace Center
var (
SccApiEndpoint string
Expand Down Expand Up @@ -1216,6 +1226,38 @@ func init() {
if Account_to_be_imported == "" {
fmt.Println("[INFO] Set the environment variable ACCOUNT_TO_BE_IMPORTED for testing import enterprise account resource else tests will fail if this is not set correctly")
}
Cos_bucket = os.Getenv("COS_BUCKET")
if Cos_bucket == "" {
fmt.Println("[INFO] Set the environment variable COS_BUCKET for testing CRUD operations on billing snapshot configuration APIs")
}
Cos_location = os.Getenv("COS_LOCATION")
if Cos_location == "" {
fmt.Println("[INFO] Set the environment variable COS_LOCATION for testing CRUD operations on billing snapshot configuration APIs")
}
Cos_bucket_update = os.Getenv("COS_BUCKET_UPDATE")
if Cos_bucket_update == "" {
fmt.Println("[INFO] Set the environment variable COS_BUCKET_UPDATE for testing update operation on billing snapshot configuration API")
}
Cos_location_update = os.Getenv("COS_LOCATION_UPDATE")
if Cos_location_update == "" {
fmt.Println("[INFO] Set the environment variable COS_LOCATION_UPDATE for testing update operation on billing snapshot configuration API")
}
Cos_reports_folder = os.Getenv("COS_REPORTS_FOLDER")
if Cos_reports_folder == "" {
fmt.Println("[INFO] Set the environment variable COS_REPORTS_FOLDER for testing CRUD operations on billing snapshot configuration APIs")
}
Snapshot_date_from = os.Getenv("SNAPSHOT_DATE_FROM")
if Snapshot_date_from == "" {
fmt.Println("[INFO] Set the environment variable SNAPSHOT_DATE_FROM for testing CRUD operations on billing snapshot configuration APIs")
}
Snapshot_date_to = os.Getenv("SNAPSHOT_DATE_TO")
if Snapshot_date_to == "" {
fmt.Println("[INFO] Set the environment variable SNAPSHOT_DATE_TO for testing CRUD operations on billing snapshot configuration APIs")
}
Snapshot_month = os.Getenv("SNAPSHOT_MONTH")
if Snapshot_month == "" {
fmt.Println("[INFO] Set the environment variable SNAPSHOT_MONTH for testing CRUD operations on billing snapshot configuration APIs")
}
HpcsAdmin1 = os.Getenv("IBM_HPCS_ADMIN1")
if HpcsAdmin1 == "" {
fmt.Println("[WARN] Set the environment variable IBM_HPCS_ADMIN1 with a VALID HPCS Admin Key1 Path")
Expand Down Expand Up @@ -1616,6 +1658,12 @@ func TestAccPreCheckCodeEngine(t *testing.T) {
}
}

func TestAccPreCheckUsage(t *testing.T) {
if v := os.Getenv("IC_API_KEY"); v == "" {
t.Fatal("IC_API_KEY must be set for acceptance tests")
}
}

func TestAccPreCheckScc(t *testing.T) {
TestAccPreCheck(t)
if SccApiEndpoint == "" {
Expand Down
48 changes: 48 additions & 0 deletions ibm/conns/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import (
"github.com/IBM/platform-services-go-sdk/metricsrouterv3"
resourcecontroller "github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
resourcemanager "github.com/IBM/platform-services-go-sdk/resourcemanagerv2"
"github.com/IBM/platform-services-go-sdk/usagereportsv4"
project "github.com/IBM/project-go-sdk/projectv1"
"github.com/IBM/push-notifications-go-sdk/pushservicev1"
schematicsv1 "github.com/IBM/schematics-go-sdk/schematicsv1"
Expand Down Expand Up @@ -297,6 +298,7 @@ type ClientSession interface {
CdTektonPipelineV2() (*cdtektonpipelinev2.CdTektonPipelineV2, error)
CodeEngineV2() (*codeengine.CodeEngineV2, error)
ProjectV1() (*project.ProjectV1, error)
UsageReportsV4() (*usagereportsv4.UsageReportsV4, error)
}

type clientSession struct {
Expand Down Expand Up @@ -618,6 +620,15 @@ type clientSession struct {
// Project options
projectClient *project.ProjectV1
projectClientErr error

// Usage Reports options
usageReportsClient *usagereportsv4.UsageReportsV4
usageReportsClientErr error
}

// Usage Reports
func (session clientSession) UsageReportsV4() (*usagereportsv4.UsageReportsV4, error) {
return session.usageReportsClient, session.usageReportsClientErr
}

// AppIDAPI provides AppID Service APIs ...
Expand Down Expand Up @@ -1604,6 +1615,43 @@ func (c *Config) ClientSession() (interface{}, error) {
session.contextBasedRestrictionsClientErr = fmt.Errorf("[ERROR] Error occurred while configuring Context Based Restrictions service: %q", err)
}

// // Usage Reports Service Client
usageReportsURL := usagereportsv4.DefaultServiceURL
if c.Visibility == "private" {
if c.Region == "us-south" || c.Region == "us-east" {
usageReportsURL = ContructEndpoint(fmt.Sprintf("private.%s.usagereports", c.Region), fmt.Sprintf("%s/v1", cloudEndpoint))
} else {
fmt.Println("Private Endpint supports only us-south and us-east region specific endpoint")
usageReportsURL = ContructEndpoint("private.us-south.usagereports", fmt.Sprintf("%s/v1", cloudEndpoint))
}
}
if c.Visibility == "public-and-private" {
if c.Region == "us-south" || c.Region == "us-east" {
usageReportsURL = ContructEndpoint(fmt.Sprintf("private.%s.usagereports", c.Region),
fmt.Sprintf("%s/v1", cloudEndpoint))
} else {
usageReportsURL = usagereportsv4.DefaultServiceURL
}
}
if fileMap != nil && c.Visibility != "public-and-private" {
usageReportsURL = fileFallBack(fileMap, c.Visibility, "IBMCLOUD_USAGE_REPORTS_API_ENDPOINT", c.Region, usageReportsURL)
}
usageReportsClientOptions := &usagereportsv4.UsageReportsV4Options{
Authenticator: authenticator,
URL: EnvFallBack([]string{"IBMCLOUD_USAGE_REPORTS_API_ENDPOINT"}, usageReportsURL),
}
usageReportsClient, err := usagereportsv4.NewUsageReportsV4(usageReportsClientOptions)
if err != nil {
session.usageReportsClientErr = fmt.Errorf("[ERROR] Error occurred while configuring IBM Cloud Usage Reports API service: %q", err)
}
if usageReportsClient != nil && usageReportsClient.Service != nil {
usageReportsClient.Service.EnableRetries(c.RetryCount, c.RetryDelay)
usageReportsClient.SetDefaultHeaders(gohttp.Header{
"X-Original-User-Agent": {fmt.Sprintf("terraform-provider-ibm/%s", version.Version)},
})
}
session.usageReportsClient = usageReportsClient

// CATALOG MANAGEMENT Service
catalogManagementURL := "https://cm.globalcatalog.cloud.ibm.com/api/v1-beta"
if c.Visibility == "private" {
Expand Down
10 changes: 10 additions & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/schematics"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/secretsmanager"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/transitgateway"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/usagereports"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/vpc"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -664,6 +665,9 @@ func Provider() *schema.Provider {
"ibm_enterprise_account_groups": enterprise.DataSourceIBMEnterpriseAccountGroups(),
"ibm_enterprise_accounts": enterprise.DataSourceIBMEnterpriseAccounts(),

// //Added for Usage Reports
"ibm_billing_snapshot_list": usagereports.DataSourceIBMBillingSnapshotList(),

// Added for Secrets Manager
// V1 data sources:
"ibm_secrets_manager_secrets": secretsmanager.DataSourceIBMSecretsManagerSecrets(),
Expand Down Expand Up @@ -1194,6 +1198,9 @@ func Provider() *schema.Provider {
"ibm_enterprise_account_group": enterprise.ResourceIBMEnterpriseAccountGroup(),
"ibm_enterprise_account": enterprise.ResourceIBMEnterpriseAccount(),

// //Added for Usage Reports
"ibm_billing_report_snapshot": usagereports.ResourceIBMBillingReportSnapshot(),

// Added for Schematics
"ibm_schematics_workspace": schematics.ResourceIBMSchematicsWorkspace(),
"ibm_schematics_action": schematics.ResourceIBMSchematicsAction(),
Expand Down Expand Up @@ -1578,6 +1585,9 @@ func Validator() validate.ValidatorDict {
"ibm_iam_policy_template": iampolicy.ResourceIBMIAMPolicyTemplateValidator(),
"ibm_iam_policy_template_version": iampolicy.ResourceIBMIAMPolicyTemplateVersionValidator(),

// // Added for Usage Reports
"ibm_billing_report_snapshot": usagereports.ResourceIBMBillingReportSnapshotValidator(),

// // Added for Secrets Manager
"ibm_sm_secret_group": secretsmanager.ResourceIbmSmSecretGroupValidator(),
"ibm_sm_en_registration": secretsmanager.ResourceIbmSmEnRegistrationValidator(),
Expand Down
Loading
Loading