Config::Model::BackendMgr(3pm)

NAME
Config::Model::BackendMgr - Load configuration node on demand

VERSION
version 2.165

SYNOPSIS
 # Use BackendMgr to write data in Yaml file
 # This example requires Config::Model::Backend::Yaml which is now
 # shipped outside of Config::Model. Please get it on CPAN
 use Config::Model;

 # define configuration tree object
 my $model = Config::Model->new;
 $model->create_config_class(
    name    => "Foo",
    element => [
      'foo' => {
        'value_type' => 'string',
        'type' => 'leaf'
      },
      'bar' => '*foo'
    ]

 );

 $model->create_config_class(
    name => "MyClass",

    # rw_config spec is used by Config::Model::BackendMgr
    rw_config => {
        backend     => 'yaml',
        config_dir  => '/tmp/',
        file        => 'my_class.yml',
        auto_create => 1,
    },

    element => [
      'foo' => {
        'value_type' => 'string',
        'type' => 'leaf'
      },
      'bar' => '*foo'
      hash_of_nodes => {
          type       => 'hash',     # hash id
          index_type => 'string',
          cargo      => {
              type              => 'node',
              config_class_name => 'Foo'
          },
      },
    ],
 );

 my $inst = $model->instance( root_class_name => 'MyClass' );

 my $root = $inst->config_root;

 # put data
 my $steps = 'foo=FOO hash_of_nodes:fr foo=bonjour -
   hash_of_nodes:en foo=hello ';
 $root->load( steps => $steps );

 $inst->write_back;

 # now look at file /tmp/my_class.yml

DESCRIPTION
This class provides a way to specify how to load or store configuration data within the model.

With these specifications, all configuration information is read during creation of a node (which triggers the creation of a backend manager object) and written back when write_back method is called either on the instance.

This load/store can be done with different backends:

•   Any of the "Config::Model::Backend::*" classes available on your system. For instance "Config::Model::Backend::Yaml".

•   "cds_file": Config dump string (cds) in a file. I.e. a string that describes the content of a configuration tree is loaded from or saved in a text file. This format is defined by this project. See "load string syntax" in Config::Model::Loader.

•   "perl_file": Perl data structure (perl) in a file. See Config::Model::DumpAsData for details on the data structure. Now handled by Config::Model::Backend::PerlFile

When needed, "write_back" method can be called on the instance (See Config::Model::Instance) to store back all configuration information.

Backend specification
The backend specification is provided as an attribute of a Config::Model::Node specification. These attributes are optional: A node without "rw_config" attribute must rely on another node to read or save its data.

When needed (usually for the root node), the configuration class is declared with a "rw_config" parameter which specifies the read/write backend configuration.

  Parameters available for all backends
The following parameters are accepted by all backends:

config_dir
    Specify configuration directory. This parameter is optional as the directory can be hardcoded in the backend class. "config_dir" beginning with '"~"' is munged so "~" is replaced by "File::HomeDir->my_data". See File::HomeDir for details.

file
    Specify configuration file name (without the path). This parameter is optional as the file name can be hardcoded in the backend class.

    The configuration file name can be specified with &index keyword when a backend is associated to a node contained in a hash. For instance, with "file" set to "&index.conf":

     service    # hash element
       foo      # hash index
         nodeA  # values of nodeA are stored in foo.conf
       bar      # hash index
         nodeB  # values of nodeB are  stored in bar.conf

    Likewise, the keyword &element can be used to specify the file name. For instance, with "file" set to "&element-&index.conf":

     service    # hash element
       foo      # hash index
         nodeA  # values of nodeA are stored in service.foo.conf
       bar      # hash index
         nodeB  # values of nodeB are  stored in service.bar.conf

file_mode
    "file_mode" parameter can be used to set the mode of the written file(s). "file_mode" value can be in any form supported by "chmod" in Path::Tiny. Example:

      file_mode => 0664,
      file_mode => '0664',
      file_mode => 'g+w'

os_config_dir
    Specify alternate location of a configuration directory depending on the OS (as returned by $^O, see "PLATFORMS" in perlport). For instance:

     config_dir => '/etc/ssh',
     os_config_dir => { darwin => '/etc' }

default_layer
    Optional. Specifies where to find a global configuration file that specifies default values. For instance, this is used by OpenSSH to specify a global configuration file ("/etc/ssh/ssh_config") that is overridden by user's file:

     default_layer => {
        os_config_dir => { 'darwin' => '/etc' },
        config_dir    => '/etc/ssh',
        file          => 'ssh_config'
     }

    Only the 3 above parameters can be specified in "default_layer".

auto_create
    By default, an exception is thrown if no read was successful. This behavior can be overridden by specifying "auto_create => 1" in one of the backend specification. For instance:

     rw_config  => {
         backend => 'IniFile',
         config_dir => '/tmp',
         file  => 'foo.conf',
         auto_create => 1
     },

    Setting "auto_create" to 1 is necessary to create a configuration from scratch

auto_delete
    Delete configuration files that contains no data. (default is to leave an empty file)

  Config::Model::Backend::* backends
Specify the backend name and the parameters of the backend defined in their documentation.

For instance:

 rw_config => {
     backend     => 'yaml',
     config_dir  => '/tmp/',
     file        => 'my_class.yml',
 },

See Config::Model::Backend::Yaml for more details for this backend.

  Your own backend
You can also write a dedicated backend. See How to write your own backend for details.

Test setup
By default, configurations files are read from the directory specified by "config_dir" parameter specified in the model. You may override the "root" directory for test.

Methods

  support_annotation
Returns 1 if at least the backend supports read and write annotations (aka comments) in the configuration file.

AUTHOR
Dominique Dumont, (ddumont at cpan dot org)

SEE ALSO
Config::Model, Config::Model::Instance, Config::Model::Node, Config::Model::Dumper

AUTHOR
Dominique Dumont

COPYRIGHT AND LICENSE
This software is Copyright (c) 2005-2022 by Dominique Dumont.

This is free software, licensed under:

  The GNU Lesser General Public License, Version 2.1, February 1999