Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: UpdateMany() logic to properly delete and update resources #290

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions assets/terraform/internal/manager/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (o *EntryObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L
return found, true
}

for _, elt := range planEntries {
for idx, elt := range planEntries {
eltEntryName := elt.EntryName()
var processedEntry *entryObjectWithState[E]

Expand Down Expand Up @@ -257,11 +257,13 @@ func (o *EntryObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L
processedStateEntriesByName[processedEntry.Entry.EntryName()] = *processedEntry
} else {
processedEntry = &entryObjectWithState[E]{
Entry: elt,
State: entryMissing,
Entry: elt,
StateIdx: idx,
}

if !o.matcher(elt, stateElt.Entry) {
if o.matcher(elt, stateElt.Entry) {
processedEntry.State = entryOk
} else {
processedEntry.State = entryOutdated
}
processedStateEntriesByName[elt.EntryName()] = *processedEntry
Expand All @@ -273,6 +275,13 @@ func (o *EntryObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L
return nil, &Error{err: err, message: "failed to get a list of existing entries from the server"}
}

for name, elt := range stateEntriesByName {
if _, processedEntryFound := processedStateEntriesByName[name]; !processedEntryFound {
elt.State = entryDeleted
processedStateEntriesByName[name] = elt
}
}

updates := xmlapi.NewMultiConfig(len(planEntries))

for _, existingEntry := range existing {
Expand Down Expand Up @@ -353,6 +362,12 @@ func (o *EntryObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L
delete(processedStateEntriesByName, elt.Entry.EntryName())
elt.Entry.SetEntryName(elt.NewName)
processedStateEntriesByName[elt.NewName] = elt
case entryDeleted:
updates.Add(&xmlapi.Config{
Action: "delete",
Xpath: util.AsXpath(path),
Target: o.client.GetTarget(),
})
case entryUnknown:
slog.Warn("Entry state is still unknown after reconciliation", "Name", elt.Entry.EntryName())
case entryOk:
Expand All @@ -367,9 +382,16 @@ func (o *EntryObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L
}
}

entries := make([]E, len(processedStateEntriesByName))
for _, elt := range processedStateEntriesByName {
entries[elt.StateIdx] = elt.Entry
existing, err = o.service.List(ctx, location, "get", "", "")
if err != nil {
return nil, fmt.Errorf("Failed to list remote entries: %w", err)
}

entries := make([]E, len(planEntries))
for _, elt := range existing {
if planEntry, found := planEntriesByName[elt.EntryName()]; found {
entries[planEntry.StateIdx] = elt
}
}

return entries, nil
Expand Down
20 changes: 15 additions & 5 deletions assets/terraform/internal/manager/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,27 @@ var _ = Describe("Entry", func() {
Context("UpdateMany()", func() {
Context("when entries from the plan are missing from the server", func() {
It("should recreate them, and return back list of all managed entries", func() {
entries := []*MockEntryObject{{Name: "4", Value: "D"}}
processed, err := sdk.UpdateMany(ctx, location, entries, entries)
expected := append(existing, &MockEntryObject{Name: "4", Value: "D"})
processed, err := sdk.UpdateMany(ctx, location, existing, expected)

Expect(err).ToNot(HaveOccurred())
Expect(processed).To(HaveExactElements(entries))

expected := append(existing, entries...)
Expect(processed).To(HaveExactElements(expected))
Expect(client.list()).To(HaveExactElements(expected))
})

})

Context("when some entries are removed from the plan", func() {
It("should properly remove deleted entries from the server and return back updated list", func() {
stateEntries := []*MockEntryObject{{Name: "1", Value: "A"}, {Name: "2", Value: "B"}, {Name: "3", Value: "C"}}
planEntries := []*MockEntryObject{{Name: "1", Value: "A"}, {Name: "3", Value: "C"}}
processed, err := sdk.UpdateMany(ctx, location, stateEntries, planEntries)

Expect(err).ToNot(HaveOccurred())
Expect(processed).To(HaveExactElements(planEntries))
Expect(client.list()).To(HaveExactElements(planEntries))
})
})
})

Context("Delete()", func() {
Expand Down
Loading