Skip to content

Commit

Permalink
Make resource read methods to set actual state
Browse files Browse the repository at this point in the history
  • Loading branch information
virjaina committed Oct 20, 2023
1 parent c6ac75d commit 77a1199
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
27 changes: 17 additions & 10 deletions acloud/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package acloud
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand Down Expand Up @@ -61,10 +60,6 @@ func resourceCluster() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
//"node_pools": {
// Type: schema.TypeList,
// Optional: true,
//},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -103,7 +98,7 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, m interf
return diags
}

return diag.FromErr(fmt.Errorf("cluster was not created"))
return resourceClusterRead(ctx, d, m)
}

func resourceClusterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -118,10 +113,20 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, m interfac
if err != nil {
return diag.FromErr(err)
}
if cluster != nil {
return diags
if cluster == nil {
return diag.FromErr(fmt.Errorf("cluster was not found"))
}
return diag.FromErr(fmt.Errorf("cluster was not found"))

d.SetId(cluster.Identity)
d.Set("name", cluster.Name)
d.Set("description", cluster.Description)
d.Set("slug", cluster.Slug)
d.Set("cloud_provider", cluster.CloudProvider)
d.Set("region", cluster.Region)
d.Set("version", cluster.Version)
d.Set("update_channel", cluster.UpdateChannel)

return diags
}

func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -146,7 +151,7 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, m interf
return diags
}

return diags
return resourceClusterRead(ctx, d, m)
}

func resourceClusterDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -166,5 +171,7 @@ func resourceClusterDelete(ctx context.Context, d *schema.ResourceData, m interf
return diag.FromErr(err)
}

d.SetId("")

return diags
}
22 changes: 17 additions & 5 deletions acloud/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, m in
d.Set("slug", environment.Slug)
return diags
}
return diag.FromErr(fmt.Errorf("environment was not created"))
return resourceEnvironmentRead(ctx, d, m)
}

func resourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -91,10 +91,19 @@ func resourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, m inte
if err != nil {
return diag.FromErr(err)
}
if environment != nil {
return diags
if environment == nil {
return diag.FromErr(fmt.Errorf("environment was not found"))
}
return diag.FromErr(fmt.Errorf("environment was not found"))

d.SetId(strconv.Itoa(environment.ID))
d.Set("name", environment.Name)
d.Set("slug", environment.Slug)
d.Set("purpose", environment.Purpose)
d.Set("type", environment.Type)
d.Set("description", environment.Description)

return diags

}

func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -125,7 +134,7 @@ func resourceEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, m in
return diags
}

return diags
return resourceEnvironmentRead(ctx, d, m)
}

func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -140,5 +149,8 @@ func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, m in
if err != nil {
return diag.FromErr(err)
}

d.SetId("")

return diags
}
19 changes: 17 additions & 2 deletions acloud/resource_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ func resourceNodepoolCreate(ctx context.Context, d *schema.ResourceData, m inter
return diags
}

return diag.FromErr(fmt.Errorf("nodepool was not created"))
return resourceNodepoolRead(ctx, d, m)

}

func castNodeTaints(taints []interface{}) []acloudapi.NodeTaint {
Expand Down Expand Up @@ -204,6 +205,18 @@ func resourceNodepoolRead(ctx context.Context, d *schema.ResourceData, m interfa
return diag.FromErr(fmt.Errorf("nodepool was not found"))
}

nodePool := nodePools[idx]

d.SetId(strconv.Itoa(nodePool.ID))
d.Set("name", nodePool.Name)
d.Set("node_size", nodePool.NodeSize)
d.Set("auto_scaling", nodePool.AutoScaling)
d.Set("min_size", nodePool.MinSize)
d.Set("max_size", nodePool.MaxSize)
d.Set("annotations", nodePool.Annotations)
d.Set("labels", nodePool.Labels)
d.Set("taints", nodePool.Taints)

return diags
}

Expand Down Expand Up @@ -243,7 +256,7 @@ func resourceNodepoolUpdate(ctx context.Context, d *schema.ResourceData, m inter
return diags
}

return diags
return resourceNodepoolRead(ctx, d, m)
}

func resourceNodepoolDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -263,5 +276,7 @@ func resourceNodepoolDelete(ctx context.Context, d *schema.ResourceData, m inter
return diag.FromErr(err)
}

d.SetId("")

return diags
}

0 comments on commit 77a1199

Please sign in to comment.