Skip to content

Commit 92c16e7

Browse files
committed
Stop throwing an error if host or group is not found when trying to remove a host or group. We still achieve the goal and can continue.
1 parent c401d8b commit 92c16e7

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

internal/ansible/resource_ansible_group.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,12 @@ func ansibleGroupResourceQueryDelete(d *schema.ResourceData, meta interface{}) e
177177
id := d.Id()
178178
g := db.Group(id)
179179
if g == nil {
180-
return fmt.Errorf("unable to find group with id '%s'", d.Id())
181-
}
182-
183-
if err := db.RemoveGroup(*g); err != nil {
184-
return fmt.Errorf("unable to delete group '%s': %e", g.GetName(), err)
180+
logger.Error().Err(err).Msg("cannot find group so unable to remove, but continuing anyway")
181+
} else {
182+
// if we find the group we remove it, if we can't find it we can skip removing it
183+
if err := db.RemoveGroup(*g); err != nil {
184+
return fmt.Errorf("unable to delete group '%s': %e", g.GetName(), err)
185+
}
185186
}
186187

187188
// Save and export database

internal/ansible/resource_ansible_host.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,19 @@ func ansibleHostResourceQueryDelete(d *schema.ResourceData, meta interface{}) er
211211
id := d.Id()
212212
g, entry, err := db.FindEntryById(id)
213213
if err != nil {
214-
return fmt.Errorf("unable to find entry with id '%s': %e", id, err)
215-
}
214+
logger.Error().Err(err).Msg("cannot find host so unable to remove, but continuing anyway")
215+
} else {
216+
// only remove host from group if we actually find it there. if we dont find it, then everything is ok and we
217+
// can skip the removing it.
216218

217-
// remove entry from group
218-
if err := g.RemoveEntity(*entry); err != nil {
219-
return fmt.Errorf("unable to remove entry from group with id: %e", err)
220-
}
219+
// remove entry from group
220+
if err := g.RemoveEntity(*entry); err != nil {
221+
return fmt.Errorf("unable to remove entry from group with id: %e", err)
222+
}
221223

222-
// update group
223-
db.UpdateGroup(*g)
224+
// update group
225+
db.UpdateGroup(*g)
226+
}
224227

225228
// Save and export database
226229
if err := commitAndExport(db, i.GetDatabasePath()); err != nil {

0 commit comments

Comments
 (0)