Skip to content

Commit 1a37355

Browse files
author
mikatong
committed
pg support apply template
1 parent d7a9992 commit 1a37355

7 files changed

+327
-0
lines changed

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,7 @@ func Provider() *schema.Provider {
15041504
"tencentcloud_postgresql_instance_ha_config": postgresql.ResourceTencentCloudPostgresqlInstanceHAConfig(),
15051505
"tencentcloud_postgresql_account": postgresql.ResourceTencentCloudPostgresqlAccount(),
15061506
"tencentcloud_postgresql_account_privileges_operation": postgresql.ResourceTencentCloudPostgresqlAccountPrivilegesOperation(),
1507+
"tencentcloud_postgresql_apply_template_operation": postgresql.ResourceTencentCloudPostgresqlApplyTemplateOperation(),
15071508
"tencentcloud_sqlserver_instance": sqlserver.ResourceTencentCloudSqlserverInstance(),
15081509
"tencentcloud_sqlserver_db": sqlserver.ResourceTencentCloudSqlserverDB(),
15091510
"tencentcloud_sqlserver_account": sqlserver.ResourceTencentCloudSqlserverAccount(),

tencentcloud/provider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ TencentDB for PostgreSQL(PostgreSQL)
899899
tencentcloud_postgresql_base_backup
900900
tencentcloud_postgresql_instance_ha_config
901901
tencentcloud_postgresql_account
902+
tencentcloud_postgresql_apply_template_operation
902903

903904
TencentDB for Redis(crs)
904905
Data Source
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package postgresql
2+
3+
import (
4+
"context"
5+
6+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
11+
postgres "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
12+
)
13+
14+
func ResourceTencentCloudPostgresqlApplyTemplateOperation() *schema.Resource {
15+
return &schema.Resource{
16+
Create: resourceTencentCloudPostgresqlApplyTemplateOperationCreate,
17+
Read: resourceTencentCloudPostgresqlApplyTemplateOperationRead,
18+
Delete: resourceTencentCloudPostgresqlApplyTemplateOperationDelete,
19+
Schema: map[string]*schema.Schema{
20+
"db_instance_id": {
21+
Required: true,
22+
ForceNew: true,
23+
Type: schema.TypeString,
24+
Description: "Instance ID.",
25+
},
26+
"template_id": {
27+
Required: true,
28+
ForceNew: true,
29+
Type: schema.TypeString,
30+
Description: "Template ID.",
31+
},
32+
},
33+
}
34+
}
35+
36+
func resourceTencentCloudPostgresqlApplyTemplateOperationCreate(d *schema.ResourceData, meta interface{}) error {
37+
defer tccommon.LogElapsed("resource.tencentcloud_postgresql_apply_template_operation.create")()
38+
defer tccommon.InconsistentCheck(d, meta)()
39+
40+
logId := tccommon.GetLogId(tccommon.ContextNil)
41+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
42+
43+
service := PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
44+
45+
var (
46+
err, innerErr error
47+
dbParmas, diffParmas map[string]string
48+
templateAttributes *postgres.DescribeParameterTemplateAttributesResponseParams
49+
)
50+
dbInstanceId := d.Get("db_instance_id").(string)
51+
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
52+
dbParmas, innerErr = service.DescribePgParams(ctx, dbInstanceId)
53+
if innerErr != nil {
54+
return tccommon.RetryError(innerErr)
55+
}
56+
57+
return nil
58+
})
59+
60+
templateId := d.Get("template_id").(string)
61+
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
62+
templateAttributes, innerErr = service.DescribePostgresqlParameterTemplateById(ctx, templateId)
63+
if innerErr != nil {
64+
return tccommon.RetryError(innerErr)
65+
}
66+
67+
return nil
68+
})
69+
70+
if err != nil {
71+
return err
72+
}
73+
74+
diffParmas = make(map[string]string)
75+
if templateAttributes != nil {
76+
for _, param := range templateAttributes.ParamInfoSet {
77+
name := param.Name
78+
value := param.CurrentValue
79+
if name != nil && value != nil && dbParmas[*name] != *value {
80+
diffParmas[*name] = *value
81+
}
82+
}
83+
}
84+
85+
err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
86+
innerErr = service.ModifyPgParams(ctx, dbInstanceId, diffParmas)
87+
if innerErr != nil {
88+
return tccommon.RetryError(innerErr)
89+
}
90+
91+
return nil
92+
})
93+
94+
if err != nil {
95+
return err
96+
}
97+
98+
err = service.CheckDBInstanceStatus(ctx, dbInstanceId)
99+
if err != nil {
100+
return err
101+
}
102+
103+
d.SetId(dbInstanceId + tccommon.FILED_SP + templateId)
104+
105+
return resourceTencentCloudPostgresqlApplyTemplateOperationRead(d, meta)
106+
}
107+
108+
func resourceTencentCloudPostgresqlApplyTemplateOperationRead(d *schema.ResourceData, meta interface{}) error {
109+
defer tccommon.LogElapsed("resource.tencentcloud_postgresql_apply_template_operation.read")()
110+
defer tccommon.InconsistentCheck(d, meta)()
111+
112+
return nil
113+
}
114+
115+
func resourceTencentCloudPostgresqlApplyTemplateOperationDelete(d *schema.ResourceData, meta interface{}) error {
116+
defer tccommon.LogElapsed("resource.tencentcloud_postgresql_apply_template_operation.delete")()
117+
defer tccommon.InconsistentCheck(d, meta)()
118+
119+
return nil
120+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Provides a resource to create a postgresql account
2+
3+
Example Usage
4+
5+
```hcl
6+
variable "availability_zone" {
7+
default = "ap-guangzhou-3"
8+
}
9+
10+
# create vpc
11+
resource "tencentcloud_vpc" "vpc" {
12+
name = "vpc"
13+
cidr_block = "10.0.0.0/16"
14+
}
15+
16+
# create vpc subnet
17+
resource "tencentcloud_subnet" "subnet" {
18+
availability_zone = var.availability_zone
19+
name = "subnet"
20+
vpc_id = tencentcloud_vpc.vpc.id
21+
cidr_block = "10.0.20.0/28"
22+
is_multicast = false
23+
}
24+
25+
# create postgresql
26+
resource "tencentcloud_postgresql_instance" "example" {
27+
name = "example"
28+
availability_zone = var.availability_zone
29+
charge_type = "POSTPAID_BY_HOUR"
30+
vpc_id = tencentcloud_vpc.vpc.id
31+
subnet_id = tencentcloud_subnet.subnet.id
32+
db_major_version = "10"
33+
engine_version = "10.23"
34+
root_user = "root123"
35+
root_password = "Root123$"
36+
charset = "UTF8"
37+
project_id = 0
38+
cpu = 1
39+
memory = 2
40+
storage = 10
41+
42+
tags = {
43+
test = "tf"
44+
}
45+
}
46+
47+
# create account
48+
resource "tencentcloud_postgresql_account" "example" {
49+
db_instance_id = tencentcloud_postgresql_instance.example.id
50+
user_name = "tf_example"
51+
password = "Password@123"
52+
type = "normal"
53+
remark = "remark"
54+
lock_status = false
55+
}
56+
```
57+
58+
Import
59+
60+
postgres account can be imported using the id, e.g.
61+
62+
```
63+
terraform import tencentcloud_postgresql_account.example postgres-3hk6b6tj#tf_example
64+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package postgresql_test
2+
3+
import (
4+
"testing"
5+
6+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccTencentCloudPostgresqlApplyTemplateOperationResource_basic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
tcacctest.AccPreCheck(t)
15+
},
16+
Providers: tcacctest.AccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testPostgresqlApplyTemplateOperation,
20+
Check: resource.ComposeTestCheckFunc(
21+
resource.TestCheckResourceAttrSet("tencentcloud_postgresql_apply_template_operation.apply_template_operation", "id"),
22+
resource.TestCheckResourceAttrSet("tencentcloud_postgresql_apply_template_operation.apply_template_operation", "db_instance_id"),
23+
resource.TestCheckResourceAttrSet("tencentcloud_postgresql_apply_template_operation.apply_template_operation", "template_id"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
const testPostgresqlApplyTemplateOperation = testAccPostgresqlInstance + `
31+
resource "tencentcloud_postgresql_parameter_template" "parameter_template" {
32+
template_name = "tf_test_apply"
33+
db_major_version = "13"
34+
db_engine = "postgresql"
35+
template_description = "test"
36+
37+
modify_param_entry_set {
38+
name = "vacuum_freeze_table_age"
39+
expected_value = "160000000"
40+
}
41+
}
42+
43+
resource tencentcloud_postgresql_apply_template_operation "apply_template_operation" {
44+
db_instance_id = tencentcloud_postgresql_instance.test.id
45+
template_id = tencentcloud_postgresql_parameter_template.parameter_template.id
46+
}
47+
`
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
subcategory: "TencentDB for PostgreSQL(PostgreSQL)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_postgresql_apply_template_operation"
5+
sidebar_current: "docs-tencentcloud-resource-postgresql_apply_template_operation"
6+
description: |-
7+
Provides a resource to create a postgresql account
8+
---
9+
10+
# tencentcloud_postgresql_apply_template_operation
11+
12+
Provides a resource to create a postgresql account
13+
14+
## Example Usage
15+
16+
```hcl
17+
variable "availability_zone" {
18+
default = "ap-guangzhou-3"
19+
}
20+
21+
# create vpc
22+
resource "tencentcloud_vpc" "vpc" {
23+
name = "vpc"
24+
cidr_block = "10.0.0.0/16"
25+
}
26+
27+
# create vpc subnet
28+
resource "tencentcloud_subnet" "subnet" {
29+
availability_zone = var.availability_zone
30+
name = "subnet"
31+
vpc_id = tencentcloud_vpc.vpc.id
32+
cidr_block = "10.0.20.0/28"
33+
is_multicast = false
34+
}
35+
36+
# create postgresql
37+
resource "tencentcloud_postgresql_instance" "example" {
38+
name = "example"
39+
availability_zone = var.availability_zone
40+
charge_type = "POSTPAID_BY_HOUR"
41+
vpc_id = tencentcloud_vpc.vpc.id
42+
subnet_id = tencentcloud_subnet.subnet.id
43+
db_major_version = "10"
44+
engine_version = "10.23"
45+
root_user = "root123"
46+
root_password = "Root123$"
47+
charset = "UTF8"
48+
project_id = 0
49+
cpu = 1
50+
memory = 2
51+
storage = 10
52+
53+
tags = {
54+
test = "tf"
55+
}
56+
}
57+
58+
# create account
59+
resource "tencentcloud_postgresql_account" "example" {
60+
db_instance_id = tencentcloud_postgresql_instance.example.id
61+
user_name = "tf_example"
62+
password = "Password@123"
63+
type = "normal"
64+
remark = "remark"
65+
lock_status = false
66+
}
67+
```
68+
69+
## Argument Reference
70+
71+
The following arguments are supported:
72+
73+
* `db_instance_id` - (Required, String, ForceNew) Instance ID.
74+
* `template_id` - (Required, String, ForceNew) Template ID.
75+
76+
## Attributes Reference
77+
78+
In addition to all arguments above, the following attributes are exported:
79+
80+
* `id` - ID of the resource.
81+
82+
83+
84+
## Import
85+
86+
postgres account can be imported using the id, e.g.
87+
88+
```
89+
terraform import tencentcloud_postgresql_account.example postgres-3hk6b6tj#tf_example
90+
```
91+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5828,6 +5828,9 @@
58285828
<li>
58295829
<a href="/docs/providers/tencentcloud/r/postgresql_account.html">tencentcloud_postgresql_account</a>
58305830
</li>
5831+
<li>
5832+
<a href="/docs/providers/tencentcloud/r/postgresql_apply_template_operation.html">tencentcloud_postgresql_apply_template_operation</a>
5833+
</li>
58315834
<li>
58325835
<a href="/docs/providers/tencentcloud/r/postgresql_backup_download_restriction_config.html">tencentcloud_postgresql_backup_download_restriction_config</a>
58335836
</li>

0 commit comments

Comments
 (0)