Plugin to validate host parameters


I’m trying to build a plugin to validate host parameters, based on

I’m using a couple of special host parameters on install to facilitate integration with other systems via foreman-hooks. One such example is a parameter to hold the ticket number from our ticketing system. I would like to validate this parameter but my ruby skills cant figure out how to do it. Any help would be appreciated!

I can access the parameter value the same way I access it in templates:

ticket = params['provision-ticket']

(where ‘provision-ticket’ is the name of my host parameter).

But I cant figure out how to reference this parameter so it is marked red in the UI. In the original plugin, this line is used:
errors.add(:name, _('must match regex /%s/') % validate_name_regex) unless shortname =~ /#{validate_name_regex}/

where I guess the symbol ‘:name’ makes the plugin magically signal the UI to mark that form red if validation fails. What do I use instead of ‘:name’ when validating host parameters?

The parameter is represented like this in an api call to /api/hosts:

  "parameters": [
      "priority": 70,
      "created_at": "2017-09-22 09:52:23 UTC",
      "updated_at": "2017-09-22 09:52:23 UTC",
      "id": 606,
      "name": "provision-ticket",
      "value": "cXXXXX"

Any pointers?



errors.add(:name, msg)

adds a validation error to a name attribute, UI picks up on that and then displays the attributes with errors in red. In the case of foreman_host_extra_validator, the validation is added to the name attribute of a host.

If you want to validate parameters, then you need to define your validations in a module:

module MyPlugin
  module HostParameterExtensions
    extend ActiveSupport::Concern

    included do
      validate :my_validation

    def my_validation
      errors.add(:value, 'Failed validation') if name == 'provision-ticket' && some_condition_here

then you need to extend the HostParameter model with your module the same way foreman_host_extra_validator does it with host

It is difficult to troubleshoot this kind of problem without seeing the actual code, but this is a general approach how to add validations to a core model from a plugin.

Hope it helps,


Thank you very much for your pointers! Plugin development is a bit overwhelming when your not very familiar with ruby, and I really appreciate the help. Will do some testing.