feat: implement tags api

This commit is contained in:
2026-06-01 11:32:05 -04:00
parent 3a4cea2e33
commit a41c992260
3 changed files with 297 additions and 0 deletions
+1
View File
@@ -97,6 +97,7 @@ func (p *immichProvider) Resources(ctx context.Context) []func() resource.Resour
NewPartnerResource,
NewMemoryResource,
NewStackResource,
NewTagResource,
}
}
+172
View File
@@ -0,0 +1,172 @@
package provider
import (
"context"
"fmt"
"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/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/immich-app/terraform-provider-immich/internal/client"
)
// Ensure the implementation satisfies the expected interfaces.
var _ resource.Resource = &tagResource{}
var _ resource.ResourceWithImportState = &tagResource{}
func NewTagResource() resource.Resource {
return &tagResource{}
}
// tagResource defines the resource implementation.
type tagResource struct {
client *client.Client
}
// tagResourceModel describes the resource data model.
type tagResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Type types.String `tfsdk:"type"`
}
func (r *tagResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_tag"
}
func (r *tagResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Manages an Immich tag.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Unique identifier for the tag.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
Required: true,
MarkdownDescription: "Name of the tag.",
},
"type": schema.StringAttribute{
Required: true,
MarkdownDescription: "Type of the tag (OBJECT or USER).",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
func (r *tagResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*client.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r *tagResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data tagResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
createReq := client.CreateTagRequest{
Name: data.Name.ValueString(),
Type: data.Type.ValueString(),
}
tag, err := r.client.CreateTag(createReq)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create tag, got error: %s", err))
return
}
data.ID = types.StringValue(tag.ID)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *tagResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data tagResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
tag, err := r.client.GetTag(data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read tag, got error: %s", err))
return
}
data.Name = types.StringValue(tag.Name)
data.Type = types.StringValue(tag.Type)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *tagResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data tagResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
updateReq := client.UpdateTagRequest{
Name: data.Name.ValueString(),
}
_, err := r.client.UpdateTag(data.ID.ValueString(), updateReq)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update tag, got error: %s", err))
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *tagResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data tagResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
err := r.client.DeleteTag(data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete tag, got error: %s", err))
return
}
}
func (r *tagResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}