Skip to content

Commit d5bc34d

Browse files
authored
fix: pgsql - add security group (#1414)
* fix: pgsql - add security group * fix: pg - testcase ref sg_id * fix: pgsql - network status error
1 parent 912bffd commit d5bc34d

File tree

4 files changed

+73
-28
lines changed

4 files changed

+73
-28
lines changed

tencentcloud/commom_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ func TestIsContains(t *testing.T) {
7777
}
7878
}
7979

80+
func TestMatchAny(t *testing.T) {
81+
assert.False(t, MatchAny(1))
82+
assert.True(t, MatchAny(1, 1, 2, 3))
83+
assert.False(t, MatchAny(1, 4, 5, 6))
84+
assert.True(t, MatchAny("a", "b", "c", "a", "aa"))
85+
86+
one := 1
87+
two := 2
88+
var ptrOne *int
89+
var nilVal *string
90+
91+
ptrOne = &one
92+
assert.True(t, MatchAny(ptrOne, &one, two))
93+
94+
assert.False(t, MatchAny(ptrOne, 5, 6, 7))
95+
assert.False(t, MatchAny(nilVal, nil))
96+
97+
var oneI64 int64 = 1
98+
var oneUI64 uint64 = 1
99+
100+
assert.False(t, MatchAny(oneI64, 2, 1, 3))
101+
assert.False(t, MatchAny(oneUI64, 1))
102+
}
103+
80104
func TestGetListIncrement(t *testing.T) {
81105
var (
82106
old1 = []int{1, 2, 2, 3, 5}

tencentcloud/common.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,23 @@ func IsContains(array interface{}, value interface{}) bool {
299299
}
300300
}
301301

302+
func MatchAny(value interface{}, matches ...interface{}) bool {
303+
rVal := reflect.ValueOf(value)
304+
kind := rVal.Kind()
305+
if kind == reflect.Ptr || kind == reflect.Interface {
306+
if rVal.IsNil() {
307+
return false
308+
}
309+
}
310+
for i := range matches {
311+
match := matches[i]
312+
if reflect.DeepEqual(value, match) {
313+
return true
314+
}
315+
}
316+
return false
317+
}
318+
302319
func FindIntListIndex(list []int, elem int) int {
303320
for i, v := range list {
304321
if v == elem {

tencentcloud/resource_tc_postgresql_instance_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func TestAccTencentCloudPostgresqlInstanceResource(t *testing.T) {
168168
})
169169
}
170170

171-
func TestAccTencentCloudPostgresqlMAZInstanceResource(t *testing.T) {
171+
func TestAccTencentCloudPostgresqlInstanceResource_MAZ(t *testing.T) {
172172
t.Parallel()
173173
resource.Test(t, resource.TestCase{
174174
PreCheck: func() { testAccPreCheck(t) },
@@ -255,7 +255,7 @@ func testAccCheckPostgresqlInstanceExists(n string) resource.TestCheckFunc {
255255
}
256256
}
257257

258-
const testAccPostgresqlInstanceBasic = `
258+
const testAccPostgresqlInstanceBasic = defaultSecurityGroupData + `
259259
data "tencentcloud_availability_zones_by_product" "zone" {
260260
product = "postgres"
261261
}
@@ -270,6 +270,7 @@ resource "tencentcloud_postgresql_instance" "test" {
270270
subnet_id = local.subnet_id
271271
engine_version = "10.4"
272272
root_password = "t1qaA2k1wgvfa3?ZZZ"
273+
security_groups = [local.sg_id]
273274
charset = "LATIN1"
274275
project_id = 0
275276
memory = 4
@@ -300,6 +301,7 @@ resource "tencentcloud_postgresql_instance" "test" {
300301
charset = "LATIN1"
301302
project_id = 0
302303
public_access_switch = true
304+
security_groups = [local.sg_id]
303305
memory = 4
304306
storage = 250
305307
backup_plan {
@@ -327,6 +329,7 @@ resource "tencentcloud_postgresql_instance" "test" {
327329
charset = "LATIN1"
328330
project_id = 0
329331
public_access_switch = false
332+
security_groups = [local.sg_id]
330333
memory = 4
331334
storage = 250
332335
backup_plan {

tencentcloud/service_tencentcloud_postgresql.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -277,21 +277,21 @@ func (me *PostgresqlService) ModifyPublicService(ctx context.Context, openIntern
277277
return resource.NonRetryableError(fmt.Errorf("illegal net info of postgresql instance %s", instanceId))
278278
}
279279
for _, v := range instance.DBInstanceNetInfo {
280-
if *v.NetType == "public" {
281-
if *v.Status == "opened" || *v.Status == "1" {
282-
return nil
283-
}
284-
if *v.Status == "opening" || *v.Status == "initing" || *v.Status == "3" || *v.Status == "0" {
285-
startProgressRetries = 0
286-
return resource.RetryableError(fmt.Errorf("status %s, postgresql instance %s waiting", *v.Status, instanceId))
287-
}
288-
if startProgressRetries > 0 && *v.Status == "closed" {
289-
startProgressRetries -= 1
290-
return resource.RetryableError(fmt.Errorf("status still closed, retry remaining count: %d", startProgressRetries))
291-
}
292-
return resource.NonRetryableError(fmt.Errorf("status %s, postgresql instance %s open public service fail", *v.Status, instanceId))
293-
280+
if *v.NetType != "public" {
281+
continue
282+
}
283+
if MatchAny(*v.Status, "opened", "2") {
284+
return nil
285+
}
286+
if MatchAny(*v.Status, "opening", "4") {
287+
startProgressRetries = 0
288+
return resource.RetryableError(fmt.Errorf("status %s, postgresql instance %s waiting", *v.Status, instanceId))
289+
}
290+
if startProgressRetries > 0 && MatchAny(*v.Status, "closed", "initing") {
291+
startProgressRetries -= 1
292+
return resource.RetryableError(fmt.Errorf("status still closed, retry remaining count: %d", startProgressRetries))
294293
}
294+
return resource.NonRetryableError(fmt.Errorf("status %s, postgresql instance %s open public service fail", *v.Status, instanceId))
295295
}
296296
// there is no public service yet
297297
return resource.RetryableError(fmt.Errorf("cannot find public status, postgresql instance %s watiting", instanceId))
@@ -335,19 +335,20 @@ func (me *PostgresqlService) ModifyPublicService(ctx context.Context, openIntern
335335
return resource.NonRetryableError(fmt.Errorf("illegal net info of postgresql instance %s", instanceId))
336336
}
337337
for _, v := range instance.DBInstanceNetInfo {
338-
if *v.NetType == "public" {
339-
if *v.Status == "closed" || *v.Status == "2" {
340-
return nil
341-
}
342-
if *v.Status == "closing" || *v.Status == "4" {
343-
return resource.RetryableError(fmt.Errorf("status %s, postgresql instance %s waiting", *v.Status, instanceId))
344-
}
345-
if startProgressRetries > 0 && *v.Status == "opened" {
346-
startProgressRetries -= 1
347-
return resource.RetryableError(fmt.Errorf("status still opened, retry remaining count: %d", startProgressRetries))
348-
}
349-
return resource.NonRetryableError(fmt.Errorf("status %s, postgresql instance %s open public service fail", *v.Status, instanceId))
338+
if *v.NetType != "public" {
339+
continue
340+
}
341+
if MatchAny(*v.Status, "closed", "3", "initing", "1") {
342+
return nil
343+
}
344+
if MatchAny(*v.Status, "closing", "4") {
345+
return resource.RetryableError(fmt.Errorf("status %s, postgresql instance %s waiting", *v.Status, instanceId))
346+
}
347+
if startProgressRetries > 0 && *v.Status == "opened" {
348+
startProgressRetries -= 1
349+
return resource.RetryableError(fmt.Errorf("status still opened, retry remaining count: %d", startProgressRetries))
350350
}
351+
return resource.NonRetryableError(fmt.Errorf("status %s, postgresql instance %s open public service fail", *v.Status, instanceId))
351352
}
352353
return nil
353354
})

0 commit comments

Comments
 (0)