From b46cd7837be9e7e9664daf04185b74662832a040 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 9 Jul 2025 09:10:07 +0200 Subject: [PATCH] feat(provider): add repository action variable resource import #17 --- CHANGELOG.md | 6 ++++++ docs/resources/repository_actions_variable.md | 8 +++++++ .../import.sh | 1 + .../repository_actions_variable_resource.go | 21 +++++++++++++++++-- 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 examples/resources/forgejo_repository_actions_variable/import.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ab3755..e7edc54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. +## 1.2.0 - 2025-07-09 + +### Added + +- Added repository action variable resource import. + ## 1.1.0 - 2025-07-08 ### Added diff --git a/docs/resources/repository_actions_variable.md b/docs/resources/repository_actions_variable.md index f5a9949..8df11be 100644 --- a/docs/resources/repository_actions_variable.md +++ b/docs/resources/repository_actions_variable.md @@ -30,3 +30,11 @@ resource "forgejo_repository_actions_variable" "main" { - `name` (String) The variable's name. It must be uppercase or the plan will not be idempotent. - `owner` (String) The variable's owner. - `repository` (String) The variable's repository. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import forgejo_repository_actions_variable.main // +``` diff --git a/examples/resources/forgejo_repository_actions_variable/import.sh b/examples/resources/forgejo_repository_actions_variable/import.sh new file mode 100644 index 0000000..b1c249b --- /dev/null +++ b/examples/resources/forgejo_repository_actions_variable/import.sh @@ -0,0 +1 @@ +terraform import forgejo_repository_actions_variable.main // diff --git a/internal/provider/repository_actions_variable_resource.go b/internal/provider/repository_actions_variable_resource.go index 5723484..cb0ef50 100644 --- a/internal/provider/repository_actions_variable_resource.go +++ b/internal/provider/repository_actions_variable_resource.go @@ -3,8 +3,10 @@ package provider import ( "context" "fmt" + "strings" "git.adyxax.org/adyxax/terraform-provider-forgejo/internal/client" + "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" @@ -16,7 +18,8 @@ type RepositoryActionsVariableResource struct { client *client.Client } -var _ resource.Resource = &RepositoryActionsVariableResource{} // Ensure provider defined types fully satisfy framework interfaces +var _ resource.Resource = &RepositoryActionsVariableResource{} // Ensure provider defined types fully satisfy framework interfaces +var _ resource.ResourceWithImportState = &RepositoryActionsVariableResource{} // Ensure provider defined types fully satisfy framework interfaces func NewRepositoryActionsVariableResource() resource.Resource { return &RepositoryActionsVariableResource{} } @@ -79,7 +82,7 @@ func (d *RepositoryActionsVariableResource) Create(ctx context.Context, req reso data.Name.ValueString(), data.Data.ValueString()) if err != nil { - resp.Diagnostics.AddError("CreateRepositoryActionsVariable", fmt.Sprintf("failed to create repository actions variable: %s", err)) + resp.Diagnostics.AddError("CreateRepositoryActionsVariable", fmt.Sprintf("failed to create repository actions variable: %s\nTry importing the resource instead?", err)) return } resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -102,6 +105,20 @@ func (d *RepositoryActionsVariableResource) Delete(ctx context.Context, req reso } } +func (r *RepositoryActionsVariableResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + idParts := strings.Split(req.ID, "/") + if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { + resp.Diagnostics.AddError( + "Unexpected Import Identifier", + fmt.Sprintf("Expected import identifier with format: owner/repository/variableName. Got: %q", req.ID), + ) + return + } + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("owner"), idParts[0])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("repository"), idParts[1])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[2])...) +} + func (d *RepositoryActionsVariableResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var data RepositoryActionsVariableResourceModel resp.Diagnostics.Append(req.State.Get(ctx, &data)...)