From 3835734ed4090991285dbd148dfbd34fa161c2f3 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 13 Nov 2023 14:35:48 +0100 Subject: [PATCH] add API to trigger sync Signed-off-by: Jens Langhammer --- authentik/sources/ldap/api/sources.py | 14 ++++--- schema.yml | 39 +++++++++++++++++-- .../admin/sources/ldap/LDAPSourceViewPage.ts | 5 +-- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/authentik/sources/ldap/api/sources.py b/authentik/sources/ldap/api/sources.py index c9cb53e80..77f7c5da1 100644 --- a/authentik/sources/ldap/api/sources.py +++ b/authentik/sources/ldap/api/sources.py @@ -138,14 +138,18 @@ class LDAPSourceViewSet(UsedByMixin, ModelViewSet): ordering = ["name"] @extend_schema( + request=None, responses={ 200: LDAPSyncStatusSerializer(), - } + }, ) - @action(methods=["GET"], detail=True, pagination_class=None, filter_backends=[]) - def sync_status(self, request: Request, slug: str) -> Response: - """Get source's sync status""" - source: LDAPSource = self.get_object() + @action(methods=["GET", "POST"], detail=True, pagination_class=None, filter_backends=[]) + def sync(self, request: Request, slug: str) -> Response: + """Get source's sync status or start source sync""" + source = self.get_object() + if request.method == "POST": + # We're not waiting for the sync to finish here as it could take multiple hours + ldap_sync_single.delay(source.pk) tasks = TaskInfo.by_name(f"ldap_sync:{source.slug}:*") or [] status = { "tasks": tasks, diff --git a/schema.yml b/schema.yml index fd025466a..9011cd628 100644 --- a/schema.yml +++ b/schema.yml @@ -18940,10 +18940,43 @@ paths: schema: $ref: '#/components/schemas/GenericError' description: '' - /sources/ldap/{slug}/sync_status/: + /sources/ldap/{slug}/sync/: get: - operationId: sources_ldap_sync_status_retrieve - description: Get source's sync status + operationId: sources_ldap_sync_retrieve + description: Get source's sync status or start source sync + parameters: + - in: path + name: slug + schema: + type: string + description: Internal source name, used in URLs. + required: true + tags: + - sources + security: + - authentik: [] + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/LDAPSyncStatus' + description: '' + '400': + content: + application/json: + schema: + $ref: '#/components/schemas/ValidationError' + description: '' + '403': + content: + application/json: + schema: + $ref: '#/components/schemas/GenericError' + description: '' + post: + operationId: sources_ldap_sync_create + description: Get source's sync status or start source sync parameters: - in: path name: slug diff --git a/web/src/admin/sources/ldap/LDAPSourceViewPage.ts b/web/src/admin/sources/ldap/LDAPSourceViewPage.ts index 6ac64c14b..b8ac6b3c4 100644 --- a/web/src/admin/sources/ldap/LDAPSourceViewPage.ts +++ b/web/src/admin/sources/ldap/LDAPSourceViewPage.ts @@ -100,7 +100,7 @@ export class LDAPSourceViewPage extends AKElement { load(): void { new SourcesApi(DEFAULT_CONFIG) - .sourcesLdapSyncStatusRetrieve({ + .sourcesLdapSyncRetrieve({ slug: this.source.slug, }) .then((state) => { @@ -198,9 +198,8 @@ export class LDAPSourceViewPage extends AKElement { ?disabled=${this.syncState?.isRunning} .apiRequest=${() => { return new SourcesApi(DEFAULT_CONFIG) - .sourcesLdapPartialUpdate({ + .sourcesLdapSyncCreate({ slug: this.source?.slug || "", - patchedLDAPSourceRequest: this.source, }) .then(() => { this.dispatchEvent(