Could you transform this into a puppet function ?

This way the same function could be proposed into puppetlabs-stdlib (or any other more network-related module) …