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

feat: Validate app processes are alphabetically sorted #64

Merged
merged 4 commits into from
Jan 28, 2025
Merged
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
17 changes: 17 additions & 0 deletions internal/provider/resource_tsuru_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func resourceTsuruApplicationCreate(ctx context.Context, d *schema.ResourceData,
if m, ok := d.GetOk("process"); ok {
processes := processesFromResourceData(m)
if processes != nil {
err := validateProcessesOrder(processes)
if err != nil {
return diag.FromErr(err)
}
app.Processes = processes
}
}
Expand Down Expand Up @@ -306,6 +310,10 @@ func resourceTsuruApplicationUpdate(ctx context.Context, d *schema.ResourceData,
if newProcesses == nil {
newProcesses = []tsuru_client.AppProcess{}
}
err := validateProcessesOrder(newProcesses)
if err != nil {
return diag.FromErr(err)
}

app.Processes = markRemovedProcessAsDefaultPlan(oldProcesses, newProcesses)
}
Expand Down Expand Up @@ -334,6 +342,15 @@ func resourceTsuruApplicationUpdate(ctx context.Context, d *schema.ResourceData,
return resourceTsuruApplicationRead(ctx, d, meta)
}

func validateProcessesOrder(processes []tsuru_client.AppProcess) error {
for i := 1; i < len(processes); i++ {
if processes[i-1].Name > processes[i].Name {
return errors.Errorf("please, sort app processes alphabetically")
}
}
return nil
}

func resourceTsuruApplicationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
provider := meta.(*tsuruProvider)
name := d.Id()
Expand Down
45 changes: 45 additions & 0 deletions internal/provider/resource_tsuru_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -261,6 +262,10 @@ func TestAccResourceTsuruApp(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.0", "tagA"),
),
},
{
Config: testAccResourceTsuruApp_invalidProcessOrder(),
ExpectError: regexp.MustCompile("please, sort app processes alphabetically"),
},
},
})
}
Expand Down Expand Up @@ -358,3 +363,43 @@ func testAccResourceTsuruApp_metadataAfterUpdate() string {
}
`
}

func testAccResourceTsuruApp_invalidProcessOrder() string {
return `
resource "tsuru_app" "app" {
name = "app01"
description = "my app description"
platform = "python"
plan = "c2m4"
team_owner = "my-team"
pool = "prod"
tags = ["tagA", "tagB"]

process {
name = "worker"

metadata {
labels = {
"workerlabel" = "value"
}
annotations = {
"workerannotation" = "nice"
}
}
}

process {
name = "web"
plan = "c2m2"
metadata {
labels = {
"weblabel" = "value"
}
annotations = {
"webannotation" = "nice"
}
}
}
}
`
}
Loading