Skip to content

Commit

Permalink
Fix/process updates (#56)
Browse files Browse the repository at this point in the history
* fix: processes updates

* test: processes updates

* test: fix flaky test

* doc: add comments to improve clarity
  • Loading branch information
crgisch authored Mar 27, 2024
1 parent 98d6099 commit dbff4e2
Show file tree
Hide file tree
Showing 3 changed files with 580 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/provider/resource_tsuru_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,18 @@ func resourceTsuruApplicationUpdate(ctx context.Context, d *schema.ResourceData,
}
}

if m, ok := d.GetOk("process"); ok {
processes := processesFromResourceData(m)
if processes != nil {
app.Processes = processes
if d.HasChange("process") {
old, new := d.GetChange("process")
oldProcesses := processesFromResourceData(old)
if oldProcesses == nil {
oldProcesses = []tsuru_client.AppProcess{}
}
newProcesses := processesFromResourceData(new)
if newProcesses == nil {
newProcesses = []tsuru_client.AppProcess{}
}

app.Processes = markRemovedProcessAsDefaultPlan(oldProcesses, newProcesses)
}

if desc, ok := d.GetOk("description"); ok {
Expand Down
78 changes: 78 additions & 0 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package provider
import (
"context"
"net/http"
"sort"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -22,6 +23,11 @@ type MaxRetriesError struct {
Meta interface{}
}

type ChangedProcess struct {
Old tsuru_client.AppProcess
New tsuru_client.AppProcess
}

func (e *MaxRetriesError) Error() string {
return e.Message
}
Expand Down Expand Up @@ -82,3 +88,75 @@ func markRemovedMetadataItemAsDeleted(oldMetadataItems []tsuru_client.MetadataIt
}
return newMetadataItemsList
}

func markRemovedProcessAsDefaultPlan(oldProcesses []tsuru_client.AppProcess, newProcesses []tsuru_client.AppProcess) []tsuru_client.AppProcess {
onlyInOldList, onlyInNewList, inBoth := checkProcessesListsChanges(oldProcesses, newProcesses)

newProcessesList := onlyInNewList //new processes need no change and can be added directly
for _, oldProcess := range onlyInOldList { //any process deleted needs to be marked as $default plan and needs annotations and labels mark to deletion
removedProcess := tsuru_client.AppProcess{
Name: oldProcess.Name,
Plan: "$default",
Metadata: tsuru_client.Metadata{
Annotations: markRemovedMetadataItemAsDeleted(oldProcess.Metadata.Annotations, []tsuru_client.MetadataItem{}),
Labels: markRemovedMetadataItemAsDeleted(oldProcess.Metadata.Labels, []tsuru_client.MetadataItem{}),
},
}
newProcessesList = append(newProcessesList, removedProcess)
}

for _, changedProcess := range inBoth { //for processes changes, we need to check if metadata was changed and properly update or delete them
processChange := tsuru_client.AppProcess{
Name: changedProcess.New.Name,
Plan: changedProcess.New.Plan,
Metadata: tsuru_client.Metadata{
Annotations: markRemovedMetadataItemAsDeleted(changedProcess.Old.Metadata.Annotations, changedProcess.New.Metadata.Annotations),
Labels: markRemovedMetadataItemAsDeleted(changedProcess.Old.Metadata.Labels, changedProcess.New.Metadata.Labels),
},
}
newProcessesList = append(newProcessesList, processChange)
}

sort.Slice(newProcessesList, func(i, j int) bool { //sort for consistent output
return newProcessesList[i].Name < newProcessesList[j].Name
})

return newProcessesList
}

func checkProcessesListsChanges(oldProcesses []tsuru_client.AppProcess, newProcesses []tsuru_client.AppProcess) ([]tsuru_client.AppProcess, []tsuru_client.AppProcess, []ChangedProcess) {
oldMap := map[string]tsuru_client.AppProcess{}
newMap := map[string]tsuru_client.AppProcess{}

for _, oldProcess := range oldProcesses {
oldMap[oldProcess.Name] = oldProcess
}

for _, newProcess := range newProcesses {
newMap[newProcess.Name] = newProcess
}

onlyInOldList := []tsuru_client.AppProcess{}
onlyInNewList := []tsuru_client.AppProcess{}
inBoth := []ChangedProcess{}

for key, oldValue := range oldMap {
if _, found := newMap[key]; found {
changedProcess := ChangedProcess{
Old: oldMap[key],
New: newMap[key],
}
inBoth = append(inBoth, changedProcess)
} else {
onlyInOldList = append(onlyInOldList, oldValue)
}
}

for key, newValue := range newMap {
if _, found := oldMap[key]; !found {
onlyInNewList = append(onlyInNewList, newValue)
}
}

return onlyInOldList, onlyInNewList, inBoth
}
Loading

0 comments on commit dbff4e2

Please sign in to comment.