Skip to content

Commit

Permalink
Fix Elasticsearch and Postgres resources (#198)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
luisarojas authored Sep 23, 2024
1 parent af0e44b commit 143750a
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 348 deletions.
25 changes: 3 additions & 22 deletions infracost-usage-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
48 changes: 48 additions & 0 deletions internal/providers/terraform/ibm/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,68 @@ 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,
Service: service,
Plan: plan,
Location: location,
Group: d.RawValues,
Flavor: flavor,
Disk: disk,
Memory: memory,
CPU: cpu,
Members: members,
}
r.PopulateUsage(u)

configuration := make(map[string]any)
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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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
∙ 6 were estimated
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<CONFIGURATION
Expand All @@ -35,19 +35,21 @@ resource "ibm_database" "test_db1" {
CONFIGURATION
}

resource "ibm_database" "test_db2" {
name = "demo-postgres2"
resource "ibm_database" "postgresql_standard" {
name = "postgres-standard"
service = "databases-for-postgresql"
plan = "standard"
location = "eu-gb"

location = "us-south"
group {
group_id = "member"
memory {
allocation_mb = 15360
allocation_mb = 114688
}
members {
allocation_count = 4
disk {
allocation_mb = 4194304
}
cpu {
allocation_count = 28
}
}
configuration = <<CONFIGURATION
Expand All @@ -57,76 +59,77 @@ resource "ibm_database" "test_db2" {
CONFIGURATION
}

resource "ibm_database" "test_es_enterprise_db1" {
name = "demo-es-enterprise"
service = "databases-for-elasticsearch"
plan = "enterprise"
location = "eu-gb"
# -------------------------------------------
# ELASTICSEARCH
# -------------------------------------------

resource "ibm_database" "elasticsearch_platinum" {
name = "elasticsearch-platinum"
service = "databases-for-elasticsearch"
plan = "platinum"
location = "us-south"
group {
group_id = "member"
memory {
allocation_mb = 12288
allocation_mb = 114688
}
disk {
allocation_mb = 131072
allocation_mb = 4194304
}
cpu {
allocation_count = 3
allocation_count = 28
}
}
}

resource "ibm_database" "test_es_enterprise_db2" {
name = "demo-es-enterprise2"
resource "ibm_database" "elasticsearch_platinum_flavor" {
name = "elasticsearch-platinum-flavor"
service = "databases-for-elasticsearch"
plan = "enterprise"
location = "eu-gb"

group {
plan = "platinum"
location = "us-south"
group { # Note: "memory" not allowed when host_flavor is set
group_id = "member"
members {
allocation_count = 4
host_flavor {
id = "m3c.30x240.encrypted"
}
memory {
allocation_mb = 15360
disk {
allocation_mb = 4194304
}
}
}

resource "ibm_database" "test_es_platinum_db1" {
name = "demo-es-platinum"
resource "ibm_database" "elasticsearch_enterprise" {
name = "elasticsearch-enterprise"
service = "databases-for-elasticsearch"
plan = "platinum"
location = "eu-gb"

plan = "enterprise"
location = "us-south"
group {
group_id = "member"

memory {
allocation_mb = 12288
allocation_mb = 114688
}
disk {
allocation_mb = 131072
allocation_mb = 4194304
}
cpu {
allocation_count = 3
allocation_count = 28
}
}
}

resource "ibm_database" "test_es_platinum_db2" {
name = "demo-es-platinum2"
resource "ibm_database" "elasticsearch_enterprise_flavor" {
name = "elasticsearch-enterprise-flavor"
service = "databases-for-elasticsearch"
plan = "platinum"
location = "eu-gb"

group {
plan = "enterprise"
location = "us-south"
group { # Note: "memory" not allowed when host_flavor is set
group_id = "member"
members {
allocation_count = 4
host_flavor {
id = "m3c.30x240.encrypted"
}
memory {
allocation_mb = 2048
disk {
allocation_mb = 4194304
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,2 @@
version: 0.1
resource_usage:
ibm_database.test_db1:
postgresql_database_ram_mb: 12288
postgresql_database_disk_mb: 131072
postgresql_database_core: 3
ibm_database.test_db2:
postgresql_database_ram_mb: 15360
postgresql_database_members: 4
ibm_database.test_es_enterprise_db1:
elasticsearch_database_ram_mb: 12288
elasticsearch_database_disk_mb: 131072
elasticsearch_database_core: 3
ibm_database.test_es_enterprise_db2:
elasticsearch_database_ram_mb: 15360
elasticsearch_database_members: 4
ibm_database.test_es_platinum_db1:
elasticsearch_database_ram_mb: 12288
elasticsearch_database_disk_mb: 131072
elasticsearch_database_core: 3
ibm_database.test_es_platinum_db2:
elasticsearch_database_ram_mb: 1024
elasticsearch_database_members: 4
Loading

0 comments on commit 143750a

Please sign in to comment.