Skip to content

Commit

Permalink
Add support to new fields on app and job
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Nov 8, 2023
1 parent 4995218 commit 569239f
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 392 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.7.2
VERSION=2.7.1

UNAME_S := $(shell uname -s)
UNAME_P := $(shell uname -p)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/labstack/echo/v4 v4.9.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
github.com/tsuru/go-tsuruclient v0.0.0-20231009130311-a01dfd615e16
github.com/tsuru/go-tsuruclient v0.0.0-20231108180105-5735487e6f98
github.com/tsuru/tsuru v0.0.0-20230721211340-f41fb455f8c3
k8s.io/apimachinery v0.22.5
)
Expand Down
343 changes: 8 additions & 335 deletions go.sum

Large diffs are not rendered by default.

63 changes: 62 additions & 1 deletion internal/provider/data_source_tsuru_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ func dataSourceTsuruApp() *schema.Resource {
},
},

"metadata": metadataSchema(),
"process": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"plan": {
Type: schema.TypeString,
Computed: true,
},
"metadata": metadataSchema(),
},
},
},

"router": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -137,7 +156,8 @@ func dataSourceTsuruAppRead(ctx context.Context, d *schema.ResourceData, meta in

d.Set("internal_address", flattenInternalAddresses(app.InternalAddresses))
d.Set("router", flattenRouters(app.Routers))

d.Set("metadata", flattenMetadata(app.Metadata))
d.Set("process", flattenProcesses(app.Processes))
d.Set("team_owner", app.TeamOwner)
d.Set("teams", app.Teams)

Expand Down Expand Up @@ -173,3 +193,44 @@ func flattenRouters(routers []tsuru.AppRouters) []interface{} {

return result
}

func flattenMetadata(metadata tsuru.Metadata) []interface{} {
annotations := map[string]interface{}{}
if len(metadata.Annotations) > 0 {
for _, annotation := range metadata.Annotations {
annotations[annotation.Name] = annotation.Value
}
}

labels := map[string]interface{}{}
if len(metadata.Labels) > 0 {
for _, label := range metadata.Labels {
labels[label.Name] = label.Value
}
}

if len(annotations) > 0 || len(labels) > 0 {
return []interface{}{
map[string]interface{}{
"annotations": annotations,
"labels": labels,
},
}
}

return []interface{}{}
}

func flattenProcesses(processes []tsuru.AppProcess) []interface{} {
result := []interface{}{}

for _, process := range processes {
result = append(result, map[string]interface{}{
"name": process.Name,
"plan": process.Plan,
"metadata": flattenMetadata(process.Metadata),
})
}

return result
}
111 changes: 80 additions & 31 deletions internal/provider/resource_tsuru_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,21 @@ func resourceTsuruApplication() *schema.Resource {
Type: schema.TypeString,
},
},
"metadata": {
"metadata": metadataSchema(),
"process": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
"name": {
Type: schema.TypeString,
Required: true,
},
"annotations": {
Type: schema.TypeMap,
"plan": {
Type: schema.TypeString,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"metadata": metadataSchema(),
},
},
},
Expand Down Expand Up @@ -161,6 +160,28 @@ func resourceTsuruApplication() *schema.Resource {
}
}

func metadataSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"annotations": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
}

func resourceTsuruApplicationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
provider := meta.(*tsuruProvider)

Expand Down Expand Up @@ -206,6 +227,13 @@ func resourceTsuruApplicationCreate(ctx context.Context, d *schema.ResourceData,
}
}

if m, ok := d.GetOk("process"); ok {
processes := processesFromResourceData(m)
if processes != nil {
app.Processes = processes
}
}

if desc, ok := d.GetOk("description"); ok {
app.Description = desc.(string)
}
Expand Down Expand Up @@ -308,29 +336,10 @@ func resourceTsuruApplicationRead(ctx context.Context, d *schema.ResourceData, m

d.Set("tags", app.Tags)

annotations := map[string]interface{}{}
if len(app.Metadata.Annotations) > 0 {
for _, annotation := range app.Metadata.Annotations {
annotations[annotation.Name] = annotation.Value
}
}

labels := map[string]interface{}{}
if len(app.Metadata.Labels) > 0 {
for _, label := range app.Metadata.Labels {
labels[label.Name] = label.Value
}
}

if len(annotations) > 0 || len(labels) > 0 {
d.Set("metadata", []map[string]interface{}{{
"annotations": annotations,
"labels": labels,
}})
}

d.Set("metadata", flattenMetadata(app.Metadata))
d.Set("internal_address", flattenInternalAddresses(app.InternalAddresses))
d.Set("router", flattenRouters(app.Routers))
d.Set("process", flattenProcesses(app.Processes))

return nil
}
Expand Down Expand Up @@ -361,6 +370,45 @@ func resourceTsuruApplicationImport(ctx context.Context, d *schema.ResourceData,
return []*schema.ResourceData{d}, nil
}

func processesFromResourceData(meta interface{}) []tsuru_client.AppProcess {
m := meta.([]interface{})

if len(m) == 0 {
return nil
}

processes := []tsuru_client.AppProcess{}

for _, iface := range m {
process := tsuru_client.AppProcess{}
m := iface.(map[string]interface{})

if v, ok := m["name"]; ok {
process.Name = v.(string)
}

if v, ok := m["plan"]; ok {
process.Plan = v.(string)
}

if process.Plan == "" {
process.Plan = "$default"
}

if v, ok := m["metadata"]; ok {
metadata := metadataFromResourceData(v)

if metadata != nil {
process.Metadata = *metadata
}
}

processes = append(processes, process)
}

return processes
}

func metadataFromResourceData(meta interface{}) *tsuru_client.Metadata {
m := meta.([]interface{})
if len(m) == 0 || m[0] == nil {
Expand Down Expand Up @@ -398,14 +446,15 @@ func validPlatform(ctx context.Context, provider *tsuruProvider, platform string
return err
}

platformParts := strings.SplitN(platform, ":", 2)
availablePlatforms := []string{}
for _, p := range platforms {
if p.Disabled {
continue
}

availablePlatforms = append(availablePlatforms, p.Name)
if p.Name == platform {
if p.Name == platformParts[0] {
return nil
}
}
Expand Down
63 changes: 63 additions & 0 deletions internal/provider/resource_tsuru_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,43 @@ func TestAccResourceTsuruApp(t *testing.T) {
},
},
},
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)
}
Expand Down Expand Up @@ -156,6 +193,32 @@ func testAccResourceTsuruApp_basic() string {
"annotation1": "some really long value"
}
}
process {
name = "web"
plan = "c2m2"
metadata {
labels = {
"weblabel" = "value"
}
annotations = {
"webannotation": "nice"
}
}
}
process {
name = "worker"
metadata {
labels = {
"workerlabel" = "value"
}
annotations = {
"workerannotation": "nice"
}
}
}
}
`
}
Loading

0 comments on commit 569239f

Please sign in to comment.