Skip to content

Commit

Permalink
Feat/allow label annotation deletion (#55)
Browse files Browse the repository at this point in the history
* feature: support labels/annotation deletions

* test: labels/annotation deletions

* refactor: review suggestions
  • Loading branch information
crgisch authored Mar 4, 2024
1 parent 7ba2512 commit 96ec7d1
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 5 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ HOSTNAME=registry.terraform.io
NAMESPACE=tsuru
NAME=tsuru
BINARY=terraform-provider-${NAME}
VERSION=2.9.0
VERSION=2.9.2

UNAME_S := $(shell uname -s)
UNAME_P := $(shell uname -p)
Expand Down
18 changes: 14 additions & 4 deletions internal/provider/resource_tsuru_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,20 @@ func resourceTsuruApplicationUpdate(ctx context.Context, d *schema.ResourceData,
Tags: tags,
}

if m, ok := d.GetOk("metadata"); ok {
metadata := metadataFromResourceData(m)
if metadata != nil {
app.Metadata = *metadata
if d.HasChange("metadata") {
old, new := d.GetChange("metadata")
oldMetadata := metadataFromResourceData(old)
if oldMetadata == nil {
oldMetadata = &tsuru_client.Metadata{}
}
newMetadata := metadataFromResourceData(new)
if newMetadata == nil {
newMetadata = &tsuru_client.Metadata{}
}

app.Metadata = tsuru_client.Metadata{
Annotations: markRemovedMetadataItemAsDeleted(oldMetadata.Annotations, newMetadata.Annotations),
Labels: markRemovedMetadataItemAsDeleted(oldMetadata.Labels, newMetadata.Labels),
}
}

Expand Down
136 changes: 136 additions & 0 deletions internal/provider/resource_tsuru_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,87 @@ func TestAccResourceTsuruApp(t *testing.T) {
return c.JSON(http.StatusOK, app)
}

if iterationCount == 2 {
app := &tsuru.App{
Name: name,
Description: "my app description",
TeamOwner: "my-team",
Platform: "python",
Plan: tsuru.Plan{Name: "c2m4"},
Cluster: "my-cluster-01",
Pool: "prod",
Provisioner: "kubernetes",
Tags: []string{"tagA", "tagB"},
Metadata: tsuru.Metadata{
Labels: []tsuru.MetadataItem{{Name: "label1", Value: "value1"}, {Name: "label3", Value: "value3"}},
},
Deploys: 2,
Units: []tsuru.Unit{
{Processname: "web"},
},
InternalAddresses: []tsuru.AppInternalAddresses{
{
Version: "10",
Port: 8888,
Process: "web",
Domain: "app01.namespace.svc.cluster.local",
Protocol: "TCP",
},
},
Routers: []tsuru.AppRouters{
{
Name: "default-router",
Addresses: []string{
"my-app.router.io",
},
},
},
Processes: []tsuru.AppProcess{
{
Name: "web",
Plan: "c2m2",
Metadata: tsuru.Metadata{
Labels: []tsuru.MetadataItem{
{
Name: "weblabel",
Value: "value",
},
},
Annotations: []tsuru.MetadataItem{
{
Name: "webannotation",
Value: "nice",
},
},
},
},
{
Name: "worker",
Metadata: tsuru.Metadata{
Labels: []tsuru.MetadataItem{
{
Name: "workerlabel",
Value: "value",
},
},
Annotations: []tsuru.MetadataItem{
{
Name: "workerannotation",
Value: "nice",
},
},
},
},
},
}
return c.JSON(http.StatusOK, app)
}

return c.JSON(http.StatusNotFound, nil)
})

fakeServer.PUT("/1.0/apps/:name", func(c echo.Context) error {
iterationCount++
return c.JSON(http.StatusOK, nil)
})

Expand Down Expand Up @@ -171,6 +248,19 @@ func TestAccResourceTsuruApp(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "internal_address.0.domain", "app01.namespace.svc.cluster.local"),
),
},
{
Config: testAccResourceTsuruApp_metadataAfterUpdate(),
Check: resource.ComposeAggregateTestCheckFunc(
testAccResourceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", "app01"),
resource.TestCheckResourceAttr(resourceName, "description", "my app description"),
resource.TestCheckResourceAttr(resourceName, "platform", "python"),
resource.TestCheckResourceAttr(resourceName, "plan", "c2m4"),
resource.TestCheckResourceAttr(resourceName, "team_owner", "my-team"),
resource.TestCheckResourceAttr(resourceName, "pool", "prod"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "tagA"),
),
},
},
})
}
Expand Down Expand Up @@ -222,3 +312,49 @@ func testAccResourceTsuruApp_basic() string {
}
`
}

func testAccResourceTsuruApp_metadataAfterUpdate() 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"]
metadata {
labels = {
"label1" = "value1"
"label3" = "value3"
}
}
process {
name = "web"
plan = "c2m2"
metadata {
labels = {
"weblabel" = "value"
}
annotations = {
"webannotation": "nice"
}
}
}
process {
name = "worker"
metadata {
labels = {
"workerlabel" = "value"
}
annotations = {
"workerannotation": "nice"
}
}
}
}
`
}
16 changes: 16 additions & 0 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ func IDtoParts(input string, minLength int) ([]string, error) {
}
return output, nil
}

func markRemovedMetadataItemAsDeleted(oldMetadataItems []tsuru_client.MetadataItem, newMetadataItems []tsuru_client.MetadataItem) []tsuru_client.MetadataItem {
newMap := map[string]bool{}
for _, newMetadataItem := range newMetadataItems {
newMap[newMetadataItem.Name] = true
}

newMetadataItemsList := newMetadataItems
for _, oldMetadataItem := range oldMetadataItems {
if _, found := newMap[oldMetadataItem.Name]; !found {
oldMetadataItem.Delete = true
newMetadataItemsList = append(newMetadataItemsList, oldMetadataItem)
}
}
return newMetadataItemsList
}
108 changes: 108 additions & 0 deletions internal/provider/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package provider

import (
"testing"

"github.com/stretchr/testify/assert"
tsuru_client "github.com/tsuru/go-tsuruclient/pkg/tsuru"
)

func TestMarkRemovedMetadataItemWhenOldIsEmpty(t *testing.T) {
oldMetadataItemsList := []tsuru_client.MetadataItem{}
newMetadataItemsList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "label_value",
}}

resultMetadataItemList := markRemovedMetadataItemAsDeleted(oldMetadataItemsList, newMetadataItemsList)

expectedList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "label_value",
}}
assert.Equal(t, expectedList, resultMetadataItemList)
}

func TestMarkRemovedMetadataItemWhenNewIsEmpty(t *testing.T) {
oldMetadataItemsList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "label_value",
}}
newMetadataItemsList := []tsuru_client.MetadataItem{}

resultMetadataItemList := markRemovedMetadataItemAsDeleted(oldMetadataItemsList, newMetadataItemsList)

expectedList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "label_value",
Delete: true,
}}
assert.Equal(t, expectedList, resultMetadataItemList)
}

func TestMarkRemovedMetadataItemWhenOldIsReplaced(t *testing.T) {
oldMetadataItemsList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "label_value",
}}
newMetadataItemsList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "new_label_value",
}}

resultMetadataItemList := markRemovedMetadataItemAsDeleted(oldMetadataItemsList, newMetadataItemsList)

expectedList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "new_label_value",
}}
assert.Equal(t, expectedList, resultMetadataItemList)
}

func TestMarkRemovedMetadataItemWhenOldIsRemovedNewIsAdded(t *testing.T) {
oldMetadataItemsList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "label_value",
}}
newMetadataItemsList := []tsuru_client.MetadataItem{{
Name: "new_label_name",
Value: "new_label_value",
}}

resultMetadataItemList := markRemovedMetadataItemAsDeleted(oldMetadataItemsList, newMetadataItemsList)

expectedList := []tsuru_client.MetadataItem{{
Name: "new_label_name",
Value: "new_label_value",
}, {
Name: "label_name",
Value: "label_value",
Delete: true,
}}
assert.Equal(t, expectedList, resultMetadataItemList)
}

func TestMarkRemovedMetadataItemWhenOldShouldNotChange(t *testing.T) {
oldMetadataItemsList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "label_value",
}}
newMetadataItemsList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "label_value",
}, {
Name: "new_label_name",
Value: "new_label_value",
}}

resultMetadataItemList := markRemovedMetadataItemAsDeleted(oldMetadataItemsList, newMetadataItemsList)

expectedList := []tsuru_client.MetadataItem{{
Name: "label_name",
Value: "label_value",
}, {
Name: "new_label_name",
Value: "new_label_value",
}}
assert.Equal(t, expectedList, resultMetadataItemList)
}

0 comments on commit 96ec7d1

Please sign in to comment.