On Jun 4, 2010, at 3:45 AM, David Schmitt wrote:

Hi all,

a recurring problem with the namevars are resources that do not have a natural unique single-property key. For example, mysql users are defined by their username and the host(-pattern) they are connection from.

Traditionally, this was solved by ugly hackery in the type. In the windows session at the puppetcamp, Markus and I developed the idea of associating a set of patterns/syntaxes with the namevar to automatically parse such multi-key titles and automatically put them into proper parameters, which can then be validated, munged and used without needing to know about the actual syntax elsewhere in the type/provider.

It's definitely long-past time to solve this, and I'm glad it's finally being worked on.

I'll start with an example, of how this could look like:

 Puppet::Type.newtype(:mysql_user) do
   @doc = "Manage a mysql database user."

   ensurable

   newtitle do
     pattern "([...@]+)@([...@]+)", :user, :host
     pattern "([...@]+)", :user
   end

   newparam(:user) do
     desc "The username."
   end

   newparam(:host) do
     desc "The host, the users connects from. This may be a pattern
           using '%' and '_'."
     defaultto "%"
   end

Patterns would be tried in order and match groups assigned to the specified parameters. Having no pattern match woul


This could be used in the following ways:

 mysql_user {
   "foo": ;
   "f...@%": ;   # both 'f...@%'
   "bar": host => "localhost";
   "b...@localhost": ; # both 'b...@localhost'
 }

 Mysql_user { host => "webserver" }
 mysql_user { [ "frob", "frub" ]: }
 # expands to 'f...@webserver' and 'f...@webserver'

 mysql_user {
   "frob": host => "web1";
   "frob": host => "web2";
   "frob": host => "web3";
 }

What's the reason for not just requiring full parameter specification, rather than supporting the title short-hand?

It seems to me we kind of have two problems: How to uniquely specify the resource with parameters, and how to specify it with a resource reference. Your solution (the title becomes a template filled in by the different parameters) does kind of neatly solve it, but I'm afraid it might be too complicated for normal humans to use.

The last stanza shows how this can be used to confusing effects. This could be mitigated by allowing using arrays in parameters. For example creating four users:

 mysql_user {
   [ "frob", "frub" ]:
     host => [ "web1", "web2" ]
 }

Here are some illegal constructs:

 # Setting a property twice
 mysql_user {
   "foo": user => "bar";
   "f...@%": host => "localhost";
 }

 # redefine resources
 mysql_user {
   "frob": ;
   "f...@%": ;

   "foo": host => "localhost";
   "f...@localhost": ;
 }

 # pattern match fails:
 mysql_user {
   [ "", "@", "foo@", "@localhost", "f...@bar@localhost" ]: ;
 }

For more flexibility when parsing, the list-of-paramnames could be replaced by blocks:

 pattern "complicated_regexp" do
   # insert disgusting hack here
 end

The block would have to return true or false to accept or reject the title.

By substituting the param names into the patterns, puppet could automatically create documentation like this:

 Patterns:
   1) u...@host:
     user must be [...@]+
     host must be [...@]+
   2) user:
     user must be [...@]+
 user: The username.
 host: The host, the users connects from. This may be a pattern using
       '%' and '_'.

This could be enhanced by specifying additional docstrings with the patterns.


Of course, this is currently vapour-ware, as it'll need some changes in the puppet core to handle the new uniqueness, but I hope to accelerate the development by providing a good spec/discussion of how the "user-facing" side should look like.

It'd probably be a good idea to run through three or four other resource types with this to see how it works. E.g., ports (/etc/ services), packages (optionally specify a version number or architecture, where two packages with the same name but different version/arch are considered different packages depending on the provider, I think), and probably something else.

--
Trying to determine what is going on in the world by reading
newspapers is like trying to tell the time by watching the second
hand of a clock. --Ben Hecht
---------------------------------------------------------------------
Luke Kanies  -|-   http://puppetlabs.com   -|-   +1(615)594-8199

--
You received this message because you are subscribed to the Google Groups "Puppet 
Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to