Skip to content

Commit

Permalink
Add support for elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Koo committed May 7, 2024
1 parent e8b1bf7 commit 4686ecc
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 2 deletions.
6 changes: 6 additions & 0 deletions infracost-usage-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1361,3 +1361,9 @@ resource_usage:
database_disk_mb: 131072 # Allocated disk in MB per-member
database_core: 3 # Allocated dedicated CPU per-member
database_members: 4 # Allocated number of members in the cluster

ibm_database.elasticsearch:
database_ram_mb: 12288 # Allocated memory in MB per-member
database_disk_mb: 131072 # Allocated disk in MB per-member
database_core: 3 # Allocated dedicated CPU per-member
database_members: 4 # Allocated number of members in the cluster
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

terraform {
required_providers {
ibm = {
Expand Down Expand Up @@ -58,3 +57,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"

group {
group_id = "member"
memory {
allocation_mb = 12288
}
disk {
allocation_mb = 131072
}
cpu {
allocation_count = 3
}
}
}

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

group {
group_id = "member"
members {
allocation_count = 4
}
memory {
allocation_mb = 15360
}
}
}

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

# group {
# group_id = "member"
# memory {
# allocation_mb = 12288
# }
# disk {
# allocation_mb = 131072
# }
# cpu {
# allocation_count = 3
# }
# }
# }

# resource "ibm_database" "test_es_platinum_db2" {
# name = "demo-es-platinum"
# service = "databases-for-elasticsearch"
# plan = "platinum"
# location = "eu-gb"

# group {
# group_id = "member"
# members {
# allocation_count = 3
# }
# memory {
# allocation_mb = 1024
# }
# }
# }
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@ resource_usage:
database_disk_mb: 131072
database_core: 3
ibm_database.test_db2:
database_ram_mb: 15360
database_members: 4
ibm_database.test_es_enterprise_db1:
database_ram_mb: 12288
database_disk_mb: 131072
database_core: 3
ibm_database.test_es_enterprise_db2:
database_ram_mb: 15360
database_members: 4
112 changes: 111 additions & 1 deletion internal/resources/ibm/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var DatabaseCostMap map[string]DatabaseCostComponentsFunc = map[string]DatabaseC
"databases-for-postgresql": GetPostgresCostComponents,
// "databases-for-etcd":
// "databases-for-redis":
// "databases-for-elasticsearch":
"databases-for-elasticsearch": GetElasticSearchCostComponents,
// "messages-for-rabbitmq":
// "databases-for-mongodb":
// "databases-for-mysql":
Expand Down Expand Up @@ -169,6 +169,116 @@ func GetPostgresCostComponents(r *Database) []*schema.CostComponent {
}
}

func ElasticSearchRAMCostComponent(r *Database) *schema.CostComponent {
var R decimal.Decimal
if r.RAM != nil {
R = ConvertMBtoGB(decimal.NewFromInt(*r.RAM))
} else { // set the default
R = decimal.NewFromInt(1)
}
var m decimal.Decimal
if r.Members != nil {
m = decimal.NewFromInt(*r.Members)
} else { // set the default
m = decimal.NewFromInt(3)
}

cost := R.Mul(m)

costComponent := schema.CostComponent{
Name: "RAM",
Unit: "GB-RAM",
MonthlyQuantity: &cost,
UnitMultiplier: decimal.NewFromInt(1),
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("ibm"),
Region: strPtr(r.Location),
Service: strPtr("databases-for-elasticsearch"),
ProductFamily: strPtr("service"),
},
PriceFilter: &schema.PriceFilter{
Unit: strPtr("GIGABYTE_MONTHS_RAM"),
},
}
return &costComponent
}

func ElasticSearchDiskCostComponent(r *Database) *schema.CostComponent {
var d decimal.Decimal
if r.Disk != nil {
d = ConvertMBtoGB(decimal.NewFromInt(*r.Disk))
} else { // set the default
d = decimal.NewFromInt(5)
}
var m decimal.Decimal
if r.Members != nil {
m = decimal.NewFromInt(*r.Members)
} else { // set the default
m = decimal.NewFromInt(3)
}

cost := d.Mul(m)

costComponent := schema.CostComponent{
Name: "Disk",
Unit: "GB-DISK",
MonthlyQuantity: &cost,
UnitMultiplier: decimal.NewFromInt(1),
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("ibm"),
Region: strPtr(r.Location),
Service: strPtr("databases-for-elasticsearch"),
ProductFamily: strPtr("service"),
},
PriceFilter: &schema.PriceFilter{
Unit: strPtr("GIGABYTE_MONTHS_DISK"),
},
}
return &costComponent
}

func ElasticSearchCoreCostComponent(r *Database) *schema.CostComponent {
var c decimal.Decimal
if r.Core != nil {
c = decimal.NewFromInt(*r.Core)
} else { // set the default
c = decimal.NewFromInt(0)
}
var m decimal.Decimal
if r.Members != nil {
m = decimal.NewFromInt(*r.Members)
} else { // set the default
m = decimal.NewFromInt(3)
}

cost := c.Mul(m)

costComponent := schema.CostComponent{
Name: "Core",
Unit: "Virtual Processor Core",
MonthlyQuantity: &cost,
UnitMultiplier: decimal.NewFromInt(1),
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("ibm"),
Region: strPtr(r.Location),
Service: strPtr("databases-for-elasticsearch"),
ProductFamily: strPtr("service"),
},
PriceFilter: &schema.PriceFilter{
Unit: strPtr("VIRTUAL_PROCESSOR_CORES"),
},
}
return &costComponent
}

func GetElasticSearchCostComponents(r *Database) []*schema.CostComponent {
return []*schema.CostComponent{
ElasticSearchRAMCostComponent(r),
ElasticSearchDiskCostComponent(r),
ElasticSearchCoreCostComponent(r),
}
}

// BuildResource builds a schema.Resource from a valid Database struct.
// This method is called after the resource is initialised by an IaC provider.
// See providers folder for more information.
Expand Down

0 comments on commit 4686ecc

Please sign in to comment.