Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: agent id change requires recreate #81

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that the agent with the old id is removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, RequiresReplace indicates that this attribute can't be updated in-place and instead the old one needs to be deleted and a new resource needs to be created.

We also use this for example for applications: https://github.com/humanitec/terraform-provider-humanitec/blob/main/internal/provider/resource_application.go#L69

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
Loading