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"]
@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,

View File

@ -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

View File

@ -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(