Skip to content

Commit 4799b38

Browse files
authored
add logic to return successfully on deletion if database instance was already deleted (#453)
1 parent 67ddf97 commit 4799b38

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

services/rds/rds.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,7 @@ func (d *dedicatedDBAdapter) waitForDbDeleted(operation base.Operation, i *RDSIn
553553
for !isDeleted && attempt <= int(d.settings.PollAwsMaxRetries) {
554554
dbState, err = d.checkDBStatus(database)
555555
if err != nil {
556-
var exception *rdsTypes.DBInstanceNotFoundFault
557-
if !errors.As(err, &exception) {
556+
if !isDatabaseInstanceNotFoundError(err) {
558557
updateErr := jobs.WriteAsyncJobMessage(d.db, i.ServiceID, i.Uuid, operation, base.InstanceNotGone, fmt.Sprintf("Could not check database status: %s", err))
559558
if updateErr != nil {
560559
err = fmt.Errorf("waitForDbDeleted: while handling error %w, error updating async job message %w", err, updateErr)
@@ -590,7 +589,12 @@ func (d *dedicatedDBAdapter) deleteDatabaseInstance(i *RDSInstance, operation ba
590589
params := prepareDeleteDbInput(database)
591590
_, err := d.rds.DeleteDBInstance(context.TODO(), params)
592591
if err != nil {
593-
return fmt.Errorf("deleteDatabaseInstance: %w", err)
592+
if isDatabaseInstanceNotFoundError(err) {
593+
d.logger.Debug(fmt.Sprintf("database %s was already deleted, continuing", database))
594+
return nil
595+
} else {
596+
return fmt.Errorf("deleteDatabaseInstance: %w", err)
597+
}
594598
}
595599

596600
err = d.waitForDbDeleted(operation, i, database)
@@ -666,3 +670,8 @@ func prepareDeleteDbInput(database string) *rds.DeleteDBInstanceInput {
666670
SkipFinalSnapshot: aws.Bool(true),
667671
}
668672
}
673+
674+
func isDatabaseInstanceNotFoundError(err error) bool {
675+
var notFoundException *rdsTypes.DBInstanceNotFoundFault
676+
return errors.As(err, &notFoundException)
677+
}

services/rds/rds_test.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"github.com/google/uuid"
2525
)
2626

27-
var logger lager.Logger
28-
2927
func NewTestDedicatedDBAdapter(s *config.Settings, db *gorm.DB, rdsClient RDSClientInterface, parameterGroupClient parameterGroupClient) *dedicatedDBAdapter {
3028
logger := lager.NewLogger("aws-rds-test")
3129
logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.INFO))
@@ -1901,7 +1899,7 @@ func TestAsyncDeleteDB(t *testing.T) {
19011899
}
19021900

19031901
dbInstanceNotFoundErr := &rdsTypes.DBInstanceNotFoundFault{
1904-
Message: aws.String("operation failed"),
1902+
Message: aws.String("not found"),
19051903
}
19061904

19071905
testCases := map[string]struct {
@@ -2055,6 +2053,53 @@ func TestAsyncDeleteDB(t *testing.T) {
20552053
expectedState: base.InstanceInProgress,
20562054
expectedRecordCount: 1,
20572055
},
2056+
"database already deleted": {
2057+
dbAdapter: NewTestDedicatedDBAdapter(
2058+
&config.Settings{
2059+
PollAwsRetryDelaySeconds: 0,
2060+
PollAwsMaxRetries: 1,
2061+
},
2062+
brokerDB,
2063+
&mockRDSClient{
2064+
deleteDbInstancesErrs: []error{dbInstanceNotFoundErr},
2065+
},
2066+
&mockParameterGroupClient{},
2067+
),
2068+
dbInstance: &RDSInstance{
2069+
Instance: base.Instance{
2070+
Request: request.Request{
2071+
ServiceID: helpers.RandStr(10),
2072+
},
2073+
Uuid: helpers.RandStr(10),
2074+
},
2075+
Database: helpers.RandStr(10),
2076+
},
2077+
expectedState: base.InstanceGone,
2078+
},
2079+
"replica and database already deleted": {
2080+
dbAdapter: NewTestDedicatedDBAdapter(
2081+
&config.Settings{
2082+
PollAwsRetryDelaySeconds: 0,
2083+
PollAwsMaxRetries: 1,
2084+
},
2085+
brokerDB,
2086+
&mockRDSClient{
2087+
deleteDbInstancesErrs: []error{dbInstanceNotFoundErr, dbInstanceNotFoundErr},
2088+
},
2089+
&mockParameterGroupClient{},
2090+
),
2091+
dbInstance: &RDSInstance{
2092+
Instance: base.Instance{
2093+
Request: request.Request{
2094+
ServiceID: helpers.RandStr(10),
2095+
},
2096+
Uuid: helpers.RandStr(10),
2097+
},
2098+
Database: helpers.RandStr(10),
2099+
ReplicaDatabase: helpers.RandStr(10),
2100+
},
2101+
expectedState: base.InstanceGone,
2102+
},
20582103
}
20592104

20602105
for name, test := range testCases {

0 commit comments

Comments
 (0)