From 143750aad0cbe1fc86b69eca45f07caa659bd117 Mon Sep 17 00:00:00 2001 From: Luisa Rojas Date: Mon, 23 Sep 2024 17:37:03 -0400 Subject: [PATCH] Fix Elasticsearch and Postgres resources (#198) * Separate out postgresql resource type * Remove all usage variables * Update Terraform tests for postgresql and elasticsearch * Clear golden file * Remove all usage variables mentions and use * Redefine database resource struct * Improve definition database resource struct * Fix retrieval of values from resource properties * Re-write cost components for elasticsearch * Fix host flavor cost component display * Update defaults for elasticsearch and postgresql * Remove function * Re-write cost components for postgresql * Update Terraform tests for only supported db plans * Update golden file (NOTE ISSUES WITH PRICING DB) * Fix merge conflict * Update databases golden file with latest princing data * Display float when MB not divisible by 1024 --- infracost-usage-example.yml | 25 +-- internal/providers/terraform/ibm/database.go | 48 ++++++ .../database_test/database_test.golden | 72 ++++----- .../testdata/database_test/database_test.tf | 111 ++++++------- .../database_test/database_test.usage.yml | 21 --- internal/resources/ibm/database.go | 147 +---------------- .../resources/ibm/database_elasticsearch.go | 149 ++++++++++-------- internal/resources/ibm/database_postgresql.go | 128 +++++++++++++++ 8 files changed, 353 insertions(+), 348 deletions(-) create mode 100644 internal/resources/ibm/database_postgresql.go diff --git a/infracost-usage-example.yml b/infracost-usage-example.yml index c0e3484a27f..d83826ddb94 100644 --- a/infracost-usage-example.yml +++ b/infracost-usage-example.yml @@ -110,17 +110,10 @@ resource_type_default_usage: connection: 3 data_transfer_global: 1000 data_transfer_local: 1000 - ibm_database: - postgresql_database_ram_mb: 12288 - postgresql_database_disk_mb: 131072 - postgresql_database_core: 3 - postgresql_database_members: 4 - elasticsearch_database_ram_mb: 12288 - elasticsearch_database_disk_mb: 131072 - elasticsearch_database_core: 3 - elasticsearch_database_members: 4 + ibm_is_share: is-share_monthly_instance_hours: 730 + ibm_is_vpc_server: is.vpn-server_CONNECTION_HOURS: 730 is.vpn-server_INSTANCE_HOURS: 730 @@ -1386,22 +1379,10 @@ resource_usage: data_transfer_local: 2500 # Monthly local traffic through the gateway in GB data_transfer_global: 2500 # Monthly global traffic through the gateway in GB - ibm_database.postgres: - postgresql_database_ram_mb: 12288 # Allocated memory in MB per-member - postgresql_database_disk_mb: 131072 # Allocated disk in MB per-member - postgresql_database_core: 3 # Allocated dedicated CPU per-member - postgresql_database_members: 4 # Allocated number of members in the cluster - - ibm_database.elasticsearch: - elasticsearch_database_ram_mb: 12288 # Allocated memory in MB per-member - elasticsearch_database_disk_mb: 131072 # Allocated disk in MB per-member - elasticsearch_database_core: 3 # Allocated dedicated CPU per-member - elasticsearch_database_members: 4 # Allocated number of members in the cluster - ibm_is_share.nfs: is-share_monthly_instance_hours: 730 # Monthly number of instance hours is-share_monthly_transmitted_gb: 1 # Data transmitted between two file shares for replication - + ibm_is_vpc_server: is.vpn-server_CONNECTION_HOURS: 730 is.vpn-server_INSTANCE_HOURS: 730 diff --git a/internal/providers/terraform/ibm/database.go b/internal/providers/terraform/ibm/database.go index 8a91403a2b5..697dcccd4dc 100644 --- a/internal/providers/terraform/ibm/database.go +++ b/internal/providers/terraform/ibm/database.go @@ -18,6 +18,40 @@ func newDatabase(d *schema.ResourceData, u *schema.UsageData) *schema.Resource { service := d.Get("service").String() name := d.Get("name").String() + var members int64 + if service == "databases-for-elasticsearch" { + members = 3 + } else if service == "databases-for-postgresql" { + members = 2 + } + + var flavor string + var memory int64 + var cpu int64 + var disk int64 + + for _, g := range d.Get("group").Array() { + + if g.Get("group_id").String() == "member" { + + if len(g.Get("host_flavor").Array()) > 0 { + flavor = g.Get("host_flavor").Array()[0].Map()["id"].String() + } + if len(g.Get("memory").Array()) > 0 { + memory = g.Get("memory").Array()[0].Map()["allocation_mb"].Int() + } + if len(g.Get("cpu").Array()) > 0 { + cpu = g.Get("cpu").Array()[0].Map()["allocation_count"].Int() + } + if len(g.Get("members").Array()) > 0 { + members = g.Get("members").Array()[0].Map()["allocation_count"].Int() + } + if len(g.Get("disk").Array()) > 0 { + disk = g.Get("disk").Array()[0].Map()["allocation_mb"].Int() + } + } + } + r := &ibm.Database{ Name: name, Address: d.Address, @@ -25,6 +59,11 @@ func newDatabase(d *schema.ResourceData, u *schema.UsageData) *schema.Resource { Plan: plan, Location: location, Group: d.RawValues, + Flavor: flavor, + Disk: disk, + Memory: memory, + CPU: cpu, + Members: members, } r.PopulateUsage(u) @@ -32,6 +71,15 @@ func newDatabase(d *schema.ResourceData, u *schema.UsageData) *schema.Resource { configuration["service"] = service configuration["plan"] = plan configuration["location"] = location + configuration["disk"] = disk + configuration["members"] = members + + if flavor != "" { + configuration["flavor"] = flavor + } else { + configuration["memory"] = memory + configuration["cpu"] = cpu + } SetCatalogMetadata(d, service, configuration) diff --git a/internal/providers/terraform/ibm/testdata/database_test/database_test.golden b/internal/providers/terraform/ibm/testdata/database_test/database_test.golden index 46312f60b90..db546636a7d 100644 --- a/internal/providers/terraform/ibm/testdata/database_test/database_test.golden +++ b/internal/providers/terraform/ibm/testdata/database_test/database_test.golden @@ -1,44 +1,34 @@ - Name Monthly Qty Unit Monthly Cost - - ibm_database.test_db1 - ├─ RAM (first 1 GB-RAM) 1 GB-RAM $5.23 - ├─ RAM (over 0 GB-RAM) 1 GB-RAM $5.39 - ├─ Disk (first 1 GB-DISK) 1 GB-DISK $0.61 - ├─ Disk (over 0 GB-DISK) 1 GB-DISK $0.63 - └─ Core (first 1 Virtual Processor Core) 1 Virtual Processor Core $31.40 - └─ Core (over 0 Virtual Processor Core) 1 Virtual Processor Core $32.34 - - ibm_database.test_db2 - ├─ RAM (first 1 GB-RAM) 1 GB-RAM $5.23 - ├─ RAM (over 0 GB-RAM) 1 GB-RAM $5.39 - └─ Disk (first 1 GB-DISK) 1 GB-DISK $0.61 - └─ Disk (over 0 GB-DISK) 1 GB-DISK $0.63 - - ibm_database.test_es_enterprise_db1 - ├─ RAM (first 1 GB-RAM) 1 GB-RAM $15.70 - ├─ RAM (over 0 GB-RAM) 1 GB-RAM $16.17 - ├─ Disk (first 1 GB-DISK) 1 GB-DISK $0.61 - ├─ Disk (over 0 GB-DISK) 1 GB-DISK $0.63 - └─ Core (first 1 Virtual Processor Core) 1 Virtual Processor Core $31.40 - └─ Core (over 0 Virtual Processor Core) 1 Virtual Processor Core $32.34 - - ibm_database.test_es_enterprise_db2 - ├─ RAM (first 1 GB-RAM) 1 GB-RAM $15.70 - ├─ RAM (over 0 GB-RAM) 1 GB-RAM $16.17 - └─ Disk (first 1 GB-DISK) 1 GB-DISK $0.61 - └─ Disk (over 0 GB-DISK) 1 GB-DISK $0.63 - - ibm_database.test_es_platinum_db1 - ├─ RAM 36 GB-RAM $967.79 - ├─ Disk 384 GB-DISK $241.27 - └─ Core 9 Virtual Processor Core $291.08 - - ibm_database.test_es_platinum_db2 - ├─ RAM 4 GB-RAM $107.53 - └─ Disk 20 GB-DISK $12.57 - - OVERALL TOTAL $1,837.64 + Name Monthly Qty Unit Monthly Cost + + ibm_database.elasticsearch_enterprise + ├─ RAM (3 members) 336 GB $5,433.46 + ├─ Disk (3 members) 12,288 GB $7,720.55 + └─ Virtual Processor Cores (3 members) 84 CPU $2,716.73 + + ibm_database.elasticsearch_enterprise_flavor + ├─ Host Flavor (3 members, m3c.30x240.encrypted) 3 Flavor $14,553.90 + └─ Disk (3 members) 12,288 GB $7,720.55 + + ibm_database.elasticsearch_platinum + ├─ RAM (3 members) 336 GB $9,032.69 + ├─ Disk (3 members) 12,288 GB $7,720.55 + └─ Virtual Processor Cores (3 members) 84 CPU $2,716.73 + + ibm_database.elasticsearch_platinum_flavor + ├─ Host Flavor (3 members, m3c.30x240.encrypted) 3 Flavor $22,266.54 + └─ Disk (3 members) 12,288 GB $7,720.55 + + ibm_database.postgresql_standard + ├─ RAM (2 members) 224 GB $1,206.67 + ├─ Disk (2 members) 8,192 GB $5,147.03 + └─ Virtual Processor Cores (2 members) 56 CPU $1,811.15 + + ibm_database.postgresql_standard_flavor + ├─ Host Flavor (2 members, m3c.30x240.encrypted) 2 Flavor $4,526.23 + └─ Disk (2 members) 8,192 GB $5,147.03 + + OVERALL TOTAL $105,440.36 ────────────────────────────────── 6 cloud resources were detected: -∙ 6 were estimated, all of which include usage-based costs, see https://infracost.io/usage-file \ No newline at end of file +∙ 6 were estimated \ No newline at end of file diff --git a/internal/providers/terraform/ibm/testdata/database_test/database_test.tf b/internal/providers/terraform/ibm/testdata/database_test/database_test.tf index 35f55084f6d..e4e2b54318e 100644 --- a/internal/providers/terraform/ibm/testdata/database_test/database_test.tf +++ b/internal/providers/terraform/ibm/testdata/database_test/database_test.tf @@ -10,22 +10,22 @@ provider "ibm" { region = "us-south" } -resource "ibm_database" "test_db1" { - name = "demo-postgres" +# ------------------------------------------- +# POSTGRES +# ------------------------------------------- + +resource "ibm_database" "postgresql_standard_flavor" { + name = "postgres-standard-flavour" service = "databases-for-postgresql" plan = "standard" - location = "eu-gb" - - group { + location = "us-south" + group { # Note: "memory" not allowed when host_flavor is set group_id = "member" - memory { - allocation_mb = 12288 + host_flavor { + id = "m3c.30x240.encrypted" } disk { - allocation_mb = 131072 - } - cpu { - allocation_count = 3 + allocation_mb = 4194304 } } configuration = <