feat(provider): add repository action variable resource import #17

This commit is contained in:
Julien Dessaux 2025-07-09 09:10:07 +02:00
parent 898bc8e607
commit b46cd7837b
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 34 additions and 2 deletions

View file

@ -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

View file

@ -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 <owner>/<repository_name>/<variable_name>
```

View file

@ -0,0 +1 @@
terraform import forgejo_repository_actions_variable.main <owner>/<repository_name>/<variable_name>

View file

@ -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)...)