Lazy execution outside of resource

Hi everyone,

I have some libraries that talk to remote APIs, and based on the APIs’ results, a resource runs or not by using not_if/only_if guards (which are executed lazily).

I have multiple resources in the same recipe that need the API response; instead of having, for example, four resources with guards, it’d be great to be able to poll the API once at converge time, and simply do an if/else.

To be clear:

What I have now:

resource A do
  ...
  only_if { Consulite.first_service(service_name).Address == node['ipaddress'] }
end

resource B do
  ...
  only_if { Consulite.first_service(service_name).Address == node['ipaddress'] }
end

resource C do
  ...
  not_if { Consulite.first_service(service_name).Address == node['ipaddress'] }
end

resource D do
  ...
  not_if { Consulite.first_service(service_name).Address == node['ipaddress'] }
end

What I would like to have:

var = lazy { Consulite.first_service(service_name).Address == node['ipaddress'] }
if var
  resource A do
    ...
  end

  resource B do
    ...
  end
else
  resource C do
    ...
  end

  resource D do
    ...
  end
end

Is it possible to achieve something like this?

Thanks,
Ameir

Sort of. Notifies is perhaps the easiest way to do this.

dumb example:

file '/var/tmp/resource_run' do
  action [:create, :touch]
  not_if { Consulite.first_service(service_name).Address == node['ipaddress'] }
  notifies :run 'resource[a]', :delayed
  notifies :run 'resource[b]', :immediately
end

resource a do
  ...
  action :nothing
end

resource b do
  ...
  action :nothing
end

(substitute :run for whichever action you want in your resource)