Skip to content

Commit

Permalink
fix: agent id change requires recreate
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Apr 29, 2024
1 parent ad50c39 commit c3a3b92
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
13 changes: 9 additions & 4 deletions internal/provider/resource_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/humanitec/humanitec-go-autogen"
Expand Down Expand Up @@ -57,6 +59,9 @@ func (*Agent) Schema(ctx context.Context, req resource.SchemaRequest, resp *reso
"id": schema.StringAttribute{
MarkdownDescription: "The ID of the Agent.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
MarkdownDescription: "A description to show future users. It can be empty.",
Expand Down Expand Up @@ -102,7 +107,7 @@ func (a *Agent) Configure(ctx context.Context, req resource.ConfigureRequest, re
a.orgId = resdata.OrgID
}

func (a *AgentModel) updateFromContent(ctx context.Context, res *client.Agent, keys *[]client.Key) {
func (a *AgentModel) updateFromContent(res *client.Agent, keys *[]client.Key) {
a.ID = types.StringValue(res.Id)
if res.Description == nil {
a.Description = types.StringValue("")
Expand Down Expand Up @@ -181,7 +186,7 @@ func (a *Agent) Create(ctx context.Context, req resource.CreateRequest, resp *re
keys = append(keys, *registeredKey)
}
}
data.updateFromContent(ctx, agent, &keys)
data.updateFromContent(agent, &keys)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)

}
Expand Down Expand Up @@ -225,7 +230,7 @@ func (a *Agent) Read(ctx context.Context, req resource.ReadRequest, resp *resour
if resp.Diagnostics.HasError() {
return
}
data.updateFromContent(ctx, agent, registeredKeys)
data.updateFromContent(agent, registeredKeys)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

Expand Down Expand Up @@ -303,7 +308,7 @@ func (a *Agent) Update(ctx context.Context, req resource.UpdateRequest, resp *re
}
keys = append(keys, *registeredKey)
}
data.updateFromContent(ctx, agent, &keys)
data.updateFromContent(agent, &keys)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

Expand Down
6 changes: 3 additions & 3 deletions internal/provider/resource_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestAccAgent(t *testing.T) {
Steps: []resource.TestStep{
// Create and Read testing
{
Config: testAccCreateAgent(t, id, description, publicKeyOne, publicKeyTwo),
Config: testAccCreateAgent(id, description, publicKeyOne, publicKeyTwo),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("humanitec_agent.agent_test", "description", description),
resource.TestCheckResourceAttr("humanitec_agent.agent_test", "public_keys.#", "2"),
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestAccAgent(t *testing.T) {
},
// Update and Read testing
{
Config: testAccCreateAgent(t, id, "", publicKeyOne, publicKeyThree),
Config: testAccCreateAgent(id, "", publicKeyOne, publicKeyThree),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("humanitec_agent.agent_test", "description", ""),
resource.TestCheckResourceAttr("humanitec_agent.agent_test", "public_keys.#", "2"),
Expand All @@ -80,7 +80,7 @@ func TestAccAgent(t *testing.T) {

}

func testAccCreateAgent(t *testing.T, id, description string, publicKey, otherPublicKey string) string {
func testAccCreateAgent(id, description string, publicKey, otherPublicKey string) string {
return fmt.Sprintf(`
resource "humanitec_agent" "agent_test" {
id = "%s"
Expand Down

0 comments on commit c3a3b92

Please sign in to comment.