Creating a host via the API

Hi,

I'm having trouble creating a single host via the API.

I read this http://theforeman.org/api/apidoc/hosts/create.html and tried to
create a host using the provided example.

curl -s -k https://admin:admin@puppet.lan.basjes.nl/api/hosts?format=json
-d'< THE EXACT JSON FROM THE PAGE >'

I got the following error:
{"host":{"id":null,"errors":{"name":["can't be
blank"],"environment_id":["can't be blank"],"root_pass":["should be 8
characters or more"]},"full_messages":["Name can't be blank","Environment
can't be blank","Root pass should be 8 characters or more"]}}

I interpreted this as "I forgot some settings".
So I retrieved the existing test host (virtual) using
curl -s -k
https://admin:admin@puppet.lan.basjes.nl/api/hosts/pxetest.lan.basjes.nl
and then I did a
curl -s -k https://admin:admin@puppet.lan.basjes.nl/api/hosts?format=json
-d'< THE EXACT JSON I GTO FROM THE PREVIOUS COMMAND >'

Resulting in the exact same error.

There is one thing I noticed that is different: All the ids for domains,
environments, architectures, … on my test installation here are '1', '2',
'3', etc. instead of '254219264', '980190962', etc.as shown in the
documentation.

Niels Basjes

You may want to take a look at this. https://github.com/plytro/foreman-bulk-provision
I had found some limitations in the curl application and wrote this. I rely on the hostgroup being correct and having certain things such as root password.

–Steve

··· ________________________________________ From: foreman-users@googlegroups.com [foreman-users@googlegroups.com] on behalf of Niels Basjes [niels@basj.es] Sent: Wednesday, August 14, 2013 1:46 PM To: foreman-users@googlegroups.com Subject: [foreman-users] Creating a host via the API

Hi,

I’m having trouble creating a single host via the API.

I read this http://theforeman.org/api/apidoc/hosts/create.html and tried to create a host using the provided example.

curl -s -k https://admin:admin@puppet.lan.basjes.nl/api/hosts?format=json -d’< THE EXACT JSON FROM THE PAGE >’

I got the following error:
{“host”:{“id”:null,“errors”:{“name”:[“can’t be blank”],“environment_id”:[“can’t be blank”],“root_pass”:[“should be 8 characters or more”]},“full_messages”:[“Name can’t be blank”,“Environment can’t be blank”,“Root pass should be 8 characters or more”]}}

I interpreted this as “I forgot some settings”.
So I retrieved the existing test host (virtual) using
curl -s -k https://admin:admin@puppet.lan.basjes.nl/api/hosts/pxetest.lan.basjes.nl
and then I did a
curl -s -k https://admin:admin@puppet.lan.basjes.nl/api/hosts?format=json -d’< THE EXACT JSON I GTO FROM THE PREVIOUS COMMAND >’

Resulting in the exact same error.

There is one thing I noticed that is different: All the ids for domains, environments, architectures, … on my test installation here are ‘1’, ‘2’, ‘3’, etc. instead of ‘254219264’, ‘980190962’, etc.as shown in the documentation.

Niels Basjes


You received this message because you are subscribed to the Google Groups “Foreman users” group.
To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/groups/opt_out.
This email message is for the sole use of the intended recipient(s) and may contain information that is privileged, confidential, and exempt from disclosure under applicable law. Any unauthorized review, use, copying, disclosure or dissemination is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.

Figuring out the right parameters for host API is not easy. In the docs
there is only subset of options mentioned. Good way to get the parameters
right is to set log level to debug and watch parameters being sent from the
web UI on host creation. The parameters may vary significantly depending on
compute resource and other things.

You may be interested in Hammer project (CLI for foreman). It is work in
progress but is evolving quickly. Host related commands are being worked on
right now and we appreciate any feedback or request for features.

Some links:
CLI - https://github.com/theforeman/hammer-cli
Foreman plugin - https://github.com/theforeman/hammer-cli-foreman
Packaged hammer -
https://groups.google.com/forum/#!topic/foreman-users/w7YhJRhBLys
Bug tracker (CLI category) -
Foreman

HTH,

Martin

Dne středa, 14. srpna 2013 21:46:09 UTC+2 Niels Basjes napsal(a):

··· > > Hi, > > I'm having trouble creating a single host via the API. > > I read this http://theforeman.org/api/apidoc/hosts/create.html and tried > to create a host using the provided example. > > curl -s -k https://admin:admin@puppet.lan.basjes.nl/api/hosts?format=json-d'< THE EXACT JSON FROM THE PAGE >' > > I got the following error: > *{"host":{"id":null,"errors":{"name":["can't be > blank"],"environment_id":["can't be blank"],"root_pass":["should be 8 > characters or more"]},"full_messages":["Name can't be blank","Environment > can't be blank","Root pass should be 8 characters or more"]}}* > > I interpreted this as "I forgot some settings". > So I retrieved the existing test host (virtual) using > curl -s -k > https://admin:admin@puppet.lan.basjes.nl/api/hosts/pxetest.lan.basjes.nl > and then I did a > curl -s -k https://admin:admin@puppet.lan.basjes.nl/api/hosts?format=json-d'< THE EXACT JSON I GTO FROM THE PREVIOUS COMMAND >' > > Resulting in the exact same error. > > There is one thing I noticed that is different: All the ids for domains, > environments, architectures, .. on my test installation here are '1', '2', > '3', etc. instead of '254219264', '980190962', etc.as shown in the > documentation. > > Niels Basjes >

Thanks. I'll have a close look at what you are doing differently.

Niels

command and output for reference?

thanks!
Ohad

··· On Tue, Aug 27, 2013 at 11:19 AM, Martin Bačovský wrote:

Figuring out the right parameters for host API is not easy. In the docs
there is only subset of options mentioned. Good way to get the parameters
right is to set log level to debug and watch parameters being sent from the
web UI on host creation. The parameters may vary significantly depending on
compute resource and other things.

You may be interested in Hammer project (CLI for foreman). It is work in
progress but is evolving quickly. Host related commands are being worked on
right now and we appreciate any feedback or request for features.

Some links:
CLI - https://github.com/theforeman/hammer-cli
Foreman plugin - https://github.com/theforeman/hammer-cli-foreman
Packaged hammer -
https://groups.google.com/forum/#!topic/foreman-users/w7YhJRhBLys
Bug tracker (CLI category) -
Foreman

Can you create hosts already? if you can, do you mind pasting here the cli

Thanks,

I did some googling and experimenting and finally figured out a working way
to create a host via the API.
A created a simple shell script that contains this:

··· ==== #! /bin/bash #

USER="admin"
PASS="secret"
FOREMAN_URL="https://puppet.lan.basjes.nl"
NAME=$1
MAC=$2
IP=$3

HOSTGROUP_ID=“3” # Change this to your favorite hostgroup

curl -s -H “Accept:application/json”
-k -u $USER:$PASS
-d “host[name]=$NAME”
-d “host[hostgroup_id]=$HOSTGROUP_ID”
-d “host[mac]=$MAC”
-d “host[ip]=$IP”
-d “host[architecture_id]=1”
-d “host[environment_id]=1”
-d “host[operatingsystem_id]=1”
-d “host[domain_id]=1”
-d “host[ptable_id]=1”
-d “host[root_pass]=“secret””
-d “host[build]=0”
$FOREMAN_URL/api/hosts

====

I give this execute permissions and run ./create-host.sh foo.bar.com
00:11:22:33:44:55 10.11.12.13
The response is this and the host is created in my system.

{“host”:{“name”:“foo.bar.com”,“id”:63,“ip”:“10.11.12.13”,“environment”:{“environment”:{“id”:1,“name”:“production”}},“last_report”:null,“updated_at”:“2013-08-29T19:50:13Z”,“created_at”:“2013-08-29T19:50:13Z”,“mac”:“00:11:22:33:44:55”,“sp_mac”:null,“sp_ip”:null,“sp_name”:null,“domain_id”:1,“architecture_id”:1,“operatingsystem_id”:1,“environment_id”:1,“subnet_id”:1,“sp_subnet_id”:null,“ptable_id”:1,“medium_id”:null,“build”:false,“comment”:null,“disk”:null,“installed_at”:null,“model_id”:null,“hostgroup_id”:3,“owner_id”:1,“owner_type”:“User”,“enabled”:true,“puppet_ca_proxy_id”:1,“managed”:true,“use_image”:null,“image_file”:"",“uuid”:null,“compute_resource_id”:null,“puppet_proxy_id”:1,“certname”:“foo.bar.com”,“image_id”:null,"":[]}}

I’m gonna try the hammer cli next :slight_smile:

Niels

>
>
>
>
> Figuring out the right parameters for host API is not easy. In the
> docs there is only subset of options mentioned. Good way to get
> the parameters right is to set log level to debug and watch
> parameters being sent from the web UI on host creation. The
> parameters may vary significantly depending on compute resource
> and other things.
>
> You may be interested in Hammer project (CLI for foreman). It is
> work in progress but is evolving quickly. Host related commands
> are being worked on right now and we appreciate any feedback or
> request for features.
>
> Some links:
> CLI - https://github.com/theforeman/hammer-cli
> Foreman plugin - https://github.com/theforeman/hammer-cli-foreman
> Packaged hammer -
> https://groups.google.com/forum/#!topic/foreman-users/w7YhJRhBLys
> <https://groups.google.com/forum/#!topic/foreman-users/w7YhJRhBLys>
> Bug tracker (CLI category) -
> Foreman
>
> Can you create hosts already? if you can, do you mind pasting here the
> cli command and output for reference?
I can, but the use is very limited, and as I mentioned in my host
template related post very uncomfortable.
With current version of hammer (when the remaining PR are merged) I was
able to create host on local libvirt, that built and run successfully
with following command:

$hammer host create --name testhost4 --hostgroup-id=1
–environment-id=2 --architecture-id=1 --domain-id 1 --puppet-proxy-id 1
–operatingsystem-id 3 --compute-resource-id 1 --ip '1.2.3.8' --build 1
–medium-id 1 --start 1 --enabled true --managed true --provider IPMI
–type 'Nic::Managed'

It assumes hostgroup has set all the params that are not present here. I
have no DHCP proxy and use tokens, so the ip has to be set to something.
It is also not possible to set volumes and network to other then the
CR's default

Martin

··· On 08/27/2013 10:28 AM, Ohad Levy wrote: > On Tue, Aug 27, 2013 at 11:19 AM, Martin Bačovský > <martin.bacovsky@gmail.com > wrote: > > thanks! > Ohad > -- > You received this message because you are subscribed to the Google > Groups "Foreman users" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to foreman-users+unsubscribe@googlegroups.com. > To post to this group, send email to foreman-users@googlegroups.com. > Visit this group at http://groups.google.com/group/foreman-users. > For more options, visit https://groups.google.com/groups/opt_out.

>
>
>
>
>
>> Figuring out the right parameters for host API is not easy. In the docs
>> there is only subset of options mentioned. Good way to get the parameters
>> right is to set log level to debug and watch parameters being sent from the
>> web UI on host creation. The parameters may vary significantly depending on
>> compute resource and other things.
>>
>> You may be interested in Hammer project (CLI for foreman). It is work in
>> progress but is evolving quickly. Host related commands are being worked on
>> right now and we appreciate any feedback or request for features.
>>
>> Some links:
>> CLI - https://github.com/theforeman/hammer-cli
>> Foreman plugin - https://github.com/theforeman/hammer-cli-foreman
>> Packaged hammer -
>> https://groups.google.com/forum/#!topic/foreman-users/w7YhJRhBLys
>> Bug tracker (CLI category) -
>> Foreman
>>
>> Can you create hosts already? if you can, do you mind pasting here the
> cli command and output for reference?
>
> I can, but the use is very limited, and as I mentioned in my host template
> related post very uncomfortable.
> With current version of hammer (when the remaining PR are merged) I was
> able to create host on local libvirt, that built and run successfully with
> following command:
>

Thanks!

>
> $hammer host create --name testhost4 --hostgroup-id=1 --environment-id=2
> --architecture-id=1 --domain-id 1 --puppet-proxy-id 1 --operatingsystem-id
> 3 --compute-resource-id 1 --ip '1.2.3.8' --build 1 --medium-id 1 --start 1
> --enabled true --managed true --provider IPMI --type 'Nic::Managed'
>

I wonder why you use IPMI for libvirt, probably not required…

is it on purpose that built is 1 and enabled is true etc? my guess is that
we need good feedback about the UX

thanks!!
Ohad

··· On Tue, Aug 27, 2013 at 12:14 PM, Martin Bačovský wrote: > On 08/27/2013 10:28 AM, Ohad Levy wrote: > On Tue, Aug 27, 2013 at 11:19 AM, Martin Bačovský < > martin.bacovsky@gmail.com> wrote:

It assumes hostgroup has set all the params that are not present here. I
have no DHCP proxy and use tokens, so the ip has to be set to something. It
is also not possible to set volumes and network to other then the CR’s
default

Martin

thanks!
Ohad

You received this message because you are subscribed to the Google Groups
"Foreman users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/groups/opt_out.


You received this message because you are subscribed to the Google Groups
"Foreman users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/groups/opt_out.

I've posted this here before. I have plans on making it a little more dynamic. I've provisioned a few hundred hosts using it.

https://github.com/plytro/foreman-bulk-provision

–Steve

··· ________________________________________ From: foreman-users@googlegroups.com [foreman-users@googlegroups.com] on behalf of Niels Basjes [niels@basj.es] Sent: Thursday, August 29, 2013 2:36 PM To: foreman-users@googlegroups.com Subject: Re: [foreman-users] Creating a host via the API

Thanks,

I did some googling and experimenting and finally figured out a working way to create a host via the API.
A created a simple shell script that contains this:

#! /bin/bash

USER=“admin”
PASS=“secret”
FOREMAN_URL=“https://puppet.lan.basjes.nl
NAME=$1
MAC=$2
IP=$3

HOSTGROUP_ID=“3” # Change this to your favorite hostgroup

curl -s -H “Accept:application/json”
-k -u $USER:$PASS
-d “host[name]=$NAME”
-d “host[hostgroup_id]=$HOSTGROUP_ID”
-d “host[mac]=$MAC”
-d “host[ip]=$IP”
-d “host[architecture_id]=1”
-d “host[environment_id]=1”
-d “host[operatingsystem_id]=1”
-d “host[domain_id]=1”
-d “host[ptable_id]=1”
-d “host[root_pass]=“secret””
-d “host[build]=0”
$FOREMAN_URL/api/hosts

====

I give this execute permissions and run ./create-host.sh foo.bar.com 00:11:22:33:44:55 10.11.12.13
The response is this and the host is created in my system.

{“host”:{“name”:“foo.bar.com”,“id”:63,“ip”:“10.11.12.13”,“environment”:{“environment”:{“id”:1,“name”:“production”}},“last_report”:null,“updated_at”:“2013-08-29T19:50:13Z”,“created_at”:“2013-08-29T19:50:13Z”,“mac”:“00:11:22:33:44:55”,“sp_mac”:null,“sp_ip”:null,“sp_name”:null,“domain_id”:1,“architecture_id”:1,“operatingsystem_id”:1,“environment_id”:1,“subnet_id”:1,“sp_subnet_id”:null,“ptable_id”:1,“medium_id”:null,“build”:false,“comment”:null,“disk”:null,“installed_at”:null,“model_id”:null,“hostgroup_id”:3,“owner_id”:1,“owner_type”:“User”,“enabled”:true,“puppet_ca_proxy_id”:1,“managed”:true,“use_image”:null,“image_file”:"",“uuid”:null,“compute_resource_id”:null,“puppet_proxy_id”:1,“certname”:“foo.bar.com”,“image_id”:null,"":}}

I’m gonna try the hammer cli next :slight_smile:

Niels


You received this message because you are subscribed to the Google Groups “Foreman users” group.
To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/groups/opt_out.
This email message is for the sole use of the intended recipient(s) and may contain information that is privileged, confidential, and exempt from disclosure under applicable law. Any unauthorized review, use, copying, disclosure or dissemination is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.

>
>
>
>
>>
>>
>>
>>
>> Figuring out the right parameters for host API is not easy.
>> In the docs there is only subset of options mentioned. Good
>> way to get the parameters right is to set log level to debug
>> and watch parameters being sent from the web UI on host
>> creation. The parameters may vary significantly depending on
>> compute resource and other things.
>>
>> You may be interested in Hammer project (CLI for foreman). It
>> is work in progress but is evolving quickly. Host related
>> commands are being worked on right now and we appreciate any
>> feedback or request for features.
>>
>> Some links:
>> CLI - https://github.com/theforeman/hammer-cli
>> Foreman plugin - https://github.com/theforeman/hammer-cli-foreman
>> Packaged hammer -
>> https://groups.google.com/forum/#!topic/foreman-users/w7YhJRhBLys
>> <https://groups.google.com/forum/#!topic/foreman-users/w7YhJRhBLys>
>> Bug tracker (CLI category) -
>> Foreman
>>
>> Can you create hosts already? if you can, do you mind pasting
>> here the cli command and output for reference?
> I can, but the use is very limited, and as I mentioned in my host
> template related post very uncomfortable.
> With current version of hammer (when the remaining PR are merged)
> I was able to create host on local libvirt, that built and run
> successfully with following command:
>
>
> Thanks!
>
>
> $hammer host create --name testhost4 --hostgroup-id=1
> --environment-id=2 --architecture-id=1 --domain-id 1
> --puppet-proxy-id 1 --operatingsystem-id 3 --compute-resource-id 1
> --ip '1.2.3.8' --build 1 --medium-id 1 --start 1 --enabled true
> --managed true --provider IPMI --type 'Nic::Managed'
>
>
> I wonder why you use IPMI for libvirt, probably not required…
>
> is it on purpose that built is 1 and enabled is true etc? my guess is
> that we need good feedback about the UX
It is possible and probable that some of the args could be stripped off
with the same result. After some unsuccessful attempts I tried to send
similar args as UI does. That is where the values come from. I found
out, that for example I can ommit --mac only if there is something (even
irrelevant) in compute_attributes set to satisfy some internal
conditions. I'm still trying to get more familiar with how the host API
works.

The rest of the API is easy and intuitive. The API for host require good
knowledge of provisioning and lacks detailed docs.
I believe with decent docs it may be okay for API but for CLI we have to
come up with something to help. Hopefully work on CLI will help us to
document API better.

Martin

··· On 08/27/2013 11:30 AM, Ohad Levy wrote: > On Tue, Aug 27, 2013 at 12:14 PM, Martin Bačovský > <martin.bacovsky@gmail.com > wrote: > On 08/27/2013 10:28 AM, Ohad Levy wrote: >> On Tue, Aug 27, 2013 at 11:19 AM, Martin Bačovský >> <martin.bacovsky@gmail.com > wrote: > > thanks!! > Ohad > > > It assumes hostgroup has set all the params that are not present > here. I have no DHCP proxy and use tokens, so the ip has to be set > to something. It is also not possible to set volumes and network > to other then the CR's default > > Martin >> >> thanks! >> Ohad >> -- >> You received this message because you are subscribed to the >> Google Groups "Foreman users" group. >> To unsubscribe from this group and stop receiving emails from it, >> send an email to foreman-users+unsubscribe@googlegroups.com >> . >> To post to this group, send email to >> foreman-users@googlegroups.com >> . >> Visit this group at http://groups.google.com/group/foreman-users. >> For more options, visit https://groups.google.com/groups/opt_out. > > -- > You received this message because you are subscribed to the Google > Groups "Foreman users" group. > To unsubscribe from this group and stop receiving emails from it, > send an email to foreman-users+unsubscribe@googlegroups.com > . > To post to this group, send email to > foreman-users@googlegroups.com > . > Visit this group at http://groups.google.com/group/foreman-users. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- > You received this message because you are subscribed to the Google > Groups "Foreman users" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to foreman-users+unsubscribe@googlegroups.com. > To post to this group, send email to foreman-users@googlegroups.com. > Visit this group at http://groups.google.com/group/foreman-users. > For more options, visit https://groups.google.com/groups/opt_out.

I’m trying to create hosts via API, the whole environment is working fine: Foreman + oVirt + FreeIPA (realm)

When I create via web UI everything just works, but now I’m trying the same via API and it’s not working, I managed to figure out some of the missing fields because the HTTP response was clear enough, but now I’m stuck with the following procedure:

Via postman I’m sending a POST with the following body:

 {
    "host": {
      "name": "banana",
      "hostgroup_id": 1,
      "environment_id": 1,
      "architecture_id": 1,
      "domain_id": 1,
      "puppet_proxy_id": 1,
      "puppet_ca_proxy_id": 1,
      "operatingsystem_id": 1,
      "provision_method": "image",
      "image_id": 1,
      "compute_resource_id": 1,
      "compute_profile_id": 1,
      "realm_id": 2,
      "enabled": true,
      "managed": true,
      "root_pass": "asdfasdfasdf"
    }
  }

And the return is not very clear, after googling for the following code, it doesn’t show any (good) result (I hope I’m not the first facing this issue)

{
  "error": {
    "id": null,
    "errors": {
      "base": [
        "Failed to create a compute oVirt (oVirt) instance banana.europe.intranet: ERF42-0973 [Foreman::Exception]:  is not valid, enter id or name\n "
      ]
    },
    "full_messages": [
      "Failed to create a compute oVirt (oVirt) instance banana.europe.intranet: ERF42-0973 [Foreman::Exception]:  is not valid, enter id or name\n "
    ]
  }
}

You don’t need to solve my problem, but it would be nice if could point me into the right direction, what is not clear to me is what exactly is not valid, which field/parameter?

Product versions:
Foreman Version 2.1.1
oVirt 4.3.10
FreeIPA version: 4.6.6

This is a very old thread. It’s better to create a new one.

I’m on it. Thanks.