Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 37 additions & 9 deletions assets/terraform/internal/manager/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,22 +253,50 @@ func (o *EntryObjectManager[E, T, S]) Delete(ctx context.Context, location T, pa
return nil
}

func (o *EntryObjectManager[E, L, S]) Update(ctx context.Context, location L, components []string, entry E, name string) (E, error) {
xpath, err := location.XpathWithComponents(o.client.Versioning(), append(components, util.AsEntryXpath(name))...)
func (o *EntryObjectManager[E, L, S]) Update(ctx context.Context, location L, components []string, entry E, newName string) (E, error) {
updates := xmlapi.NewMultiConfig(2)

var xpath, renamedXpath []string
var err error

spec, err := o.specifier(entry)
if err != nil {
return *new(E), err
}

err = o.service.UpdateWithXpath(ctx, util.AsXpath(xpath), entry, name)
if err != nil {
if sdkerrors.IsObjectNotFound(err) {
return *new(E), ErrObjectNotFound
} else {
return *new(E), &Error{err: err, message: "sdk error while updating"}
xpath, err = location.XpathWithComponents(o.client.Versioning(), append(components, util.AsEntryXpath(entry.EntryName()))...)
updates.Add(&xmlapi.Config{
Action: "edit",
Xpath: util.AsXpath(xpath),
Element: spec,
Target: o.client.GetTarget(),
})

if newName != "" {
renamedXpath, err = location.XpathWithComponents(o.client.Versioning(), append(components, util.AsEntryXpath(newName))...)
_, err = o.service.ReadWithXpath(ctx, util.AsXpath(renamedXpath), "get")
if err == nil {
return *new(E), &Error{err: ErrConflict, message: fmt.Sprintf("entry '%s' already exists", newName)}
}

updates.Add(&xmlapi.Config{
Action: "rename",
Xpath: util.AsXpath(xpath),
NewName: newName,
Target: o.client.GetTarget(),
})
}

_, _, _, err = o.client.MultiConfig(ctx, updates, false, nil)
if err != nil {
return *new(E), err
}

return o.service.ReadWithXpath(ctx, util.AsXpath(xpath), "get")
if renamedXpath != nil {
return o.service.ReadWithXpath(ctx, util.AsXpath(renamedXpath), "get")
} else {
return o.service.ReadWithXpath(ctx, util.AsXpath(xpath), "get")
}
}

func (o *EntryObjectManager[E, L, S]) UpdateMany(ctx context.Context, location L, components []string, stateEntries []E, planEntries []E) ([]E, error) {
Expand Down
43 changes: 43 additions & 0 deletions assets/terraform/internal/manager/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,50 @@ var _ = Describe("Entry", func() {
})

Context("Update()", func() {
BeforeEach(func() {
existing = []*MockEntryObject{{Name: "entry-to-rename", Value: "original"}}
})
Context("when just updating an entry", func() {
It("should update entry on the server without executing 'rename' command", func() {
updatedContent := &MockEntryObject{Name: "entry-to-rename", Value: "updated"}
updatedEntry, err := sdk.Update(ctx, location, []string{}, updatedContent, "")

Expect(err).ToNot(HaveOccurred())

Expect(updatedEntry).ToNot(BeNil())
Expect(updatedEntry.EntryName()).To(Equal("entry-to-rename"))
Expect(updatedEntry.Value).To(Equal("updated"))

Expect(client.MultiConfigOpers[0]).To(HaveExactElements([]MultiConfigOper{
{Operation: MultiConfigOperEdit, EntryName: "entry-to-rename"},
}))

expectedAfterUpdate := []*MockEntryObject{{Name: "entry-to-rename", Value: "updated"}}
Expect(client.list()).To(MatchEntries(expectedAfterUpdate))
})
})
Context("when renaming an entry", func() {

It("should rename the entry on the server", func() {
updatedContent := &MockEntryObject{Name: "entry-to-rename", Value: "updated"}
newName := "renamed-entry"
updatedEntry, err := sdk.Update(ctx, location, []string{}, updatedContent, newName)

Expect(err).ToNot(HaveOccurred())

Expect(updatedEntry).ToNot(BeNil())
Expect(updatedEntry.EntryName()).To(Equal(newName))
Expect(updatedEntry.Value).To(Equal("updated"))

Expect(client.MultiConfigOpers[0]).To(HaveExactElements([]MultiConfigOper{
{Operation: MultiConfigOperEdit, EntryName: "entry-to-rename"},
{Operation: MultiConfigOperRename, EntryName: "entry-to-rename", NewName: "renamed-entry"},
}))

expectedAfterUpdate := []*MockEntryObject{{Name: newName, Value: "updated"}}
Expect(client.list()).To(MatchEntries(expectedAfterUpdate))
})
})
})

Context("UpdateMany()", func() {
Expand Down
11 changes: 6 additions & 5 deletions assets/terraform/internal/manager/entry_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (o *MockEntryObject) DeepCopy() any {
type MockEntryClient[E manager.UuidObject] struct {
Initial *list.List
Current *list.List
MultiConfigOpers []MultiConfigOper
MultiConfigOpers [][]MultiConfigOper
}

func NewMockEntryClient[E manager.UuidObject](initial []E) *MockEntryClient[E] {
Expand Down Expand Up @@ -110,7 +110,8 @@ func (o *MockEntryClient[E]) ChunkedMultiConfig(ctx context.Context, updates *xm
}

func (o *MockEntryClient[E]) MultiConfig(ctx context.Context, updates *xmlapi.MultiConfig, arg1 bool, arg2 url.Values) ([]byte, *http.Response, *xmlapi.MultiConfigResponse, error) {
o.MultiConfigOpers, _ = MultiConfig[E](updates, &o.Current, multiConfigEntry, 0)
opers, _ := MultiConfig[E](updates, &o.Current, multiConfigEntry, 0)
o.MultiConfigOpers = append(o.MultiConfigOpers, opers)

return nil, nil, nil, nil
}
Expand All @@ -136,12 +137,12 @@ func (o *MockEntryService[E, L]) CreateWithXpath(ctx context.Context, xpath stri
}

func (o *MockEntryService[E, L]) Create(ctx context.Context, location L, entry E) (E, error) {
o.client.Initial.PushBack(entry)
o.client.Current.PushBack(entry)

return entry, nil
}

func (o *MockEntryService[E, L]) UpdateWithXpath(ctx context.Context, xpath string, entry E, name string) error {
func (o *MockEntryService[E, L]) UpdateWithXpath(ctx context.Context, xpath string, entry E, renamedXpath string) error {
for e := o.client.Initial.Front(); e != nil; e = e.Next() {
eltEntry := e.Value.(E)
if entry.EntryName() == eltEntry.EntryName() {
Expand All @@ -166,7 +167,7 @@ func (o *MockEntryService[E, L]) ReadWithXpath(ctx context.Context, xpath string
}

func (o *MockEntryService[E, L]) Read(ctx context.Context, location L, name string, action string) (E, error) {
for e := o.client.Initial.Front(); e != nil; e = e.Next() {
for e := o.client.Current.Front(); e != nil; e = e.Next() {
entry := e.Value.(E)
if entry.EntryName() == name {
return entry, nil
Expand Down
17 changes: 17 additions & 0 deletions assets/terraform/internal/manager/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type MultiConfigOper struct {
Operation MultiConfigOperType
EntryName string
EntryUuid *string
NewName string
Value string
}

func entryNameFromXpath(xpath string) string {
Expand Down Expand Up @@ -133,6 +135,7 @@ func MultiConfig[E sdkmanager.UuidObject](updates *xmlapi.MultiConfig, existingP
operEntry := MultiConfigOper{
Operation: MultiConfigOperType(op),
EntryName: entryName,
NewName: oper.NewName,
}

slog.Debug("MultiConfig", "operEntry", operEntry)
Expand All @@ -146,6 +149,7 @@ func MultiConfig[E sdkmanager.UuidObject](updates *xmlapi.MultiConfig, existingP
entry.SetEntryUuid(existing.Entry.EntryUuid())
}
existing.Entry = entry
entriesByName[entryName] = existing
} else {
entryUuid := fmt.Sprintf("%05d", uuid)
if objectType == multiConfigUuid {
Expand All @@ -169,6 +173,19 @@ func MultiConfig[E sdkmanager.UuidObject](updates *xmlapi.MultiConfig, existingP
fixIndices(entry.Idx)
delete(entriesByName, entryName)
idx -= 1
case MultiConfigOperRename:
if _, found := entriesByName[oper.NewName]; found {
panic(fmt.Sprintf("FIXME: should propagate back error from MultiConfig"))
}

existing, found := entriesByName[entryName]
if !found {
panic(fmt.Sprintf("FIXME: should propagate back erro from MultiConfig"))
}

delete(entriesByName, entryName)
existing.Entry.SetEntryName(oper.NewName)
entriesByName[oper.NewName] = existing
case MultiConfigOperMove:
pivot, found := entriesByName[oper.Destination]
if !found && oper.Destination != "top" && oper.Destination != "bottom" {
Expand Down
Loading
Loading