Skip to content

Commit 0f3f509

Browse files
author
hellertang
authored
postgresql support security_group (#670)
1 parent b82af15 commit 0f3f509

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

tencentcloud/resource_tc_postgresql_instance.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ package tencentcloud
5757
import (
5858
"context"
5959
"fmt"
60+
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
6061
"strings"
6162

6263
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -109,6 +110,16 @@ func resourceTencentCloudPostgresqlInstance() *schema.Resource {
109110
Optional: true,
110111
Description: "ID of subnet.",
111112
},
113+
"security_groups": {
114+
Type: schema.TypeSet,
115+
Optional: true,
116+
ForceNew: true,
117+
Elem: &schema.Schema{Type: schema.TypeString},
118+
Set: func(v interface{}) int {
119+
return hashcode.String(v.(string))
120+
},
121+
Description: "ID of security group. If both vpc_id and subnet_id are not set, this argument should not be set either.",
122+
},
112123
"storage": {
113124
Type: schema.TypeInt,
114125
Required: true,
@@ -210,6 +221,7 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
210221
projectId = d.Get("project_id").(int)
211222
subnetId = d.Get("subnet_id").(string)
212223
vpcId = d.Get("vpc_id").(string)
224+
securityGroups = d.Get("security_groups").(*schema.Set).List()
213225
zone = d.Get("availability_zone").(string)
214226
storage = d.Get("storage").(int)
215227
memory = d.Get("memory").(int)
@@ -225,6 +237,12 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
225237
var outErr, inErr error
226238
var allowVersion, allowMemory []string
227239

240+
requestSecurityGroup := make([]string, 0, len(securityGroups))
241+
242+
for _, v := range securityGroups {
243+
requestSecurityGroup = append(requestSecurityGroup, v.(string))
244+
}
245+
228246
// get speccode with engine_version and memory
229247
outErr = resource.Retry(readRetryTimeout, func() *resource.RetryError {
230248
speccodes, inErr := postgresqlService.DescribeSpecinfos(ctx, zone)
@@ -262,7 +280,21 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
262280
}
263281

264282
outErr = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
265-
instanceId, inErr = postgresqlService.CreatePostgresqlInstance(ctx, name, dbVersion, payType, specCode, 0, projectId, period, subnetId, vpcId, zone, storage, username, password, charset)
283+
instanceId, inErr = postgresqlService.CreatePostgresqlInstance(ctx,
284+
name,
285+
dbVersion,
286+
payType, specCode,
287+
0,
288+
projectId,
289+
period,
290+
subnetId,
291+
vpcId,
292+
zone,
293+
requestSecurityGroup,
294+
storage,
295+
username,
296+
password,
297+
charset)
266298
if inErr != nil {
267299
return retryError(inErr)
268300
}

tencentcloud/service_tencentcloud_postgresql.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type PostgresqlService struct {
1919
client *connectivity.TencentCloudClient
2020
}
2121

22-
func (me *PostgresqlService) CreatePostgresqlInstance(ctx context.Context, name, dbVersion, chargeType, specCode string, autoRenewFlag, projectId, period int, subnetId, vpcId, zone string, storage int, username, password, charset string) (instanceId string, errRet error) {
22+
func (me *PostgresqlService) CreatePostgresqlInstance(ctx context.Context, name, dbVersion, chargeType, specCode string, autoRenewFlag, projectId, period int, subnetId, vpcId, zone string, securityGroups []string, storage int, username, password, charset string) (instanceId string, errRet error) {
2323
logId := getLogId(ctx)
2424
request := postgresql.NewCreateInstancesRequest()
2525
defer func() {
@@ -43,6 +43,13 @@ func (me *PostgresqlService) CreatePostgresqlInstance(ctx context.Context, name,
4343
request.AdminPassword = &password
4444
request.Charset = &charset
4545

46+
if len(securityGroups) > 0 {
47+
request.SecurityGroupIds = make([]*string, 0, len(securityGroups))
48+
for v := range securityGroups {
49+
request.SecurityGroupIds = append(request.SecurityGroupIds, &securityGroups[v])
50+
}
51+
}
52+
4653
ratelimit.Check(request.GetAction())
4754
response, err := me.client.UsePostgresqlClient().CreateInstances(request)
4855
if err != nil {

0 commit comments

Comments
 (0)