VirtusLab / besom

Besom - a Pulumi SDK for Scala. Also, incidentally, a broom made of twigs tied round a stick. Brooms and besoms are used for protection, to ward off evil spirits, and cleansing of ritual spaces.
https://virtuslab.github.io/besom/
Apache License 2.0
123 stars 7 forks source link

Add resource lifecycle hooks #420

Open lbialy opened 6 months ago

lbialy commented 6 months ago

For example:

  extension [A <: besom.internal.Resource](outputOfResource: Output[A])
    def afterCreate(callback: A => Output[Unit]): Output[A] =
      for
        resource <- outputOfResource
        // make sure we run the callback after URN is resolved on a separate fork
        // CHECK THAT TASK TRACKER IS TRACKING THIS FORK
        _        <- (resource.urn *> callback(resource)).getData.fork 
      yield resource

This allows user to run code after a resource was actually created (by hijacking URN Promise resolution). Useful for execution of postconditions and unrelated tasks that have to happen ONLY for said resource, integrations for instance.

Separate fork is necessary to prevent pollution of main thread (that declares resources) with dynamic Output that will short-circuit on empty during dry-run and remove subsequent resources from the plan.

Use cases: