Methods type promises are powerful abstraction tools in CFEngine 3. Methods allow you to activate bundles (optionally parametrized) from other bundles. This allows encapsulation of knowledge and lends itself to re-usability.

I just want to share an example of calling bundles dynamically. It’s a contrived example, but I thought it was neat so here it is.

R: in bundle agent handler1: I got value1
R: in bundle agent handler1: I got value2
 !! Method invoked repairs
 !! Method invoked repairs
R: in bundle agent handler2: I got value1
R: in bundle agent handler2: I got value2
 !! Method invoked repairs
 !! Method invoked repairs

  <div class="gist-meta">
    <a href="https://gist.github.com/raw/3901453/0b683269921c056b4e821dd2c63ca6b622122e26/output" style="float:right;">view raw</a> <a href="https://gist.github.com/3901453#file_output" style="float:right;margin-right:10px;color:#666">output</a> <a href="https://gist.github.com/3901453">This Gist</a> is brought to you using <a href="http://en.bainternet.info/2011/simple-gist-embed"><small>Simple Gist Embed</small></a>.
  </div>
</div>

<div class="gist-file">
  <div class="gist-data gist-syntax">
    <div class="highlight">
      <pre><div class='line' id='LC1'>

body common control {


    bundlesequence => {"main",};

}

bundle agent main{
vars:
  "mybundles" slist => { "handler1","handler2" };
  "myvalues" slist => { "value1", "value2" };

methods:
  "$(mybundle)" usebundle => handler_iterator("$(mybundles)", "@(main.myvalues)");

}

bundle agent handler_iterator(handler, values)
# This expects a single value
{
methods:
  "$(handler)" usebundle => $(handler)("@(handler_iterator.values)");

}
bundle agent handler1(value1)
{
reports:
  cfengine::
    "in bundle agent handler1: I got $(value1)";
}

bundle agent handler2(value1)
{
reports:
  cfengine::
  "in bundle agent handler2: I got $(value1)";
}

  <div class="gist-meta">
    <a href="https://gist.github.com/raw/3901453/67c8bda449460da4d5b8c1983d5b0bbaacc63b13/test.cf" style="float:right;">view raw</a> <a href="https://gist.github.com/3901453#file_test.cf" style="float:right;margin-right:10px;color:#666">test.cf</a> <a href="https://gist.github.com/3901453">This Gist</a> is brought to you using <a href="http://en.bainternet.info/2011/simple-gist-embed"><small>Simple Gist Embed</small></a>.
  </div>
</div>