Re: Is it possible to access type params in another type?

47 views
Skip to first unread message

jcbollinger

unread,
Jun 26, 2012, 3:40:32 PM6/26/12
to puppet...@googlegroups.com


On Tuesday, June 26, 2012 6:07:41 AM UTC-5, Yanis Guenane wrote:
Hey guys,

I was wondering if it was possible to access type params within another type.

Here is an example of what I would like to do.

Puppet::Type.newtype(:a) do
     newparam(:name) do
      isnamevar
     end
      newparam(:path) do
      end
end

Puppet::Type.newtype(:b) do
   newparam(:name) do
    isnamevar
   end
   newparam(:needs) do
 
   /* I want to access path param from 'a' type here
      something like resource[:needs].path */

   end
end

And the manifest calling that would be

 a {'test-a' :
path => '/usr',
}

 b {'test-b' :
needs => a
}
 


Is it possible ?


What is the example supposed to mean?  It doesn't make sense.  As a best guess, what you actually want to do is access the parameter values of a resource instance, not (as you wrote) the parameter[ definition]s of a resource type.  It might be possible to do that (involving somewhat different manifest syntax), but it would be decidedly non-idiomatic.

Why do you want this?  Your example is too abstract to tell me much.


John

Yanis Guenane

unread,
Jun 27, 2012, 1:27:17 AM6/27/12
to puppet...@googlegroups.com
What I actually would like it to create a kind of dependency.

I would like that a parameter X of any instance of  type 'b' depends on the path param of the 'a' resource instance namely passed as a parameter.

file {'test' :
 ensure => present,
 name => 'test.txt',
 within => dir,
}

file {'dir' :
 ensure => directory,
 name => 'dir',
 path => '/tmp'
}

This is not the best example since file does not work this way but its the closest one to what I would like.

Imagine file was a custom type, so I know in the within param I am expecting a File[] resource insance. I'd like to know if there is any way I can get the parameters of this resource instance (dir)? (Something like extrapolating it, or accessing it through catalog)

In the previous case File['test'] will end up being located at '/tmp/text.txt', appending dir.path + test.name, without specifying the path in File['test'] but just the directory it depends on.

I hope I could explain myself a bit better,

jcbollinger

unread,
Jun 27, 2012, 10:39:36 AM6/27/12
to puppet...@googlegroups.com


On Wednesday, June 27, 2012 12:27:17 AM UTC-5, Yanis Guenane wrote:
What I actually would like it to create a kind of dependency.

I would like that a parameter X of any instance of  type 'b' depends on the path param of the 'a' resource instance namely passed as a parameter.

file {'test' :
 ensure => present,
 name => 'test.txt',
 within => dir,
}

file {'dir' :
 ensure => directory,
 name => 'dir',
 path => '/tmp'
}

This is not the best example since file does not work this way but its the closest one to what I would like.

Imagine file was a custom type, so I know in the within param I am expecting a File[] resource insance. I'd like to know if there is any way I can get the parameters of this resource instance (dir)? (Something like extrapolating it, or accessing it through catalog)

In the previous case File['test'] will end up being located at '/tmp/text.txt', appending dir.path + test.name, without specifying the path in File['test'] but just the directory it depends on.

I hope I could explain myself a bit better,

Yes, you did, and that's pretty much what I had supposed you were after.  Chances are good that you could force this to work, but I cannot advise you about details, and I recommend you do not follow this path.  Nothing else in Puppet works the way you describe.

More generally, despite appearances, implementation language, and some of the terminology used in the Puppet community, Puppet DSL is not an object-oriented language in any conventional sense, but that's how you want to make it behave (albeit only within the confines of your custom type).  The fact that you are trying to do something so non-idiomatic should give you pause.

The usual ways to approach this sort of problem all involve making multiple classes and / or resources pull data from the same source, typically one of global or class variables, defined type parameters, an external data repository, or class parameters.  For example, if the two associated resources are going to be declared together, then you could do this:

define my_module::file_and_directory($filename, $dir) {
  file {$dir :
   ensure => directory,
  }

  file {"${dir}/${filename}":
   ensure => present,
  }
}

my_module::file_and_directory { "foo":
  filename => 'file',
  dir => '/tmp'
}


John

Yanis Guenane

unread,
Jun 28, 2012, 6:19:28 AM6/28/12
to puppet...@googlegroups.com
John, thanks a lot for your answers.

I was doing it this way at first, but I thought that providing native type would be much simpler for end users.

The same way puppet is providing native types for Nagios I wanted to create native type for the application I need.

Anyway I'll get back to defined type and class ;)

Thanks again,

Steve Traylen

unread,
Jun 28, 2012, 2:52:49 PM6/28/12
to puppet...@googlegroups.com
Hi Yanis,
 http://projects.puppetlabs.com/issues/10166 describes a unproven technique for catalog lookup from 
 a provider to access another type.
Steve,
 
Reply all
Reply to author
Forward
0 new messages