Creating interfaces via API

Hello folks, I'm working on an API script for foreman 1.1 which will add
additional interfaces for a host.

per Ohad's direction I was roughly following this:

I get it to add an interface but the only attribute I can set successfully
is the MAC

I've been searching around the codebase for the proper param names for
name, domain_id etc but my return value for my api call shows mostly nil
values:

[{"subnet_id"=>nil, "updated_at"=>"2013-04-28T01:26:02Z", "host_id"=>10406,
"mac"=>"06:54:9e:00:0a:d9", "name"=>nil, "attrs"=>nil, "id"=>19, "ip"=>nil,
"created_at"=>"2013-04-28T01:26:02Z", "domain_id"=>nil}]

my code: http://pastie.org/7731241

any suggestions?

> Hello folks, I'm working on an API script for foreman 1.1 which will add
> additional interfaces for a host.
>
> per Ohad's direction I was roughly following this:
> https://gist.github.com/ohadlevy/4147992
>
> I get it to add an interface but the only attribute I can set successfully
> is the MAC
>
> I've been searching around the codebase for the proper param names for
> name, domain_id etc but my return value for my api call shows mostly nil
> values:
>
> [{"subnet_id"=>nil, "updated_at"=>"2013-04-28T01:26:02Z",
> "host_id"=>10406, "mac"=>"06:54:9e:00:0a:d9", "name"=>nil, "attrs"=>nil,
> "id"=>19, "ip"=>nil, "created_at"=>"2013-04-28T01:26:02Z",
> "domain_id"=>nil}]
>
> my code: http://pastie.org/7731241
>
> any suggestions?
>
what do you see in foreman logs?

Ohad

··· On Sun, Apr 28, 2013 at 4:32 AM, Brian Galura wrote:


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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

Started PUT "/hosts/bgtest4..com" for 10.22.6.154 at Sun Apr 28 12:58:34
-0700 2013
Processing by HostsController#update as JSON
Parameters:
{"host"=>{"interfaces_attributes"=>{"1"=>{"domain_id"=>"2688",
"subnet_id"=>"1", "nested"=>"true", "name"=>"bgtest-1-1.cs.
.com",
"mac"=>"06:54:9e:00:0a:d9", "ip"=>"10.229.69.80", "domain"=>"cs."}}},
"id"=>"bgtest4.cs.
.com"}
Authorized user apiuser(api user)
Completed 200 OK in 416ms (Views: 5.0ms | ActiveRecord: 121.8ms)

Started GET "/hosts/bgtest4..com" for 10.22.6.154 at Sun Apr 28 12:58:34
-0700 2013
Processing by HostsController#show as JSON
Parameters: {"id"=>"bgtest4.cs.
.com"}
Authorized user apiuser(api user)

··· On Sun, Apr 28, 2013 at 12:50 PM, Ohad Levy wrote:

On Sun, Apr 28, 2013 at 4:32 AM, Brian Galura bgalura@gmail.com wrote:

Hello folks, I’m working on an API script for foreman 1.1 which will add
additional interfaces for a host.

per Ohad’s direction I was roughly following this:
https://gist.github.com/ohadlevy/4147992

I get it to add an interface but the only attribute I can set
successfully is the MAC

I’ve been searching around the codebase for the proper param names for
name, domain_id etc but my return value for my api call shows mostly nil
values:

[{“subnet_id”=>nil, “updated_at”=>“2013-04-28T01:26:02Z”,
“host_id”=>10406, “mac”=>“06:54:9e:00:0a:d9”, “name”=>nil, “attrs”=>nil,
“id”=>19, “ip”=>nil, “created_at”=>“2013-04-28T01:26:02Z”,
“domain_id”=>nil}]

my code: http://pastie.org/7731241

any suggestions?

what do you see in foreman logs?

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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


You received this message because you are subscribed to a topic in the
Google Groups “Foreman users” group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/foreman-users/ZQ9ogBDfYvU/unsubscribe?hl=en
.
To unsubscribe from this group and all its topics, 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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

Hi Brian,
I happened to have the same challenge today.

I solved it as follows:
http://pastebin.com/aKTCaApb

Another problem I ran upon - for whom it may concern (@Ohad) - is that the
subnets are not shown correctly in the WebGUI (means non is showing up).

I think this is due to a bug in both the 1.1 and develop version.

The problem is the domain_subnets method defined in
hosts_and_hostgroups_helper.rb
(https://github.com/theforeman/foreman/blob/develop/app/helpers/hosts_and_hostgroups_helper.rb#L35).
The subnets to be returned will only work for the primary interface.
The following path let's this problem go away.
I don't know if this is the appropriate way (concerning rights and ACLs)
but I couldn't find a better way.
http://pastebin.com/R0iFWPwC

Regards Marc.

··· On Sunday, April 28, 2013 10:00:30 PM UTC+2, Brian Galura wrote: > > Started PUT "/hosts/bgtest4.*.com" for 10.22.6.154 at Sun Apr 28 12:58:34 > -0700 2013 > Processing by HostsController#update as JSON > Parameters: > {"host"=>{"interfaces_attributes"=>{"1"=>{"domain_id"=>"2688", > "subnet_id"=>"1", "nested"=>"true", "name"=>"bgtest-1-1.cs.*.com", > "mac"=>"06:54:9e:00:0a:d9", "ip"=>"10.229.69.80", "domain"=>"cs.*"}}}, > "id"=>"bgtest4.cs.*.com"} > Authorized user apiuser(api user) > Completed 200 OK in 416ms (Views: 5.0ms | ActiveRecord: 121.8ms) > > > Started GET "/hosts/bgtest4.*.com" for 10.22.6.154 at Sun Apr 28 12:58:34 > -0700 2013 > Processing by HostsController#show as JSON > Parameters: {"id"=>"bgtest4.cs.*.com"} > Authorized user apiuser(api user) > > > > On Sun, Apr 28, 2013 at 12:50 PM, Ohad Levy <ohad...@gmail.com > > wrote: > >> >> >> On Sun, Apr 28, 2013 at 4:32 AM, Brian Galura <bga...@gmail.com >> > wrote: >> >>> Hello folks, I'm working on an API script for foreman 1.1 which will add >>> additional interfaces for a host. >>> >>> per Ohad's direction I was roughly following this: >>> https://gist.github.com/ohadlevy/4147992 >>> >>> I get it to add an interface but the only attribute I can set >>> successfully is the MAC >>> >>> I've been searching around the codebase for the proper param names for >>> name, domain_id etc but my return value for my api call shows mostly nil >>> values: >>> >>> [{"subnet_id"=>nil, "updated_at"=>"2013-04-28T01:26:02Z", >>> "host_id"=>10406, "mac"=>"06:54:9e:00:0a:d9", "name"=>nil, "attrs"=>nil, >>> "id"=>19, "ip"=>nil, "created_at"=>"2013-04-28T01:26:02Z", >>> "domain_id"=>nil}] >>> >>> my code: http://pastie.org/7731241 >>> >>> any suggestions? >>> >> what do you see in foreman logs? >> >> 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-user...@googlegroups.com . >>> >>> To post to this group, send email to forema...@googlegroups.com >>> . >>> Visit this group at http://groups.google.com/group/foreman-users?hl=en. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >>> >>> >> >> -- >> You received this message because you are subscribed to a topic in the >> Google Groups "Foreman users" group. >> To unsubscribe from this topic, visit >> https://groups.google.com/d/topic/foreman-users/ZQ9ogBDfYvU/unsubscribe?hl=en >> . >> To unsubscribe from this group and all its topics, send an email to >> foreman-user...@googlegroups.com . >> To post to this group, send email to forema...@googlegroups.com >> . >> Visit this group at http://groups.google.com/group/foreman-users?hl=en. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > >

Thanks this worked for me. Alternatively, I was able to do what I wanted by
talking directly to the smart-proxy API.

··· On Mon, Apr 29, 2013 at 8:06 AM, Marc Grimme wrote:

Hi Brian,
I happened to have the same challenge today.

I solved it as follows:
http://pastebin.com/aKTCaApb

Another problem I ran upon - for whom it may concern (@Ohad) - is that
the subnets are not shown correctly in the WebGUI (means non is showing up).

I think this is due to a bug in both the 1.1 and develop version.

The problem is the domain_subnets method defined in
hosts_and_hostgroups_helper.rb (
https://github.com/theforeman/foreman/blob/develop/app/helpers/hosts_and_hostgroups_helper.rb#L35
).
The subnets to be returned will only work for the primary interface.
The following path let’s this problem go away.
I don’t know if this is the appropriate way (concerning rights and ACLs)
but I couldn’t find a better way.
http://pastebin.com/R0iFWPwC

Regards Marc.

On Sunday, April 28, 2013 10:00:30 PM UTC+2, Brian Galura wrote:

Started PUT “/hosts/bgtest4..com" for 10.22.6.154 at Sun Apr 28 12:58:34
-0700 2013
Processing by HostsController#update as JSON
Parameters: {“host”=>{“interfaces_**attributes”=>{“1”=>{“domain_**id”=>“2688”,
“subnet_id”=>“1”, “nested”=>“true”, “name”=>"bgtest-1-1.cs.
.com”,
“mac”=>“06:54:9e:00:0a:d9”, “ip”=>“10.229.69.80”, “domain”=>“cs."}}},
“id”=>"bgtest4.cs.
.com”}
Authorized user apiuser(api user)
Completed 200 OK in 416ms (Views: 5.0ms | ActiveRecord: 121.8ms)

Started GET “/hosts/bgtest4..com" for 10.22.6.154 at Sun Apr 28 12:58:34
-0700 2013
Processing by HostsController#show as JSON
Parameters: {“id”=>"bgtest4.cs.
.com”}
Authorized user apiuser(api user)

On Sun, Apr 28, 2013 at 12:50 PM, Ohad Levy ohad...@gmail.com wrote:

On Sun, Apr 28, 2013 at 4:32 AM, Brian Galura bga...@gmail.com wrote:

Hello folks, I’m working on an API script for foreman 1.1 which will
add additional interfaces for a host.

per Ohad’s direction I was roughly following this:
https://gist.github.com/**ohadlevy/4147992https://gist.github.com/ohadlevy/4147992

I get it to add an interface but the only attribute I can set
successfully is the MAC

I’ve been searching around the codebase for the proper param names for
name, domain_id etc but my return value for my api call shows mostly nil
values:

[{“subnet_id”=>nil, “updated_at”=>“2013-04-28T01:**26:02Z”,
“host_id”=>10406, “mac”=>“06:54:9e:00:0a:d9”, “name”=>nil, “attrs”=>nil,
“id”=>19, “ip”=>nil, “created_at”=>“2013-04-28T01:**26:02Z”,
“domain_id”=>nil}]

my code: http://pastie.org/**7731241 http://pastie.org/7731241

any suggestions?

what do you see in foreman logs?

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-user…@**googlegroups.com.

To post to this group, send email to forema...@googlegroups.com**.

Visit this group at http://groups.google.com/**
group/foreman-users?hl=enhttp://groups.google.com/group/foreman-users?hl=en
.
For more options, visit https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
.


You received this message because you are subscribed to a topic in the
Google Groups “Foreman users” group.
To unsubscribe from this topic, visit https://groups.google.com/d/**
topic/foreman-users/**ZQ9ogBDfYvU/unsubscribe?hl=enhttps://groups.google.com/d/topic/foreman-users/ZQ9ogBDfYvU/unsubscribe?hl=en
.
To unsubscribe from this group and all its topics, send an email to
foreman-user…@googlegroups.com.
To post to this group, send email to forema...@googlegroups.com
.

Visit this group at http://groups.google.com/**group/foreman-users?hl=enhttp://groups.google.com/group/foreman-users?hl=en
.
For more options, visit https://groups.google.com/**groups/opt_outhttps://groups.google.com/groups/opt_out
.


You received this message because you are subscribed to a topic in the
Google Groups “Foreman users” group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/foreman-users/ZQ9ogBDfYvU/unsubscribe?hl=en
.
To unsubscribe from this group and all its topics, 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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.