Creating host in EC2 via API

I'm trying to create a host using the foreman API but getting errors. I
don't think I am specifying enough information in my post. Does anyone
have an example of a valid POST request for creating an EC2 host?

Example:

curl -sS -u username -k -H "Content-Type: application/json" -XPOST
https://foreman.server/api/v2/hosts -d '
{
"host": {
"build": false,
"operatingsystem_name": "Ubuntu 12.04",
"operatingsystem_id": 7,
"environment_name": "production",
"environment_id": 5,
"name": "test.server",
"compute_resource_id": 8,
"provision_method": "image",
"certname": "test.server.local",
"image_id": 55,
"image_name": "Ubuntu v3",
"puppet_status": 0,
"organization_id": 22,
"location_id": 32,
"managed": true,
"enabled": true,
"owner_type": "User",
"owner_id": 7,
"architecture_name": "x86_64",
"architecture_id": 1,
"domain_name": "server.local",
"domain_id": 3,
"subnet_id": "subnet-89afc7eb"
}
}'

Using this, I get:

{
"error": {"id":null,"errors":{"mac":["has already been taken","is
invalid","can't be blank"],"ip":["is invalid"]},"full_messages":["MAC
address has already been taken","MAC address is invalid","MAC address can't
be blank","IP address is invalid"]}
}

How do i specify the subnet, security group, flavor, avail zone, managed
ip?

BTW, I'm running Foreman 1.5.0

··· On Tuesday, December 30, 2014 3:11:12 PM UTC-8, Dylan Charleston wrote: > > I'm trying to create a host using the foreman API but getting errors. I > don't think I am specifying enough information in my post. Does anyone > have an example of a valid POST request for creating an EC2 host? > > Example: > > curl -sS -u username -k -H "Content-Type: application/json" -XPOST > https://foreman.server/api/v2/hosts -d ' > { > "host": { > "build": false, > "operatingsystem_name": "Ubuntu 12.04", > "operatingsystem_id": 7, > "environment_name": "production", > "environment_id": 5, > "name": "test.server", > "compute_resource_id": 8, > "provision_method": "image", > "certname": "test.server.local", > "image_id": 55, > "image_name": "Ubuntu v3", > "puppet_status": 0, > "organization_id": 22, > "location_id": 32, > "managed": true, > "enabled": true, > "owner_type": "User", > "owner_id": 7, > "architecture_name": "x86_64", > "architecture_id": 1, > "domain_name": "server.local", > "domain_id": 3, > "subnet_id": "subnet-89afc7eb" > } > }' > > Using this, I get: > > { > "error": {"id":null,"errors":{"mac":["has already been taken","is > invalid","can't be blank"],"ip":["is invalid"]},"full_messages":["MAC > address has already been taken","MAC address is invalid","MAC address can't > be blank","IP address is invalid"]} > } > > > How do i specify the subnet, security group, flavor, avail zone, managed > ip? >

Hi Dylan,

Yes there are some attributes missing, and others specified twice :slight_smile:

The reason why I say there are some attributes duplicated in your JSON is
that it's not necessary to specify both the ID and the name of image,
operating systems and so forth. Also managed and enabled are true by
default and puppet_status doesn't have be specified.

Unfortunately I think neither subnets nor security groups, flavor,
availability zone or managed IP can be specified for hosts in an EC2
compute resource directly through a POST to api/v2/hosts easily. You can
use another hash key "compute_attributes" in which you put all the compute
resource specific attributes. For EC2 it's "image_id", "flavor_id",
"availability_zone", "security_group_id" and "managed_ip", but I had to
look for it in the code. It's messy and undocumented, thanks for unearthing
this because we should cover this stuff in Foreman

Instead of submitting the "compute_attributes" subhash, you can create a
compute profile in the UI and use it in the API with "compute_profile_id"
or "compute_profile_name". It is much simpler this way.

This is an example of a hash I've just used to create a host in an EC2
compute resource (Foreman 1.7.1 though, but it should be the same):

{
"host": {
"build": false,
"compute_resource_id": 20,
"compute_profile_id": 5,
"operatingsystem_id": 22,
"environment_id": 59253,
"name": "testie",
"provision_method": "image",
"image_id": 5,
"managed": true,
"owner_type": "User",
"owner_id": 7,
"architecture_id": 1,
"domain_id": 2,
"mac": "58:1B:54:AC:B9:EA"
}
}

Hope this was useful for you, let us know if you need more help!

Best,

··· On Wed, Dec 31, 2014 at 12:12 AM, Dylan Charleston < dcharleston@opentable.com> wrote:

BTW, I’m running Foreman 1.5.0

On Tuesday, December 30, 2014 3:11:12 PM UTC-8, Dylan Charleston wrote:

I’m trying to create a host using the foreman API but getting errors. I
don’t think I am specifying enough information in my post. Does anyone
have an example of a valid POST request for creating an EC2 host?

Example:

curl -sS -u username -k -H “Content-Type: application/json” -XPOST
https://foreman.server/api/v2/hosts -d ‘
{
“host”: {
“build”: false,
“operatingsystem_name”: “Ubuntu 12.04”,
“operatingsystem_id”: 7,
“environment_name”: “production”,
“environment_id”: 5,
“name”: “test.server”,
“compute_resource_id”: 8,
“provision_method”: “image”,
“certname”: “test.server.local”,
“image_id”: 55,
“image_name”: “Ubuntu v3”,
“puppet_status”: 0,
“organization_id”: 22,
“location_id”: 32,
“managed”: true,
“enabled”: true,
“owner_type”: “User”,
“owner_id”: 7,
“architecture_name”: “x86_64”,
“architecture_id”: 1,
“domain_name”: “server.local”,
“domain_id”: 3,
“subnet_id”: “subnet-89afc7eb”
}
}’

Using this, I get:

{
“error”: {“id”:null,“errors”:{“mac”:[“has already been taken”,“is
invalid”,“can’t be blank”],“ip”:[“is invalid”]},“full_messages”:[“MAC
address has already been taken”,“MAC address is invalid”,“MAC address can’t
be blank”,“IP address is invalid”]}
}

How do i specify the subnet, security group, flavor, avail zone, managed
ip?


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/d/optout.


Daniel Lobato

@elobatoss
blog.daniellobato.me
daniellobato.me

GPG: http://keys.gnupg.net/pks/lookup?op=get&search=0x7A92D6DD38D6DE30

> Hi Dylan,
>
> Yes there are some attributes missing, and others specified twice :slight_smile:
>
> The reason why I say there are some attributes duplicated in your JSON is
> that it's not necessary to specify both the ID and the name of image,
> operating systems and so forth. Also managed and enabled are true by
> default and puppet_status doesn't have be specified.
>
> Unfortunately I think neither subnets nor security groups, flavor,
> availability zone or managed IP can be specified for hosts in an EC2
> compute resource directly through a POST to api/v2/hosts easily. You can
> use another hash key "compute_attributes" in which you put all the compute
> resource specific attributes. For EC2 it's "image_id", "flavor_id",
> "availability_zone", "security_group_id" and "managed_ip", but I had to
> look for it in the code. It's messy and undocumented, thanks for unearthing
> this because we should cover this stuff in
> Foreman
>
> Instead of submitting the "compute_attributes" subhash, you can create a
> compute profile in the UI and use it in the API with "compute_profile_id"
> or "compute_profile_name". It is much simpler this way.
>
> This is an example of a hash I've just used to create a host in an EC2
> compute resource (Foreman 1.7.1 though, but it should be the same):
>
> {
> "host": {
> "build": false,
> "compute_resource_id": 20,
> "compute_profile_id": 5,
> "operatingsystem_id": 22,
> "environment_id": 59253,
> "name": "testie",
> "provision_method": "image",
> "image_id": 5,
> "managed": true,
> "owner_type": "User",
> "owner_id": 7,
> "architecture_id": 1,
> "domain_id": 2,
> "mac": "58:1B:54:AC:B9:EA"
> }
> }
>
> Hope this was useful for you, let us know if you need more help!
>

another option is simply to use hammer (our cli) which simplify it a bit
see

btw, I dont think you need to specify build/mac etc, and if you dont use
compute_profiles then you need to add the compute_attributes hash to your
json (with flavor_id etc…)

Ohad

··· On Wed, Dec 31, 2014 at 9:21 AM, Daniel Lobato wrote:

Best,

On Wed, Dec 31, 2014 at 12:12 AM, Dylan Charleston < > dcharleston@opentable.com> wrote:

BTW, I’m running Foreman 1.5.0

On Tuesday, December 30, 2014 3:11:12 PM UTC-8, Dylan Charleston wrote:

I’m trying to create a host using the foreman API but getting errors. I
don’t think I am specifying enough information in my post. Does anyone
have an example of a valid POST request for creating an EC2 host?

Example:

curl -sS -u username -k -H “Content-Type: application/json” -XPOST
https://foreman.server/api/v2/hosts -d ‘
{
“host”: {
“build”: false,
“operatingsystem_name”: “Ubuntu 12.04”,
“operatingsystem_id”: 7,
“environment_name”: “production”,
“environment_id”: 5,
“name”: “test.server”,
“compute_resource_id”: 8,
“provision_method”: “image”,
“certname”: “test.server.local”,
“image_id”: 55,
“image_name”: “Ubuntu v3”,
“puppet_status”: 0,
“organization_id”: 22,
“location_id”: 32,
“managed”: true,
“enabled”: true,
“owner_type”: “User”,
“owner_id”: 7,
“architecture_name”: “x86_64”,
“architecture_id”: 1,
“domain_name”: “server.local”,
“domain_id”: 3,
“subnet_id”: “subnet-89afc7eb”
}
}’

Using this, I get:

{
“error”: {“id”:null,“errors”:{“mac”:[“has already been taken”,“is
invalid”,“can’t be blank”],“ip”:[“is invalid”]},“full_messages”:[“MAC
address has already been taken”,“MAC address is invalid”,“MAC address can’t
be blank”,“IP address is invalid”]}
}

How do i specify the subnet, security group, flavor, avail zone, managed
ip?


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/d/optout.


Daniel Lobato

@elobatoss
blog.daniellobato.me
daniellobato.me

GPG: http://keys.gnupg.net/pks/lookup?op=get&search=0x7A92D6DD38D6DE30


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/d/optout.