1Config::Model::IdElemenUtsReerfeCroenntcrei(b3u)ted PerlCoDnofciugm:e:nMtoadteilo:n:IdElementReference(3)
2
3
4
6 Config::Model::IdElementReference - Refer to id element(s) and extract
7 keys
8
10 version 2.140
11
13 # synopsis shows an example of model of a network to use references
14
15 use Config::Model;
16
17 my $model = Config::Model->new;
18
19 # model of several hosts with several NICs
20 $model->create_config_class(
21 name => 'Host',
22 'element' => [
23 ip_nic => {
24 type => 'hash',
25 index_type => 'string',
26 cargo => {
27 type => 'leaf',
28 value_type => 'uniline',
29 }
30 },
31 ]
32 );
33
34 # model to choose a master host and a master NIC (whatever that may be)
35 # among configured hosts. Once these 2 are configured, the model computes
36 # the master IP
37
38 $model->create_config_class(
39 name => "MyNetwork",
40
41 element => [
42 host => {
43 type => 'hash',
44 index_type => 'string',
45 cargo => {
46 type => 'node',
47 config_class_name => 'Host'
48 },
49 },
50
51 # master_host is one of the configured hosts
52 master_host => {
53 type => 'leaf',
54 value_type => 'reference', # provided by tConfig::Model::IdElementReference
55 refer_to => '! host'
56 },
57
58 # master_nic is one NIC of the master host
59 master_nic => {
60 type => 'leaf',
61 value_type => 'reference', # provided by tConfig::Model::IdElementReference
62 computed_refer_to => { # provided by Config::Model::ValueComputer
63 formula => ' ! host:$h ip_nic ',
64 variables => { h => '- master_host' }
65 }
66 },
67
68 # provided by Config::Model::ValueComputer
69 master_ip => {
70 type => 'leaf',
71 value_type => 'string',
72 compute => {
73 formula => '$ip',
74 variables => {
75 h => '- master_host',
76 nic => '- master_nic',
77 ip => '! host:$h ip_nic:$nic'
78 }
79 }
80 },
81
82 ],
83 );
84
85 my $inst = $model->instance(root_class_name => 'MyNetwork' );
86
87 my $root = $inst->config_root ;
88
89 # configure hosts on my network
90 my $steps = 'host:foo ip_nic:eth0=192.168.0.1 ip_nic:eth1=192.168.1.1 -
91 host:bar ip_nic:eth0=192.168.0.2 ip_nic:eth1=192.168.1.2 -
92 host:baz ip_nic:eth0=192.168.0.3 ip_nic:eth1=192.168.1.3 ';
93 $root->load( steps => $steps );
94
95 print "master host can be one of ",
96 join(' ',$root->fetch_element('master_host')->get_choice),"\n" ;
97 # prints: master host can be one of bar baz foo
98
99 # choose master host
100 $root->load('master_host=bar') ;
101
102 print "master NIC of master host can be one of ",
103 join(' ',$root->fetch_element('master_nic')->get_choice),"\n" ;
104 # prints: master NIC of master host can be one of eth0 eth1
105
106 # choose master nic
107 $root->load('master_nic=eth1') ;
108
109 # check what is the master IP computed by the model
110 print "master IP is ",$root->grab_value('master_ip'),"\n";
111 # prints master IP is 192.168.1.2
112
114 This class is user by Config::Model::Value to set up an enumerated
115 value where the possible choice depends on the key of a
116 Config::Model::HashId or the content of a Config::Model::ListId object.
117
118 This class is also used by Config::Model::CheckList to define the
119 checklist items from the keys of another hash (or content of a list).
120
122 Construction is handled by the calling object (Config::Model::Node).
123
125 refer_to
126 "refer_to" is used to specify a hash element that is used as a
127 reference. "refer_to" points to an array or hash element in the
128 configuration tree using the path syntax (See "grab" in
129 Config::Model::Role::Grab for details).
130
131 computed_refer_to
132 When "computed_refer_to" is used, the path is computed using values
133 from several elements in the configuration tree.
134 "computed_refer_to" is a hash with 2 mandatory elements: "formula"
135 and "variables".
136
137 The available choice of this (computed or not) reference value is made
138 from the available keys of the "referred_to" hash element or the values
139 of the "referred_to" array element.
140
141 The example means the the value must correspond to an existing host:
142
143 value_type => 'reference',
144 refer_to => '! host'
145
146 This example means the the value must correspond to an existing lan
147 within the host whose Id is specified by hostname:
148
149 value_type => 'reference',
150 computed_refer_to => {
151 formula => '! host:$a lan',
152 variables => { a => '- hostname' }
153 }
154
155 If you need to combine possibilities from several hash, use the ""+""
156 token to separate 2 paths:
157
158 value_type => 'reference',
159 computed_refer_to => {
160 formula => '! host:$a lan + ! host:foobar lan',
161 variables => { a => '- hostname' }
162 }
163
164 You can specify "refer_to" or "computed_refer_to" with a "choice"
165 argument so the possible enum value will be the combination of the
166 specified choice and the referred_to values.
167
169 reference_info
170 Returns a human readable string with explains how is retrieved the
171 reference. This method is mostly used to construct an error messages.
172
174 Dominique Dumont, (ddumont at cpan dot org)
175
177 Config::Model, Config::Model::Value, Config::Model::AnyId,
178 Config::Model::CheckList
179
181 Dominique Dumont
182
184 This software is Copyright (c) 2005-2020 by Dominique Dumont.
185
186 This is free software, licensed under:
187
188 The GNU Lesser General Public License, Version 2.1, February 1999
189
190
191
192perl v5.32.0 2020-08-0C2onfig::Model::IdElementReference(3)