Skip to content

Commit 451814e

Browse files
authored
Merge pull request #90 from xiaozhu36/master
fix several bugs about db result from its status and id not found
2 parents c692b78 + c8e1a95 commit 451814e

15 files changed

+214
-114
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
## 1.6.2 (Unreleased)
1+
## 1.6.2 (January 22, 2018)
22

33
IMPROVEMENTS:
44

5-
- Support to set instnace name for RDS ([#89](https://github.com/terraform-providers/terraform-provider-alicloud/pull/89))
5+
- Modify db_connection prefix default value to "instance_id + 'tf'"([#90](https://github.com/terraform-providers/terraform-provider-alicloud/pull/90))
6+
- Modify db_connection ID to make it more simple while importing it([#90](https://github.com/terraform-providers/terraform-provider-alicloud/pull/90))
7+
- Add wait method to avoid useless status error while creating/modifying account or privilege or connection or database([#90](https://github.com/terraform-providers/terraform-provider-alicloud/pull/90))
8+
- Support to set instnace name for RDS ([#88](https://github.com/terraform-providers/terraform-provider-alicloud/pull/88))
69
- Avoid container cluster cidr block conflicts with vswitch's ([#88](https://github.com/terraform-providers/terraform-provider-alicloud/pull/88))
710
- Output resource import information ([#87](https://github.com/terraform-providers/terraform-provider-alicloud/pull/87))
811

912
BUG FIXES:
1013

14+
- fix instance id not found and instane status not supported bug([#90](https://github.com/terraform-providers/terraform-provider-alicloud/pull/90))
1115
- fix deleting slb_attachment resource failed bug ([#86](https://github.com/terraform-providers/terraform-provider-alicloud/pull/86))
1216

1317

alicloud/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,5 @@ func userDataHashSum(user_data string) string {
157157
}
158158
return string(v)
159159
}
160+
161+
const DBConnectionSuffix = ".mysql.rds.aliyuncs.com"

alicloud/resource_alicloud_db_account.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func resourceAlicloudDBAccount() *schema.Resource {
5555
}
5656

5757
func resourceAlicloudDBAccountCreate(d *schema.ResourceData, meta interface{}) error {
58-
58+
conn := meta.(*AliyunClient).rdsconn
5959
args := rds.CreateAccountArgs{
6060
DBInstanceId: d.Get("instance_id").(string),
6161
AccountName: d.Get("name").(string),
@@ -65,9 +65,13 @@ func resourceAlicloudDBAccountCreate(d *schema.ResourceData, meta interface{}) e
6565
if v, ok := d.GetOk("description"); ok && v.(string) != "" {
6666
args.AccountDescription = v.(string)
6767
}
68+
// wait instance running before modifying
69+
if err := conn.WaitForInstanceAsyn(args.DBInstanceId, rds.Running, 500); err != nil {
70+
return fmt.Errorf("WaitForInstance %s got error: %#v", rds.Running, err)
71+
}
6872
err := resource.Retry(3*time.Minute, func() *resource.RetryError {
6973
ag := args
70-
if _, err := meta.(*AliyunClient).rdsconn.CreateAccount(&ag); err != nil {
74+
if _, err := conn.CreateAccount(&ag); err != nil {
7175
if IsExceptedError(err, InvalidAccountNameDuplicate) {
7276
return resource.NonRetryableError(fmt.Errorf("The account %s has already existed. Please import it using ID '%s:%s' or specify a new 'name' and try again.",
7377
args.AccountName, args.DBInstanceId, args.AccountName))
@@ -86,7 +90,7 @@ func resourceAlicloudDBAccountCreate(d *schema.ResourceData, meta interface{}) e
8690

8791
d.SetId(fmt.Sprintf("%s%s%s", args.DBInstanceId, COLON_SEPARATED, args.AccountName))
8892

89-
if err := meta.(*AliyunClient).rdsconn.WaitForAccount(args.DBInstanceId, args.AccountName, rds.Available, defaultTimeout); err != nil {
93+
if err := meta.(*AliyunClient).rdsconn.WaitForAccount(args.DBInstanceId, args.AccountName, rds.Available, 500); err != nil {
9094
return fmt.Errorf("Wait db account %s got an error: %#v.", rds.Available, err)
9195
}
9296

@@ -98,7 +102,7 @@ func resourceAlicloudDBAccountRead(d *schema.ResourceData, meta interface{}) err
98102
parts := strings.Split(d.Id(), COLON_SEPARATED)
99103
account, err := meta.(*AliyunClient).DescribeDatabaseAccount(parts[0], parts[1])
100104
if err != nil {
101-
if NotFoundError(err) {
105+
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceIdNotFound) || IsExceptedError(err, InvalidAccountNameNotFound) {
102106
d.SetId("")
103107
return nil
104108
}
@@ -156,7 +160,7 @@ func resourceAlicloudDBAccountDelete(d *schema.ResourceData, meta interface{}) e
156160

157161
resp, err := meta.(*AliyunClient).DescribeDatabaseAccount(parts[0], parts[1])
158162
if err != nil {
159-
if NotFoundError(err) || IsExceptedError(err, InvalidAccountNameNotFound) {
163+
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceIdNotFound) || IsExceptedError(err, InvalidAccountNameNotFound) {
160164
return nil
161165
}
162166
return resource.NonRetryableError(err)

alicloud/resource_alicloud_db_account_privilege.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,37 @@ func resourceAlicloudDBAccountPrivilege() *schema.Resource {
3838
Optional: true,
3939
ValidateFunc: validateAllowedStringValue([]string{string(rds.ReadOnly), string(rds.ReadWrite)}),
4040
Default: rds.ReadOnly,
41+
ForceNew: true,
4142
},
4243

4344
"db_names": &schema.Schema{
4445
Type: schema.TypeSet,
4546
Required: true,
4647
Elem: &schema.Schema{Type: schema.TypeString},
48+
MinItems: 1,
4749
},
4850
},
4951
}
5052
}
5153

5254
func resourceAlicloudDBAccountPrivilegeCreate(d *schema.ResourceData, meta interface{}) error {
55+
instanceId := d.Get("instance_id").(string)
56+
account := d.Get("account_name").(string)
57+
privilege := d.Get("privilege").(string)
58+
dbList := d.Get("db_names").(*schema.Set).List()
59+
// wait instance running before granting
60+
if err := meta.(*AliyunClient).rdsconn.WaitForInstanceAsyn(instanceId, rds.Running, 500); err != nil {
61+
return fmt.Errorf("WaitForInstance %s got error: %#v", rds.Running, err)
62+
}
63+
if len(dbList) > 0 {
64+
for _, db := range dbList {
65+
if err := meta.(*AliyunClient).GrantAccountPrivilege(instanceId, account, db.(string), privilege); err != nil {
66+
return fmt.Errorf("Grant Account %s Privilege %s got an error: %#v", account, privilege, err)
67+
}
68+
}
69+
}
5370

54-
d.SetId(fmt.Sprintf("%s%s%s%s%s", d.Get("instance_id").(string), COLON_SEPARATED, d.Get("account_name").(string), COLON_SEPARATED, d.Get("privilege").(string)))
71+
d.SetId(fmt.Sprintf("%s%s%s%s%s", instanceId, COLON_SEPARATED, account, COLON_SEPARATED, privilege))
5572

5673
return resourceAlicloudDBAccountPrivilegeUpdate(d, meta)
5774
}
@@ -61,7 +78,7 @@ func resourceAlicloudDBAccountPrivilegeRead(d *schema.ResourceData, meta interfa
6178
parts := strings.Split(d.Id(), COLON_SEPARATED)
6279
account, err := meta.(*AliyunClient).DescribeDatabaseAccount(parts[0], parts[1])
6380
if err != nil {
64-
if NotFoundError(err) {
81+
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceIdNotFound) || IsExceptedError(err, InvalidAccountNameNotFound) {
6582
d.SetId("")
6683
return nil
6784
}
@@ -85,28 +102,21 @@ func resourceAlicloudDBAccountPrivilegeRead(d *schema.ResourceData, meta interfa
85102
func resourceAlicloudDBAccountPrivilegeUpdate(d *schema.ResourceData, meta interface{}) error {
86103
client := meta.(*AliyunClient)
87104
d.Partial(true)
88-
parts := strings.Split(d.Id(), COLON_SEPARATED)
89-
90-
update := false
91105

92-
if d.HasChange("privilege") {
93-
update = true
94-
d.SetPartial("privilege")
95-
}
106+
if d.HasChange("db_names") && !d.IsNewResource() {
107+
parts := strings.Split(d.Id(), COLON_SEPARATED)
96108

97-
if d.HasChange("db_names") {
98-
update = true
99-
d.SetPartial("db_names")
100-
}
101-
102-
if update {
103109
o, n := d.GetChange("db_names")
104110
os := o.(*schema.Set)
105111
ns := n.(*schema.Set)
106112
remove := os.Difference(ns).List()
107113
add := ns.Difference(os).List()
108114

109115
if len(remove) > 0 {
116+
// wait instance running before revoking
117+
if err := client.rdsconn.WaitForInstanceAsyn(parts[0], rds.Running, 500); err != nil {
118+
return fmt.Errorf("WaitForInstance %s got error: %#v", rds.Running, err)
119+
}
110120
for _, db := range remove {
111121
if err := client.RevokeAccountPrivilege(parts[0], parts[1], db.(string)); err != nil {
112122
return err
@@ -115,12 +125,17 @@ func resourceAlicloudDBAccountPrivilegeUpdate(d *schema.ResourceData, meta inter
115125
}
116126

117127
if len(add) > 0 {
128+
// wait instance running before granting
129+
if err := client.rdsconn.WaitForInstanceAsyn(parts[0], rds.Running, 500); err != nil {
130+
return fmt.Errorf("WaitForInstance %s got error: %#v", rds.Running, err)
131+
}
118132
for _, db := range add {
119133
if err := client.GrantAccountPrivilege(parts[0], parts[1], db.(string), parts[2]); err != nil {
120134
return err
121135
}
122136
}
123137
}
138+
d.SetPartial("db_names")
124139
}
125140

126141
d.Partial(false)
@@ -133,7 +148,7 @@ func resourceAlicloudDBAccountPrivilegeDelete(d *schema.ResourceData, meta inter
133148

134149
account, err := client.DescribeDatabaseAccount(parts[0], parts[1])
135150
if err != nil {
136-
if NotFoundError(err) || IsExceptedError(err, InvalidAccountNameNotFound) {
151+
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceIdNotFound) || IsExceptedError(err, InvalidAccountNameNotFound) {
137152
return nil
138153
}
139154
return fmt.Errorf("Describe db account got an error: %#v", err)
@@ -151,7 +166,7 @@ func resourceAlicloudDBAccountPrivilegeDelete(d *schema.ResourceData, meta inter
151166
}
152167
account, err := client.DescribeDatabaseAccount(parts[0], parts[1])
153168
if err != nil {
154-
if NotFoundError(err) || IsExceptedError(err, InvalidAccountNameNotFound) {
169+
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceIdNotFound) || IsExceptedError(err, InvalidAccountNameNotFound) {
155170
return nil
156171
}
157172
return resource.NonRetryableError(fmt.Errorf("Describe db account got an error: %#v", err))

alicloud/resource_alicloud_db_account_privilege_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func testAccCheckDBAccountPrivilegeDestroy(s *terraform.State) error {
8181

8282
// Verify the error is what we want
8383
if err != nil {
84-
if NotFoundError(err) || IsExceptedError(err, InvalidAccountNameNotFound) {
85-
continue
84+
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceIdNotFound) || IsExceptedError(err, InvalidAccountNameNotFound) {
85+
return nil
8686
}
8787
return err
8888
}

alicloud/resource_alicloud_db_account_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestAccAlicloudDBAccount_basic(t *testing.T) {
2929
Check: resource.ComposeTestCheckFunc(
3030
testAccCheckDBAccountExists(
3131
"alicloud_db_account.account", &account),
32-
resource.TestCheckResourceAttr("alicloud_db_instance.account", "name", "tf_db"),
32+
resource.TestCheckResourceAttr("alicloud_db_account.account", "name", "tf_db"),
3333
),
3434
},
3535
},
@@ -79,8 +79,8 @@ func testAccCheckDBAccountDestroy(s *terraform.State) error {
7979

8080
// Verify the error is what we want
8181
if err != nil {
82-
if NotFoundError(err) || IsExceptedError(err, InvalidAccountNameNotFound) {
83-
continue
82+
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceIdNotFound) || IsExceptedError(err, InvalidAccountNameNotFound) {
83+
return nil
8484
}
8585
return err
8686
}

alicloud/resource_alicloud_db_backup_policy.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func resourceAlicloudDBBackupPolicyRead(d *schema.ResourceData, meta interface{}
8181
DBInstanceId: d.Id(),
8282
})
8383
if err != nil {
84-
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceNameNotFound) {
84+
if IsExceptedError(err, InvalidDBInstanceIdNotFound) || IsExceptedError(err, InvalidDBInstanceNameNotFound) {
8585
d.SetId("")
8686
return nil
8787
}
@@ -101,7 +101,7 @@ func resourceAlicloudDBBackupPolicyRead(d *schema.ResourceData, meta interface{}
101101
func resourceAlicloudDBBackupPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
102102

103103
d.Partial(true)
104-
104+
conn := meta.(*AliyunClient).rdsconn
105105
update := false
106106
args := rds.ModifyBackupPolicyArgs{
107107
DBInstanceId: d.Id(),
@@ -145,19 +145,21 @@ func resourceAlicloudDBBackupPolicyUpdate(d *schema.ResourceData, meta interface
145145
}
146146

147147
if update {
148-
err := resource.Retry(3*time.Minute, func() *resource.RetryError {
148+
// wait instance running before modifying
149+
if err := conn.WaitForInstanceAsyn(args.DBInstanceId, rds.Running, 500); err != nil {
150+
return fmt.Errorf("WaitForInstance %s got error: %#v", rds.Running, err)
151+
}
152+
if err := resource.Retry(3*time.Minute, func() *resource.RetryError {
149153
ag := args
150-
if _, err := meta.(*AliyunClient).rdsconn.ModifyBackupPolicy(&ag); err != nil {
151-
if IsExceptedError(err, OperationDeniedDBInstanceStatus) {
154+
if _, err := conn.ModifyBackupPolicy(&ag); err != nil {
155+
if IsExceptedError(err, OperationDeniedDBInstanceStatus) || IsExceptedError(err, DBInternalError) {
152156
return resource.RetryableError(fmt.Errorf("ModifyBackupPolicy got an error: %#v.", err))
153157
}
154158
return resource.NonRetryableError(fmt.Errorf("ModifyBackupPolicy got an error: %#v.", err))
155159
}
156160

157161
return nil
158-
})
159-
160-
if err != nil {
162+
}); err != nil {
161163
return err
162164
}
163165
}

alicloud/resource_alicloud_db_backup_policy_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func testAccCheckDBBackupPolicyDestroy(s *terraform.State) error {
7878
DBInstanceId: rs.Primary.ID,
7979
})
8080
if err != nil {
81-
if NotFoundError(err) || IsExceptedError(err, InvalidDBInstanceNameNotFound) {
82-
continue
81+
if IsExceptedError(err, InvalidDBInstanceIdNotFound) || IsExceptedError(err, InvalidDBInstanceNameNotFound) {
82+
return nil
8383
}
8484
return fmt.Errorf("Error Describe DB backup policy: %#v", err)
8585
}

0 commit comments

Comments
 (0)