2025-01-09 12:46:36 +01:00
|
|
|
package resources
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
|
2025-01-29 19:11:12 +01:00
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
|
2025-01-27 13:36:07 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/api/scim/resources/patch"
|
2025-01-09 12:46:36 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/api/scim/schemas"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ResourceHandler[T ResourceHolder] interface {
|
2025-01-29 19:11:12 +01:00
|
|
|
Schema() *schemas.ResourceSchema
|
2025-01-09 12:46:36 +01:00
|
|
|
NewResource() T
|
|
|
|
|
|
|
|
Create(ctx context.Context, resource T) (T, error)
|
2025-01-14 15:44:41 +01:00
|
|
|
Replace(ctx context.Context, id string, resource T) (T, error)
|
2025-01-27 13:36:07 +01:00
|
|
|
Update(ctx context.Context, id string, operations patch.OperationCollection) error
|
2025-01-09 15:12:13 +01:00
|
|
|
Delete(ctx context.Context, id string) error
|
2025-01-10 12:15:06 +01:00
|
|
|
Get(ctx context.Context, id string) (T, error)
|
2025-01-21 13:31:54 +01:00
|
|
|
List(ctx context.Context, request *ListRequest) (*ListResponse[T], error)
|
2025-01-09 12:46:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceHolder interface {
|
2025-01-29 15:23:56 +01:00
|
|
|
SchemasHolder
|
2025-01-29 19:11:12 +01:00
|
|
|
GetResource() *schemas.Resource
|
2025-01-09 12:46:36 +01:00
|
|
|
}
|
|
|
|
|
2025-01-29 15:23:56 +01:00
|
|
|
type SchemasHolder interface {
|
|
|
|
GetSchemas() []schemas.ScimSchemaType
|
|
|
|
}
|
|
|
|
|
2025-01-29 19:11:12 +01:00
|
|
|
func buildResource[T ResourceHolder](ctx context.Context, handler ResourceHandler[T], details *domain.ObjectDetails) *schemas.Resource {
|
2025-01-09 12:46:36 +01:00
|
|
|
created := details.CreationDate.UTC()
|
|
|
|
if created.IsZero() {
|
|
|
|
created = details.EventDate.UTC()
|
|
|
|
}
|
|
|
|
|
2025-01-29 19:11:12 +01:00
|
|
|
schema := handler.Schema()
|
|
|
|
return &schemas.Resource{
|
2025-01-29 15:23:56 +01:00
|
|
|
ID: details.ID,
|
2025-01-29 19:11:12 +01:00
|
|
|
Schemas: []schemas.ScimSchemaType{schema.ID},
|
|
|
|
Meta: &schemas.ResourceMeta{
|
|
|
|
ResourceType: schema.Name,
|
|
|
|
Created: &created,
|
|
|
|
LastModified: gu.Ptr(details.EventDate.UTC()),
|
2025-01-09 12:46:36 +01:00
|
|
|
Version: strconv.FormatUint(details.Sequence, 10),
|
2025-01-29 19:11:12 +01:00
|
|
|
Location: schemas.BuildLocationForResource(ctx, schema.PluralName, details.ID),
|
2025-01-09 12:46:36 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|