The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Config::Model::Node - Class for configuration tree node

SYNOPSIS

 $model->create_config_class 
  (
   name              => 'OneConfigClass',
   element           => [
                          [qw/X Y Z/] 
                          => {
                               type => 'leaf',
                               value_type => 'enum',
                               choice     => [qw/Av Bv Cv/]
                             }
                        ],
   permission        => [ Y => 'intermediate', 
                          X => 'master' 
                        ],
   status            => [ X => 'deprecated' ],
   description       => [ X => 'X-ray' ],
   class_description => "OneConfigClass detailed description",

  );

 my $instance = $model->instance (root_class_name => 'OneConfigClass', 
                                  instance_name => 'test1');
 my $root_node = $instance -> config_root ;

DESCRIPTION

This class provides the nodes of a configuration tree. When created, a node object will get a set of rules that will define its properties within the configuration tree.

Each node contain a set of elements. An element can contain:

  • A leaf element implemented with Config::Model::Value. A leaf can be plain (unconstrained value) or be strongly typed (values are checked against a set of rules).

  • Another node.

  • A collection of items: a list element, implemented with Config::Model::ListId. Each item can be another node or a leaf.

  • A collection of identified items: a hash element, implemented with Config::Model::HashId. Each item can be another node or a leaf.

Configuration class declaration

A class declaration is made of the following parameters:

name

Mandatory string parameter. This config class name can be used by a node element in another configuration class.

class_description

Optional string parameter. This description will be used when generating user interfaces.

element

Mandatory list ref of elements of the configuration class :

  element => [ foo => { type = 'leaf', ... },
               bar => { type = 'leaf', ... }
             ]

Element names can be grouped to save typing:

  element => [ [qw/foo bar/] => { type = 'leaf', ... } ]

See below for details on element declaration.

permission

Optional list ref of the elements whose permission are different from default value (intermediate). Possible values are master, advanced and intermediate.

  permission   => [ Y => 'intermediate', 
                    [qw/foo bar/] => 'master' 
                  ],
level

Optional list ref of the elements whose level are different from default value (normal). Possible values are important, normal or hidden.

The level is used to set how configuration data is presented to the user in browsing mode. Important elements will be shown to the user no matter what. hidden elements will be explained with the warp notion.

  level  => [ [qw/X Y/] => 'important' ]
status

Optional list ref of the elements whose status are different from default value (standard). Possible values are obsolete, deprecated or standard.

Using a deprecated element will issue a warning. Using an obsolete element will raise an exception (See Config::Model::Exception.

  status  => [ [qw/X Y/] => 'obsolete' ]
description

Optional list ref of element description. These descriptions will be used when generating user interfaces.

Element declaration

Element type

Each element is declared with a list ref that contains all necessary informations:

  element => [ 
               foo => { ... }
             ]

This most important informations from this hash ref is the mandatory type parameter. The type type can be:

node

The element is a simple node of a tree instanciated from a configuration class (declared with "create_config_class( ... )"). See "Node element".

warped_node

The element is a node whose properties (mostly config_class_name) can be changed (warped) according to the values of one or more leaf elements in the configuration tree. See Config::Model::WarpedNode for details.

leaf

The element is a scalar value. See "Leaf element"

hash

The element is a collection of nodes or values (default). Each element of this collection is identified by a string (Just like a regular hash, except that you can set up constraint of the keys). See "Hash element"

list

The element is a collection of nodes or values (default). Each element of this collection is identified by an integer (Just like a regular perl array, except that you can set up constraint of the keys). See "List element"

Node element

When declaring a node element, you must also provide a config_class_name parameter. For instance:

 $model ->create_config_class 
   (
   name => "ClassWithOneNode",
   element => [
                the_node => { 
                              type => 'node',
                              config_class_name => 'AnotherClass',
                            },
              ]
   ) ;

Locally changing properties of node element

You can provide an init_step parameter with a set of key, value pair as argument. These arguments are infact a set of targets and actions.

The targets can be elements of the node itself:

  init_step => [ 'bar' => 'Av' ] # default value of bar is Av

or elements of nodes lower in the tree:

  init_step => [ 'foo X' => 'Bv' ] # default value of X in bar is Av

You can also change the default values of several leaves:

  init_step => [ 'bar' => 'Av', 'foo X' => 'Bv'  ]

In fact, the target of init_step (bar) is retrieved by the "grab" method, so any string accepted by "grab" can be used with init_step.

The effects of init_step are applied on the target through a call to the "set" method.

The effect can be:

  • A string: In this case, the target grabed will have a default value set to the effect. I.e, the set() method will be called with (default => ... ). In other words, this feature changes locally only the default value.

  • A hash ref. In this case the content of the hash ref will be passed to the set() method. In other world, this feature changes locally all properties of the leaf. For instance, the hash ref may contain max => XXX pair to change the maximum value of the leaf target.

Leaf element

When declaring a leaf element, you must also provide a value_type parameter. See Config::Model::Value for more details.

Hash element

(with collected_type set to node) or values (default). When declaring a hash element, you must also provide a index_type parameter. See Config::Model::HashId and Config::Model::AnyId for more details.

Introspection methods

name

Returns the location of the node, or its config class name (for root node).

config_model

Returns the entire configuration model.

model

Returns the configuration model of this node.

config_class_name

Returns the configuration class name of this node.

instance

Returns the instance object containing this node. Inherited from Config::Model::AnyThing

has_element ( element_name )

Returns 1 if the class model has the element declared.

search_element ( element => <name> [, privilege => ... ] )

From this node (or warped node), search an element (respecting privilege level). Inherited from Config::Model::AnyThing.

This method returns a Config::Model::Searcher object. See Config::Model::Searcher for details on how to handle a search.

element_model ( element_name )

Returns model of the element.

element_type ( element_name )

Returns the type (e.g. leaf, hash, list or node) of the element.

element_name()

Returns the element name that contain this object. Inherited from Config::Model::AnyThing

index_value()

See "index_value()" in Config::Model::AnyThing

parent()

See "parent()" in Config::Model::AnyThing

location()

See "location()" in Config::Model::AnyThing

Element property management

get_element_name ( for => <permission> )

Return all elements names available for permission. If no permission is specified, will return all slots available at 'master' level (I.e all elements).

Returns an array in array context, and a string (e.g. join(' ',@array)) in scalar context.

next_element ( element_name, [ permission_index ] )

This method provides a way to iterate through the elements of a node.

Returns the next element name for a given permission (default master). Returns undef if no next element is available.

get_element_property ( element => ..., property => ... )

Retrieve a property of an element.

I.e. for a model :

  permission => [ X => 'master'],
  status     => [ X => 'deprecated' ]
  element    => [ X => { ... } ]

This call will return deprecated:

  $node->get_element_property ( element => 'X', property => 'status' )

set_element_property ( element => ..., property => ... )

Set a property of an element.

reset_element_property ( element => ... )

Reset a property of an element according to the model.

Information management

fetch_element ( name [ , user_permission ])

Fetch and returns an element from a node.

If user_permission is given, this method will check that the user has enough privilege to access the element. If not, a RestrictedElement exception will be raised.

fetch_element_value ( name [ , user_permission ])

Fetch and returns the value of a leaf element from a node.

If user_permission is given, this method will check that the user has enough privilege to access the element. If not, a RestrictedElement exception will be raised.

store_element_value ( name, value [ , user_permission ])

Store a value in a leaf element from a node.

If user_permission is given, this method will check that the user has enough privilege to access the element. If not, a RestrictedElement exception will be raised.

is_element_available( name => ..., permission => ... )

Returns 1 if the element name is available for the given permission ('intermediate' by default). Returns 0 otherwise.

As a syntactic sugar, this method can be called with only one parameter:

   is_element_available( 'element_name' ) ; 

is_element_defined( element_name )

Returns 1 if the element is defined.

grab(...)

See "grab(...)" in Config::Model::AnyThing.

grab_value(...)

See "grab_value(...)" in Config::Model::AnyThing.

grab_root()

See "grab_root()" in Config::Model::AnyThing.

Serialisation

load ( step => string [, permission => ... ] )

Load configuration data from the string into the node and its siblings.

This string follows the syntax defined in Config::Model::Loader. See "load ( ... )" in Config::Model::Loader for details on parameters. permission is 'master' by default.

This method can also be called with a single parameter:

  $node->load("some data:to be=loaded");

dump_tree ( [ full_dump => 1] )

Dumps the configuration data of the node and its siblings into a string.

This string follows the syntax defined in Config::Model::Loader. The string produced by dump_tree can be passed to load.

report ()

Provides a text report on the content of the configuration below this node.

audit ()

Provides a text audit on the content of the configuration below this node. This audit will show only value different from their default value.

copy_from ( another_node_object )

Copy configuration data from another node into this node and its siblings. The copy is made in a tolerant mode where invalid data are simply discarded.

Help management

get_help ( [ element_name ] )

If called without element, returns the description of the class (Stored in class_description attribute of a node declaration).

If called with an element name, returns the description of the element (Stored in description attribute of a node declaration).

Returns undef if no description was found.

AutoRead nodes

As configuration model are getting bigger, the load time of a tree gets longer. The Config::Model::AutoRead class provides a way to load the configuration informations only when needed.

TBD

AUTHOR

Dominique Dumont, domi@komarr.grenoble.hp.com

SEE ALSO

Config::Model::Model, Config::Model::Instance, Config::Model::HashId, Config::Model::ListId, Config::Model::WarpedNode, Config::Model::Value