Skip to content

Commit

Permalink
Fix shuffled node pool diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
LBGarber committed Jul 21, 2021
1 parent b38781a commit d7dd7ab
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion linode/resource_linode_lke_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func resourceLinodeLKEClusterRead(ctx context.Context, d *schema.ResourceData, m
return diag.Errorf("Error parsing Linode LKE Cluster ID: %s", err)
}

declaredPools, ok := d.Get("pool").([]interface{})
if !ok {
return diag.Errorf("failed to parse linode lke cluster pools: %d", id)
}

cluster, err := client.GetLKECluster(context.Background(), id)
if err != nil {
if lerr, ok := err.(*linodego.Error); ok && lerr.Code == 404 {
Expand Down Expand Up @@ -171,8 +176,9 @@ func resourceLinodeLKEClusterRead(ctx context.Context, d *schema.ResourceData, m
d.Set("tags", cluster.Tags)
d.Set("status", cluster.Status)
d.Set("kubeconfig", kubeconfig.KubeConfig)
d.Set("pool", flattenLinodeLKEClusterPools(pools))
d.Set("api_endpoints", flattenLinodeLKEClusterAPIEndpoints(endpoints))
d.Set("pool", flattenLinodeLKEClusterPools(matchPoolsWithSchema(pools, declaredPools)))

return nil
}

Expand Down Expand Up @@ -604,3 +610,34 @@ func flattenLinodeLKEClusterAPIEndpoints(apiEndpoints []linodego.LKEClusterAPIEn
}
return flattened
}

// This cannot currently be handled efficiently by a DiffSuppressFunc
// See: https://github.com/hashicorp/terraform-plugin-sdk/issues/477
func matchPoolsWithSchema(pools []linodego.LKEClusterPool, declaredPools []interface{}) []linodego.LKEClusterPool {
result := make([]linodego.LKEClusterPool, len(declaredPools))

poolMap := make(map[int]linodego.LKEClusterPool, len(declaredPools))
for _, pool := range pools {
poolMap[pool.ID] = pool
}

for i, declaredPool := range declaredPools {
declaredPool := declaredPool.(map[string]interface{})

for key, pool := range poolMap {
if pool.Count != declaredPool["count"] || pool.Type != declaredPool["type"] {
continue
}

result[i] = pool
delete(poolMap, key)
break
}
}

for _, pool := range poolMap {
result = append(result, pool)
}

return result
}

0 comments on commit d7dd7ab

Please sign in to comment.