Skip to content

Commit

Permalink
Make some configuration for allocation plugins optional
Browse files Browse the repository at this point in the history
  • Loading branch information
DanThrane committed Dec 2, 2022
1 parent 9c36b8d commit 31f71fb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ data class ConfigSchema(
) : ConfigSchema.Plugins.Allocations() {
@Serializable
data class Extensions(
val onAllocationTotal: String,
val onAllocationSingle: String,
val onSynchronizationTotal: String,
val onSynchronizationSingle: String,
val onAllocationTotal: String? = null,
val onAllocationSingle: String? = null,
val onSynchronizationTotal: String? = null,
val onSynchronizationSingle: String? = null,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,7 @@ suspend fun <Req, Resp> TypedExtensionWithExecutable<Req, Resp>?.optionalInvoke(
): Resp? =
this?.invoke(context, request)

suspend fun <Req, Resp> TypedExtension<Req, Resp>.optionalInvoke(context: PluginContext, exe: String?, req: Req): Resp? =
if (exe != null) this.invoke(context, exe, req) else null

class ExtensionException(message: String) : RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,30 @@ class ExtensionAllocationPlugin : AllocationPlugin {
override suspend fun PluginContext.onResourceAllocationTotal(
notifications: List<AllocationNotificationTotal>
): List<OnResourceAllocationResult> {
val results = ArrayList<OnResourceAllocationResult>()
for (notification in notifications) {
results.add(onAllocationTotal.invoke(this, pluginConfig.extensions.onAllocationTotal, notification))
onAllocationTotal.optionalInvoke(this, pluginConfig.extensions.onAllocationTotal, notification)
}
return results
return notifications.map { OnResourceAllocationResult.ManageThroughUCloud }
}

override suspend fun PluginContext.onResourceAllocationSingle(
notifications: List<AllocationNotificationSingle>
): List<OnResourceAllocationResult> {
val results = ArrayList<OnResourceAllocationResult>()
for (notification in notifications) {
results.add(onAllocationSingle.invoke(this, pluginConfig.extensions.onAllocationSingle, notification))
onAllocationSingle.optionalInvoke(this, pluginConfig.extensions.onAllocationSingle, notification)
}
return results
return notifications.map { OnResourceAllocationResult.ManageThroughUCloud }
}

override suspend fun PluginContext.onResourceSynchronizationTotal(notifications: List<AllocationNotificationTotal>) {
for (notification in notifications) {
onSynchronizationTotal.invoke(this, pluginConfig.extensions.onSynchronizationTotal, notification)
onSynchronizationTotal.optionalInvoke(this, pluginConfig.extensions.onSynchronizationTotal, notification)
}
}

override suspend fun PluginContext.onResourceSynchronizationSingle(notifications: List<AllocationNotificationSingle>) {
for (notification in notifications) {
onSynchronizationSingle.invoke(this, pluginConfig.extensions.onSynchronizationSingle, notification)
onSynchronizationSingle.optionalInvoke(this, pluginConfig.extensions.onSynchronizationSingle, notification)
}
}

Expand Down

0 comments on commit 31f71fb

Please sign in to comment.