add API to trigger sync

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens Langhammer 2023-11-13 14:35:48 +01:00
parent e0355b13cd
commit 3835734ed4
No known key found for this signature in database
3 changed files with 47 additions and 11 deletions

View File

@ -138,14 +138,18 @@ class LDAPSourceViewSet(UsedByMixin, ModelViewSet):
ordering = ["name"] ordering = ["name"]
@extend_schema( @extend_schema(
request=None,
responses={ responses={
200: LDAPSyncStatusSerializer(), 200: LDAPSyncStatusSerializer(),
} },
) )
@action(methods=["GET"], detail=True, pagination_class=None, filter_backends=[]) @action(methods=["GET", "POST"], detail=True, pagination_class=None, filter_backends=[])
def sync_status(self, request: Request, slug: str) -> Response: def sync(self, request: Request, slug: str) -> Response:
"""Get source's sync status""" """Get source's sync status or start source sync"""
source: LDAPSource = self.get_object() 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 [] tasks = TaskInfo.by_name(f"ldap_sync:{source.slug}:*") or []
status = { status = {
"tasks": tasks, "tasks": tasks,

View File

@ -18940,10 +18940,43 @@ paths:
schema: schema:
$ref: '#/components/schemas/GenericError' $ref: '#/components/schemas/GenericError'
description: '' description: ''
/sources/ldap/{slug}/sync_status/: /sources/ldap/{slug}/sync/:
get: get:
operationId: sources_ldap_sync_status_retrieve operationId: sources_ldap_sync_retrieve
description: Get source's sync status 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: parameters:
- in: path - in: path
name: slug name: slug

View File

@ -100,7 +100,7 @@ export class LDAPSourceViewPage extends AKElement {
load(): void { load(): void {
new SourcesApi(DEFAULT_CONFIG) new SourcesApi(DEFAULT_CONFIG)
.sourcesLdapSyncStatusRetrieve({ .sourcesLdapSyncRetrieve({
slug: this.source.slug, slug: this.source.slug,
}) })
.then((state) => { .then((state) => {
@ -198,9 +198,8 @@ export class LDAPSourceViewPage extends AKElement {
?disabled=${this.syncState?.isRunning} ?disabled=${this.syncState?.isRunning}
.apiRequest=${() => { .apiRequest=${() => {
return new SourcesApi(DEFAULT_CONFIG) return new SourcesApi(DEFAULT_CONFIG)
.sourcesLdapPartialUpdate({ .sourcesLdapSyncCreate({
slug: this.source?.slug || "", slug: this.source?.slug || "",
patchedLDAPSourceRequest: this.source,
}) })
.then(() => { .then(() => {
this.dispatchEvent( this.dispatchEvent(