Skip to content

Commit bad7fc0

Browse files
authored
Merge pull request #718 from tencentcloudstack/fix/pgsql-sec-group-modify
feat: pgsql sec group modify
2 parents 694011c + b1b6946 commit bad7fc0

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 1.60.3 (November 3, 2021)
2+
3+
BUGFIXES:
4+
5+
* Resource `tencentcloud_tcr_repository` fix inaccurate document and example usage
6+
7+
ENHANCEMENTS:
8+
9+
* Resource `tencentcloud_postgresql_instance` support modifying `security_groups`
10+
111
## 1.60.2 (November 1, 2021)
212

313
BUGFIXES:

tencentcloud/resource_tc_postgresql_instance.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ func resourceTencentCloudPostgresqlInstance() *schema.Resource {
114114
"security_groups": {
115115
Type: schema.TypeSet,
116116
Optional: true,
117-
ForceNew: true,
118117
Elem: &schema.Schema{Type: schema.TypeString},
119118
Set: func(v interface{}) int {
120119
return hashcode.String(v.(string))
@@ -507,6 +506,22 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
507506
d.SetPartial("root_password")
508507
}
509508

509+
if d.HasChange("security_groups") {
510+
511+
// Only redis service support modify Generic DB instance security groups
512+
service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn}
513+
ids := d.Get("security_groups").(*schema.Set).List()
514+
var sgIds []*string
515+
for _, id := range ids {
516+
sgIds = append(sgIds, helper.String(id.(string)))
517+
}
518+
err := service.ModifyDBInstanceSecurityGroups(ctx, "postgres", d.Id(), sgIds)
519+
if err != nil {
520+
return err
521+
}
522+
d.SetPartial("security_groups")
523+
}
524+
510525
if d.HasChange("tags") {
511526

512527
oldValue, newValue := d.GetChange("tags")
@@ -608,6 +623,17 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
608623
}
609624
_ = d.Set("public_access_switch", public_access_switch)
610625

626+
// security groups
627+
// Only redis service support modify Generic DB instance security groups
628+
redisService := RedisService{client:meta.(*TencentCloudClient).apiV3Conn}
629+
sg, err := redisService.DescribeDBSecurityGroups(ctx, "postgres", d.Id())
630+
if err != nil {
631+
return err
632+
}
633+
if len(sg) > 0 {
634+
_ = d.Set("security_groups", sg)
635+
}
636+
611637
// computed
612638
_ = d.Set("create_time", instance.CreateTime)
613639
_ = d.Set("status", instance.DBInstanceStatus)

tencentcloud/service_tencentcloud_redis.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,61 @@ func (me *RedisService) DescribeInstanceSecurityGroup(ctx context.Context, redis
586586
return
587587
}
588588

589+
// DescribeDBSecurityGroups support query different type of DB by passing product name
590+
func (me *RedisService) DescribeDBSecurityGroups(ctx context.Context, product string, instanceId string) (sg []string, errRet error) {
591+
logId := getLogId(ctx)
592+
request := redis.NewDescribeDBSecurityGroupsRequest()
593+
request.Product = &product
594+
request.InstanceId = &instanceId
595+
defer func() {
596+
if errRet != nil {
597+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
598+
logId, request.GetAction(), request.ToJsonString(), errRet.Error())
599+
}
600+
}()
601+
ratelimit.Check(request.GetAction())
602+
response, err := me.client.UseRedisClient().DescribeDBSecurityGroups(request)
603+
if err == nil {
604+
log.Printf("[DEBUG]%s api[%s] , request body [%s], response body[%s]\n",
605+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
606+
}
607+
if err != nil {
608+
errRet = err
609+
return
610+
}
611+
612+
groups := response.Response.Groups
613+
if len(groups) > 0 {
614+
for i := range groups {
615+
sg = append(sg, *groups[i].SecurityGroupId)
616+
}
617+
}
618+
return
619+
}
620+
621+
func (me *RedisService) ModifyDBInstanceSecurityGroups(ctx context.Context, product string, instanceId string, securityGroupIds []*string) (errRet error) {
622+
logId := getLogId(ctx)
623+
request := redis.NewModifyDBInstanceSecurityGroupsRequest()
624+
request.Product = &product
625+
request.InstanceId = &instanceId
626+
request.SecurityGroupIds = securityGroupIds
627+
628+
defer func() {
629+
if errRet != nil {
630+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
631+
logId, request.GetAction(), request.ToJsonString(), errRet.Error())
632+
}
633+
}()
634+
ratelimit.Check(request.GetAction())
635+
response, err := me.client.UseRedisClient().ModifyDBInstanceSecurityGroups(request)
636+
if err == nil {
637+
log.Printf("[DEBUG]%s api[%s] , request body [%s], response body[%s]\n",
638+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
639+
}
640+
errRet = err
641+
return
642+
}
643+
589644
func (me *RedisService) DestroyPostpaidInstance(ctx context.Context, redisId string) (taskId int64, errRet error) {
590645
logId := getLogId(ctx)
591646
request := redis.NewDestroyPostpaidInstanceRequest()

website/docs/r/postgresql_instance.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The following arguments are supported:
6969
* `project_id` - (Optional) Project id, default value is `0`.
7070
* `public_access_switch` - (Optional) Indicates whether to enable the access to an instance from public network or not.
7171
* `root_user` - (Optional, ForceNew) Instance root account name. This parameter is optional, Default value is `root`.
72-
* `security_groups` - (Optional, ForceNew) ID of security group. If both vpc_id and subnet_id are not set, this argument should not be set either.
72+
* `security_groups` - (Optional) ID of security group. If both vpc_id and subnet_id are not set, this argument should not be set either.
7373
* `subnet_id` - (Optional, ForceNew) ID of subnet.
7474
* `tags` - (Optional) The available tags within this postgresql.
7575
* `vpc_id` - (Optional, ForceNew) ID of VPC.

0 commit comments

Comments
 (0)