Skip to content

Commit

Permalink
refactor(postgresql): Update PostgreSQL products Data Source (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
youngmn authored Nov 8, 2024
1 parent 2713b90 commit 1c58dbd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
28 changes: 16 additions & 12 deletions docs/data-sources/postgresql_products.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,36 @@ Get a list of PostgreSQL products.
## Example Usage

```terraform
data "ncloud_postgresql_image_products" "example" {
}
data "ncloud_postgresql_products" "all" {
image_product_code = "SW.VPGSL.OS.LNX64.ROCKY.0810.PGSQL.B050"
image_product_code = data.ncloud_postgresql_image_products.example.image_product_list.0.product_code
filter {
name = "product_type"
values = ["STAND"]
}
output_file = products.json"
}
output "product_list" {
value = {
for product in data.ncloud_postgresql_products.all.product.list :
image.product_name => image.product_code
for product in data.ncloud_postgresql_products.all.product_list :
product.product_name => product.product_code
}
}
```

Outputs:
`terraform
list_image = {
"vCPU 2EA, Memory 8GB": "SVR.VPGSL.DBSVR.STAND.C002.M008.NET.SSD.B050.G002",
"vCPU 4EA, Memory 16GB": "SVR.VPGSL.DBSVR.STAND.C004.M016.NET.SSD.B050.G002",
"vCPU 8EA, Memory 32GB": "SVR.VPGSL.DBSVR.STAND.C008.M032.NET.SSD.B050.G002",
"vCPU 16EA, Memory 64GB": "SVR.VPGSL.DBSVR.STAND.C016.M064.NET.SSD.B050.G002",
"vCPU 32EA, Memory 128GB": "SVR.VPGSL.DBSVR.STAND.C032.M128.NET.SSD.B050.G002"
```terraform
product_list = {
"vCPU 16EA, Memory 64GB" = "SVR.VPGSL.DBSVR.STAND.C016.M064.NET.SSD.B050.G002"
"vCPU 2EA, Memory 8GB" = "SVR.VPGSL.DBSVR.STAND.C002.M008.NET.SSD.B050.G002"
"vCPU 32EA, Memory 128GB" = "SVR.VPGSL.DBSVR.STAND.C032.M128.NET.SSD.B050.G002"
"vCPU 4EA, Memory 16GB" = "SVR.VPGSL.DBSVR.STAND.C004.M016.NET.SSD.B050.G002"
"vCPU 8EA, Memory 32GB" = "SVR.VPGSL.DBSVR.STAND.C008.M032.NET.SSD.B050.G002"
}
```

## Argument Reference

Expand All @@ -57,8 +62,7 @@ This data source exports the following attributes in addition to the argument ab
* `product_code` - Product code.
* `product_type` - Product type code.
* `product_description` - Product description.
* `engine_version_code` - The engine version of the specific PostgreSQL.
* `infra_resource_type` - Infra resource type code.
* `cpu_count` - CPU count.
* `memory_size` - Memory size.
* `disk_type` - Disk type.
* `disk_type` - Disk type.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (d *postgresqlProductList) refreshFromOutput(ctx context.Context, list []*p
d.ProductList = productListValue
d.ID = types.StringValue(time.Now().UTC().String())

return nil
return diags
}

type postgresqlProductList struct {
Expand All @@ -214,6 +214,7 @@ type postgresqlProductModel struct {
MemorySize types.Int64 `tfsdk:"memory_size"`
DiskType types.String `tfsdk:"disk_type"`
}

type postgresqlProductsToJsonConvert struct {
ProductCode string `json:"product_code"`
ProductName string `json:"product_name"`
Expand All @@ -237,6 +238,7 @@ func (d postgresqlProductModel) attrTypes() map[string]attr.Type {
"product_description": types.StringType,
}
}

func (d *postgresqlProductModel) refreshFromOutput(output *vpostgresql.Product) {
d.ProductCode = types.StringPointerValue(output.ProductCode)
d.ProductName = types.StringPointerValue(output.ProductName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ import (
)

func TestAccDataSourceNcloudPostgresqlProducts_basic(t *testing.T) {
imageProductCode := "SW.VPGSL.OS.LNX64.CNTOS.0708.PGSQL.133.B050"
productType := "STAND"

resource.Test(t, resource.TestCase{
PreCheck: func() { TestAccPreCheck(t) },
ProtoV6ProviderFactories: ProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceNcloudPostgresqlProductsConfig_basic(imageProductCode, productType),
Config: testAccDataSourceNcloudPostgresqlProductsConfig_basic(productType),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.ncloud_postgresql_products.all", "product_list.0.infra_resource_type", "VPGSL"),
resource.TestCheckResourceAttr("data.ncloud_postgresql_products.all", "product_list.0.product_type", productType),
),
},
},
})
}

func testAccDataSourceNcloudPostgresqlProductsConfig_basic(imageProductCode string, productType string) string {
func testAccDataSourceNcloudPostgresqlProductsConfig_basic(productType string) string {
return fmt.Sprintf(`
data "ncloud_postgresql_image_products" "all" { }
data "ncloud_postgresql_products" "all" {
image_product_code = "%s"
image_product_code = data.ncloud_postgresql_image_products.all.image_product_list.0.product_code
filter {
name = "product_type"
values = ["%s"]
}
}
`, imageProductCode, productType)
`, productType)
}

0 comments on commit 1c58dbd

Please sign in to comment.