feat: implement activity resource and data source

This commit is contained in:
2026-06-01 11:27:13 -04:00
parent bcc8a1a47d
commit f581951019
8 changed files with 577 additions and 0 deletions
+136
View File
@@ -0,0 +1,136 @@
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/immich-app/terraform-provider-immich/internal/client"
)
// Ensure the implementation satisfies the expected interfaces.
var _ datasource.DataSource = &activitiesDataSource{}
func NewActivitiesDataSource() datasource.DataSource {
return &activitiesDataSource{}
}
// activitiesDataSource defines the data source implementation.
type activitiesDataSource struct {
client *client.Client
}
// activitiesDataSourceModel describes the data source data model.
type activitiesDataSourceModel struct {
AlbumId types.String `tfsdk:"album_id"`
AssetId types.String `tfsdk:"asset_id"`
Activities []activitiesModel `tfsdk:"activities"`
}
type activitiesModel struct {
ID types.String `tfsdk:"id"`
Type types.String `tfsdk:"type"`
UserId types.String `tfsdk:"user_id"`
Comment types.String `tfsdk:"comment"`
CreatedAt types.String `tfsdk:"created_at"`
}
func (d *activitiesDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_activities"
}
func (d *activitiesDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Retrieves a list of activities for an album or asset.",
Attributes: map[string]schema.Attribute{
"album_id": schema.StringAttribute{
Required: true,
MarkdownDescription: "ID of the album.",
},
"asset_id": schema.StringAttribute{
Optional: true,
MarkdownDescription: "ID of the asset.",
},
"activities": schema.ListNestedAttribute{
Computed: true,
MarkdownDescription: "List of activities.",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Unique identifier for the activity.",
},
"type": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Type of activity (COMMENT or LIKE).",
},
"user_id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "ID of the user who performed the activity.",
},
"comment": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Comment text.",
},
"created_at": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Timestamp when the activity was created.",
},
},
},
},
},
}
}
func (d *activitiesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*client.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (d *activitiesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data activitiesDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
activities, err := d.client.GetActivities(data.AlbumId.ValueString(), data.AssetId.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read activities, got error: %s", err))
return
}
data.Activities = []activitiesModel{}
for _, activity := range activities {
activityState := activitiesModel{
ID: types.StringValue(activity.ID),
Type: types.StringValue(activity.Type),
UserId: types.StringValue(activity.User.ID),
Comment: types.StringValue(activity.Comment),
CreatedAt: types.StringValue(activity.CreatedAt),
}
data.Activities = append(data.Activities, activityState)
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
+218
View File
@@ -0,0 +1,218 @@
package provider
import (
"context"
"fmt"
"strings"
"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 = &activityResource{}
var _ resource.ResourceWithImportState = &activityResource{}
func NewActivityResource() resource.Resource {
return &activityResource{}
}
// activityResource defines the resource implementation.
type activityResource struct {
client *client.Client
}
// activityResourceModel describes the resource data model.
type activityResourceModel struct {
ID types.String `tfsdk:"id"`
AlbumId types.String `tfsdk:"album_id"`
AssetId types.String `tfsdk:"asset_id"`
Type types.String `tfsdk:"type"`
Comment types.String `tfsdk:"comment"`
CreatedAt types.String `tfsdk:"created_at"`
UserId types.String `tfsdk:"user_id"`
}
func (r *activityResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_activity"
}
func (r *activityResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Manages an Immich activity (comment or like).",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Unique identifier for the activity.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"album_id": schema.StringAttribute{
Required: true,
MarkdownDescription: "ID of the album.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"asset_id": schema.StringAttribute{
Optional: true,
MarkdownDescription: "ID of the asset.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"type": schema.StringAttribute{
Required: true,
MarkdownDescription: "Type of activity (comment or like).",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"comment": schema.StringAttribute{
Optional: true,
MarkdownDescription: "Comment text (required for type 'comment').",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"created_at": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Timestamp when the activity was created.",
},
"user_id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "ID of the user who performed the activity.",
},
},
}
}
func (r *activityResource) 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 *activityResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data activityResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
createReq := client.CreateActivityRequest{
Type: data.Type.ValueString(),
AlbumId: data.AlbumId.ValueString(),
AssetId: data.AssetId.ValueString(),
Comment: data.Comment.ValueString(),
}
activity, err := r.client.CreateActivity(createReq)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create activity, got error: %s", err))
return
}
data.ID = types.StringValue(activity.ID)
data.CreatedAt = types.StringValue(activity.CreatedAt)
data.UserId = types.StringValue(activity.User.ID)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *activityResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data activityResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// We have to find the activity in the list
activities, err := r.client.GetActivities(data.AlbumId.ValueString(), data.AssetId.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read activities, got error: %s", err))
return
}
var found *client.Activity
for _, a := range activities {
if a.ID == data.ID.ValueString() {
found = &a
break
}
}
if found == nil {
resp.State.RemoveResource(ctx)
return
}
data.Type = types.StringValue(strings.ToLower(found.Type))
data.Comment = types.StringValue(found.Comment)
data.CreatedAt = types.StringValue(found.CreatedAt)
data.UserId = types.StringValue(found.User.ID)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *activityResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Immich doesn't seem to support updating activities.
}
func (r *activityResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data activityResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
err := r.client.DeleteActivity(data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete activity, got error: %s", err))
return
}
}
func (r *activityResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Expected format: album_id/activity_id or album_id/asset_id/activity_id
idParts := strings.Split(req.ID, "/")
if len(idParts) == 2 {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("album_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[1])...)
} else if len(idParts) == 3 {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("album_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("asset_id"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[2])...)
} else {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: album_id/activity_id or album_id/asset_id/activity_id. Got: %q", req.ID),
)
}
}
+2
View File
@@ -92,6 +92,7 @@ func (p *immichProvider) Resources(ctx context.Context) []func() resource.Resour
NewAlbumResource,
NewSystemConfigResource,
NewLibraryResource,
NewActivityResource,
}
}
@@ -100,6 +101,7 @@ func (p *immichProvider) DataSources(ctx context.Context) []func() datasource.Da
NewUsersDataSource,
NewAlbumsDataSource,
NewLibrariesDataSource,
NewActivitiesDataSource,
}
}