Create a job_invocations using the API and remote execution feature (not id)

Problem:
To create a job_invocations using the API (POST /api/job_invocations), is it not possible to directly use the template name without using the id? Reviewing the documentation, we see that it is possible using the “feature” label.

In the plugin documentation it is indicated that to register a feature label, it must be included in the plugin initializer but not much more detail is indicated, Does anyone have details on how to do it?

4.5 Plugin developer API

Plugins can leverage Remote Execution to perform a particular job on a host by defining a Remote Execution Feature and the provided inputs. The provided_inputs is a list of inputs that the feature provides to the template. The feature can be registered in the plugin initializer:

RemoteExecutionFeature.register(:reprovision, N_("Reprovision"),
                                :description => "Reprovision the host via script",
                                :provided_inputs => ["script"])

The mapping between the feature and which templates to use can be made in the UI via ‘Administer → Remote Execution Features’. Or, when seeding a template, there is a new metadata item feature, which allows to assign some template as default to the feature.

Expected outcome:
Execute job_invocations using feature label

Foreman and Proxy versions:
Foreman 3.9.1

Foreman and Proxy plugin versions:
Katello 4.11
Remote Executions 12.0.5

Distribution and version:

Other relevant data:

Correct. Hammer sort of allows you to do this by first resolving the name to the id behind the scenes.

I’m not sure if there’s anything to add apart from what is written there. Maybe it would become more clear on a concrete example. Are you developing a plugin where you’d use this?

1 Like

Thanks for your answer!

I have a scenario with several Foreman installations. In each Foreman installation, I have the same template created and I want to invoke this template by API against all Foreman installations using the template_name (in this case the feature label) because it may be the case that the template_id is not the same in all installations

From what you wrote I assume you call the APIs of each foreman instance directly, right? Would it be an option for you to switch to either using hammer or foreman-ansible-modules? Both ways should do the name-to-id resolution for you while keeping the “call all the instances from one place” model

1 Like

From what you wrote I assume you call the APIs of each foreman instance directly, right?

Correct

Would it be an option for you to switch to either using hammer or foreman-ansible-modules? Both ways should do the name-to-id resolution for you while keeping the “call all the instances from one place” model

I need to do it through API. I have a possible solution, but it would be to do it through two API calls:

  • Step 1. Call to list the templates of each installation and obtain the desired template_id
  • Step 2. With the template_id obtained in step 1, make the job_invocation api call

For this reason, I am looking at the option of using Remote Execution Features since in this way, it is possible to create a job_invocation through the API using the feature label instead of the id but I don’t know how can I add a new feature label in Foreman:

But both of these talk to the api?

As a regular user you sensibly can’t. They were never really meant to be used that way. Adding a custom one cleanly would mean either rolling your own plugin or making changes to an already existing one, which may not be be really applicable here as your usecase seems to be fairly specific.

However, if you’re feeling adventurous and you’d be willing to do something noone probably ever did before, but what could do the trick…

You can create your own feature by running foreman-rake db:console and running the following snippet in there

::RemoteExecutionFeature.register('my_custom_feature', 'My custom feature')

where my_custom_feature is the value you’d use in api, the other is a human readable label. If this succeeds, you can assign a job template to it in the ui. You’d have to do the same thing on all your instances. Just be beware that this isn’t exactly supported so ymmv

1 Like

Definitely, the use of featura_label is focused on the development of a new plugin and is something that, for now, I rule out, so I will continue working with the workaround of the double API call and we will investigate the Hammer and Ansible options you mentioned

Thanks @aruzicka for your answers :slight_smile:

I have tried the steps that you have indicated and it works, but I have realized that I still have the same problem since I have to indicate the template_id to the feature_label.
foreman-rake console
RemoteExecutionFeature.register(:reprovision, N_("Reprovision"),:description => "Reprovision the host via script",:provided_inputs => ["script"])

Anyway, I am going to continue working making the double API call but your contribution has been very helpful.

If you click the name in that table, it should take you to a form where you can assign a job template to the feature. Or am I misreading what’s missing?

1 Like

What you indicate is correct, from the UI I can assign the template as I show in the image.

Anyway, I don’t feel so adventurous and will opt for the double API call option (the first to obtain the template id by name, and the second to invoke the job indicating the template id)

thank you very much for your help! :wink: