Skip to content

Commit 34bc34d

Browse files
committed
remove parameter speccode
1 parent 8d9d9e8 commit 34bc34d

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

tencentcloud/data_source_tc_postgresql_instances_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ charge_type = "POSTPAID_BY_HOUR"
4949
engine_version = "9.3.5"
5050
root_password = "1qaA2k1wgvfa3ZZZ"
5151
charset = "UTF8"
52-
spec_code = "cdb.pg.z1.2g"
5352
project_id = 0
5453
memory = 2
5554
storage = 100

tencentcloud/resource_tc_postgresql_instance.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,12 @@ func resourceTencentCloudPostgresqlInstance() *schema.Resource {
8585
"storage": {
8686
Type: schema.TypeInt,
8787
Required: true,
88-
Description: "Disk size (in GB). Allowed value is range from 10 to 1000, and the value must be a multiple of 10.",
88+
Description: "Disk size (in GB). Allowed value must be a multiple of 10. The storage must be set with the limit of `storage_min` and `storage_max` which data source `tencentcloud_postgresql_specinfos` provides.",
8989
},
9090
"memory": {
9191
Type: schema.TypeInt,
92-
Optional: true,
93-
Computed: true,
94-
Description: "Memory size (in MB).",
92+
Required: true,
93+
Description: "Memory size (in GB). Allowed value must be larger than `memory` that data source `tencentcloud_postgresql_specinfos` provides.",
9594
},
9695
"project_id": {
9796
Type: schema.TypeInt,
@@ -106,12 +105,6 @@ func resourceTencentCloudPostgresqlInstance() *schema.Resource {
106105
Computed: true,
107106
Description: "Availability zone.",
108107
},
109-
"spec_code": {
110-
Type: schema.TypeString,
111-
ForceNew: true,
112-
Required: true,
113-
Description: "The id of specification of the postgresql instance, like `cdb.pg.z1.2g`, which can be queried with data source `tencentcloud_postgresql_specinfos`.",
114-
},
115108
"root_password": {
116109
Type: schema.TypeString,
117110
Required: true,
@@ -175,28 +168,41 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
175168
name = d.Get("name").(string)
176169
dbVersion = d.Get("engine_version").(string)
177170
payType = d.Get("charge_type").(string)
178-
specCode = d.Get("spec_code").(string)
179171
projectId = d.Get("project_id").(int)
180172
subnetId = d.Get("subnet_id").(string)
181173
vpcId = d.Get("vpc_id").(string)
182174
zone = d.Get("availability_zone").(string)
183175
storage = d.Get("storage").(int)
176+
memory = d.Get("memory").(int)
184177
)
185178

186179
var period = 1
187180
//the sdk asks to set value with 1 when paytype is postpaid
188181

189-
/*
190-
if payType == COMMON_PAYTYPE_PREPAID {
191-
payType = POSTGRESQL_PAYTYPE_PREPAID
192-
} else {
193-
payType = POSTGRESQL_PAYTYPE_POSTPAID
194-
}
182+
var instanceId, specCode string
183+
var outErr, inErr error
195184

196-
*/
185+
//get speccode with engine_version and memory
186+
outErr = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
187+
speccodes, inErr := postgresqlService.DescribeSpecinfos(ctx, zone)
188+
if inErr != nil {
189+
return retryError(inErr)
190+
}
191+
for _, info := range speccodes {
192+
if *info.Version == dbVersion && int(*info.Memory) == 1024*memory {
193+
specCode = *info.SpecCode
194+
break
195+
}
196+
}
197+
return nil
198+
})
199+
if outErr != nil {
200+
return outErr
201+
}
197202

198-
var instanceId string
199-
var outErr, inErr error
203+
if specCode == "" {
204+
return fmt.Errorf("there is no legal speccode matched with engine_version %s and memory %d matched, please check again", dbVersion, memory)
205+
}
200206

201207
outErr = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
202208
instanceId, inErr = postgresqlService.CreatePostgresqlInstance(ctx, name, dbVersion, payType, specCode, 0, projectId, period, subnetId, vpcId, zone, storage)
@@ -215,6 +221,7 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
215221
if err != nil {
216222
return retryError(err)
217223
} else if has && *instance.DBInstanceStatus == "init" {
224+
memory = int(*instance.DBInstanceMemory)
218225
return nil
219226
} else if !has {
220227
return resource.NonRetryableError(fmt.Errorf("create postgresql instance fail"))

tencentcloud/resource_tc_postgresql_instance_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ func TestAccTencentCloudPostgresqlInstanceResource(t *testing.T) {
2727
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "charge_type", "POSTPAID_BY_HOUR"),
2828
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "vpc_id"),
2929
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "subnet_id"),
30-
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "memory", "2"),
30+
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "memory", "4"),
3131
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "storage", "100"),
32-
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "spec_code", "cdb.pg.z1.2g"),
3332
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "project_id", "0"),
3433
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "create_time"),
3534
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "public_access_switch", "false"),
@@ -57,7 +56,6 @@ func TestAccTencentCloudPostgresqlInstanceResource(t *testing.T) {
5756
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "subnet_id"),
5857
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "memory", "4"),
5958
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "storage", "250"),
60-
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "spec_code", "cdb.pg.z1.2g"),
6159
resource.TestCheckResourceAttrSet(testPostgresqlInstanceResourceKey, "create_time"),
6260
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "project_id", "1154137"),
6361
resource.TestCheckResourceAttr(testPostgresqlInstanceResourceKey, "public_access_switch", "true"),
@@ -138,10 +136,9 @@ resource "tencentcloud_postgresql_instance" "test" {
138136
subnet_id = "subnet-pyio7yog"
139137
engine_version = "9.3.5"
140138
root_password = "1qaA2k1wgvfa3ZZZ"
141-
charset = "UTF8"
142-
spec_code = "cdb.pg.z1.2g"
139+
charset = "LATIN1"
143140
project_id = 0
144-
memory = 2
141+
memory = 4
145142
storage = 100
146143
}
147144
`
@@ -155,8 +152,7 @@ resource "tencentcloud_postgresql_instance" "test" {
155152
subnet_id = "subnet-pyio7yog"
156153
engine_version = "9.3.5"
157154
root_password = "1qaA2k1wgvfa3ZZZZ"
158-
charset = "UTF8"
159-
spec_code = "cdb.pg.z1.2g"
155+
charset = "LATIN1"
160156
project_id = 1154137
161157
public_access_switch = true
162158
memory = 4

website/docs/r/postgresql_instance.html.markdown

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ Use this resource to create postgresql instance
1818

1919
The following arguments are supported:
2020

21+
* `memory` - (Required) Memory size (in GB). Allowed value must be larger than `memory` that data source `tencentcloud_postgresql_specinfos` provides.
2122
* `name` - (Required) Name of the postgresql instance.
2223
* `root_password` - (Required) Password of root account. This parameter can be specified when you purchase master instances, but it should be ignored when you purchase read-only instances or disaster recovery instances.
23-
* `spec_code` - (Required, ForceNew) The id of specification of the postgresql instance, like `cdb.pg.z1.2g`, which can be queried with data source `tencentcloud_postgresql_specinfos`.
24-
* `storage` - (Required) Disk size (in GB). Allowed value is range from 10 to 1000, and the value must be a multiple of 10.
24+
* `storage` - (Required) Disk size (in GB). Allowed value must be a multiple of 10. The storage must be set with the limit of `storage_min` and `storage_max` which data source `tencentcloud_postgresql_specinfos` provides.
2525
* `availability_zone` - (Optional, ForceNew) Availability zone.
2626
* `charge_type` - (Optional, ForceNew) Pay type of the postgresql instance. For now, only `POSTPAID_BY_HOUR` is valid.
2727
* `charset` - (Optional, ForceNew) Charset of the root account. Valid values are `UTF8`,`LATIN1`.
2828
* `engine_version` - (Optional, ForceNew) Version of the postgresql database engine. Allowed values are `9.3.5`, `9.5.4`, `10.4`.
29-
* `memory` - (Optional) Memory size (in MB).
3029
* `project_id` - (Optional) Project ID, default value is 0.
3130
* `public_access_switch` - (Optional) Indicates whether to enable the access to an instance from public network or not.
3231
* `subnet_id` - (Optional, ForceNew) ID of subnet.

0 commit comments

Comments
 (0)