0
votes

I am finding some inconsistencies when the state file is written after creating a provider.

This is my resource:

package provider

import (
    "context"
    "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
    "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceTest() *schema.Resource {
    return &schema.Resource{
        CreateContext: resourceTestCreate,
        ReadContext:   resourceTestRead,
        UpdateContext: resourceTestUpdate,
        DeleteContext: resourceTestDelete,
        Schema: map[string]*schema.Schema{
            "name": &schema.Schema{
                Type:     schema.TypeString,
                Required: true,
            },
            "kids": &schema.Schema{
                Type:     schema.TypeSet,
                Required: true,
                Elem: &schema.Schema{
                    Type: schema.TypeString,
                },
            },
        },
    }
}

func resourceTestCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
    d.SetId("id")
    d.Set("name", "AnakinCreate")
    d.Set("kids", []string{"LukeCreate"})
    return nil
}

func resourceTestRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
    d.Set("name", "AnakinRead")
    d.Set("kids", []string{"LukeRead"})
    return nil
}

func resourceTestUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
    d.Set("name", "AnakinUpdate")
    d.Set("kids", []string{"LukeUpdate"})
    return nil
}

func resourceTestDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
    d.SetId("")
    return nil
}

and this is my definition:

resource "hue_test" "test" {
  name = "Anakin"
  kids = [
    "Luke"
  ]
}

when I run apply for the first time, I get this in the state "name": "AnakinCreate" (as expected) but "kids": ["Luke"] instead of LukeCreate.

after a refresh (read), I get AnakinRead and LukeRead as expected.

when I apply again, it detects the drift as expected.

after applying, the state has "name": "AnakinUpdate" (as expected) but ``"kids": ["Luke"]instead ofLukeUpdate`.

The example is minimalist, my structure is far more complex than that -- but if I can solve this inconsistency I can probably solve my original issue.

Thank you.