fix: instance remove (#4602)

This commit is contained in:
Livio Spring
2022-10-26 15:06:48 +02:00
committed by GitHub
parent 001636f2b4
commit d721f725fd
89 changed files with 656 additions and 122 deletions

View File

@@ -180,3 +180,20 @@ func (c *crdbStorage) RemoveObjects(ctx context.Context, instanceID, resourceOwn
}
return nil
}
func (c *crdbStorage) RemoveInstanceObjects(ctx context.Context, instanceID string) error {
stmt, args, err := squirrel.Delete(assetsTable).
Where(squirrel.Eq{
AssetColInstanceID: instanceID,
}).
PlaceholderFormat(squirrel.Dollar).
ToSql()
if err != nil {
return caos_errors.ThrowInternal(err, "DATAB-Sfgeq", "Errors.Internal")
}
_, err = c.client.ExecContext(ctx, stmt, args...)
if err != nil {
return caos_errors.ThrowInternal(err, "DATAB-Efgt2", "Errors.Assets.Object.RemoveFailed")
}
return nil
}

View File

@@ -35,6 +35,8 @@ const (
" WHERE asset_type = $1" +
" AND instance_id = $2" +
" AND resource_owner = $3"
removeInstanceObjectsStmt = "DELETE FROM system.assets" +
" WHERE instance_id = $1"
)
func Test_crdbStorage_CreateObject(t *testing.T) {
@@ -224,6 +226,50 @@ func Test_crdbStorage_RemoveObjects(t *testing.T) {
})
}
}
func Test_crdbStorage_RemoveInstanceObjects(t *testing.T) {
type fields struct {
client db
}
type args struct {
ctx context.Context
instanceID string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
"remove ok",
fields{
client: prepareDB(t,
expectExec(
removeInstanceObjectsStmt,
nil,
"instanceID",
)),
},
args{
ctx: context.Background(),
instanceID: "instanceID",
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &crdbStorage{
client: tt.fields.client.db,
}
err := c.RemoveInstanceObjects(tt.args.ctx, tt.args.instanceID)
if (err != nil) != tt.wantErr {
t.Errorf("RemoveInstanceObjects() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
type db struct {
mock sqlmock.Sqlmock