Search code examples
javajsonposthosttheforeman

Unable to create host using API v2 Foreman 1.7.1


My purpose: I just want to create a CentOS virtual machine from a template with specific cpu/ram/hard disk capacity through a java program that I wrote.

The things that I did: Foreman 1.7.1 + ( no cluster fix issues/1945 ) + compute resource addition for vmware

I can create a host through web ui with the following configuration: Name: testCentOsImageBased No Host Group Deploy on: cmptrsrc_VMWare (VMWare) Environment: production No puppet CA No puppet Master


Domain: localdomain


Architecture: x86_64 Operating system: CentOS 6.5 Provisioning Method: Image Based (CentOs6_5img)

According to API v2, I set my host config as follow:

host.setName("CGCentOs");
host.setEnvironmentId("1");
host.setDomainId("1");
host.setArchitectureId("1");
host.setOperatingsystemId("1");
host.setComputeResourceId("1");

I generate java pojos using http://www.jsonschema2pojo.org/ (I slightly change generated files like I delete @JsonInclude(JsonInclude.Include.NON_NULL) ) and @JsonPropertyOrder parts, I also use org.codehaus.jackson) and my code is based on this project https://github.com/muconsulting/foreman-java-sdk

I used to get/post lots of request without error, so I don't think there is anything faulty with this part

When I try to create my host with the aforementioned configs, tailing production.log gives: domain_id is not allowed as nested parameter for hosts

So I tried to get rid off using domain_id, Host groups can specify domain_id, so I created a host group with a specific environment:

Host host = new Host();
host.setName("CGCentOs");
host.setHostgroupId(1);    
host.setDomainId(1); 
host.setArchitectureId(1); 
host.setOperatingsystemId(1); 
host.setComputeResourceId(1);

It gives:

Processing by Api::V2::HostsController#create as JSON
  Parameters: {"name"=>"CGCentOs", "capabilities"=>nil, "parameters"=>nil, "interfaces"=>nil, "puppetclasses"=>nil, "domain_id"=>1, "architecture_id"=>1, "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1, "all_puppetclasses"=>nil, "config_groups"=>nil, "apiv"=>"v2", :host=>{"name"=>"CGCentOs", "domain_id"=>1, "architecture_id"=>1, "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1}}
Expire fragment views/tabs_and_title_records-3 (0.1ms)
Authorized user admin(Admin User)
domain_id is not allowed as nested parameter for hosts. Allowed parameters are hostgroup_id, location_id, organization_id, environment_id (RuntimeError)

So I specify domain_id in hostgroup too, it becomes

2.A hostgroup with specific environment and domain_id

Host host = new Host();
host.setName("CGCentOs");
host.setHostgroupId(1);    
host.setArchitectureId(1); 
host.setOperatingsystemId(1); 
host.setComputeResourceId(1);

It gives:

  Parameters: {"name"=>"CGCentOs", "capabilities"=>nil, "parameters"=>nil, "interfaces"=>nil, "puppetclasses"=>nil, "architecture_id"=>1, "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1, "all_puppetclasses"=>nil, "config_groups"=>nil, "apiv"=>"v2", :host=>{"name"=>"CGCentOs", "architecture_id"=>1, "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1}}
Authorized user admin(Admin User)
architecture_id is not allowed as nested parameter for hosts. Allowed parameters are hostgroup_id, location_id, organization_id, environment_id (RuntimeError)
  1. I add architecture_id to host group

    Host host = new Host(); host.setName("CGCentOs"); host.setHostgroupId(1);
    host.setOperatingsystemId(1); host.setComputeResourceId(1);

It gives:

 Parameters: {"name"=>"CGCentOs", "capabilities"=>nil, "parameters"=>nil, "interfaces"=>nil, "puppetclasses"=>nil, "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1, "all_puppetclasses"=>nil, "config_groups"=>nil, "apiv"=>"v2", :host=>{"name"=>"CGCentOs", "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1}}
Authorized user admin(Admin User)
operatingsystem_id is not allowed as nested parameter for hosts. Allowed parameters are hostgroup_id, location_id, organization_id, environment_id (RuntimeError)
  1. I add operatingsystem_id to host group.

    Host host = new Host(); host.setName("CGCentOs"); host.setHostgroupId(1);
    host.setComputeResourceId(1);

It gives:

Processing by Api::V2::HostsController#create as JSON
  Parameters: {"name"=>"CGCentOs", "capabilities"=>nil, "parameters"=>nil, "interfaces"=>nil, "puppetclasses"=>nil, "hostgroup_id"=>1, "compute_resource_id"=>1, "all_puppetclasses"=>nil, "config_groups"=>nil, "apiv"=>"v2", :host=>{"name"=>"CGCentOs", "hostgroup_id"=>1, "compute_resource_id"=>1}}
Expire fragment views/tabs_and_title_records-3 (0.6ms)
Authorized user admin(Admin User)
compute_resource_id is not allowed as nested parameter for hosts. Allowed parameters are hostgroup_id, location_id, organization_id, environment_id (RuntimeError)
  1. I can't add compute_resource id to hostgroup... i just remove the line...

    Host host = new Host(); host.setName("CGCentOs"); host.setHostgroupId(1);

It gives:

Processing by Api::V2::HostsController#create as JSON
  Parameters: {"name"=>"CGCentOs", "capabilities"=>nil, "parameters"=>nil, "interfaces"=>nil, "puppetclasses"=>nil, "hostgroup_id"=>1, "all_puppetclasses"=>nil, "config_groups"=>nil, "apiv"=>"v2", :host=>{"name"=>"CGCentOs", "hostgroup_id"=>1}}
Authorized user admin(Admin User)
Unprocessable entity Host::Managed (id: new):
  MAC address is not a valid MAC address
  MAC address can't be blank
  Partition Table cant be blank unless a custom partition has been defined

I was expecting this because, coumpute resource defines its virtuality... So, compute profiles and compute resources seem tight couple

I try this:

Host host = new Host();
host.setName("CGCentOs");
host.setHostgroupId(1);    
host.setComputeProfileId(3);

It gives:

Processing by Api::V2::HostsController#create as JSON
  Parameters: {"name"=>"CGCentOs", "capabilities"=>nil, "parameters"=>nil, "interfaces"=>nil, "puppetclasses"=>nil, "hostgroup_id"=>1, "compute_profile_id"=>3, "all_puppetclasses"=>nil, "config_groups"=>nil, "apiv"=>"v2", :host=>{"name"=>"CGCentOs", "hostgroup_id"=>1, "compute_profile_id"=>3}}
Authorized user admin(Admin User)
compute_profile_id is not allowed as nested parameter for hosts. Allowed parameters are hostgroup_id, location_id, organization_id, environment_id (RuntimeError)

I created a new compute profile and it appeared in the edit hos group pane... so retrying:

Host host = new Host();
host.setName("CGCentOs");
host.setHostgroupId(1); 

Again MAC adress problem:

Processing by Api::V2::HostsController#create as JSON
  Parameters: {"name"=>"CGCentOs", "capabilities"=>nil, "parameters"=>nil, "interfaces"=>nil, "puppetclasses"=>nil, "hostgroup_id"=>1, "all_puppetclasses"=>nil, "config_groups"=>nil, "apiv"=>"v2", :host=>{"name"=>"CGCentOs", "hostgroup_id"=>1}}
Expire fragment views/tabs_and_title_records-3 (0.1ms)
Authorized user admin(Admin User)
Unprocessable entity Host::Managed (id: new):
  MAC address is not a valid MAC address
  MAC address can't be blank
  Partition Table cant be blank unless a custom partition has been defined

Is there any workaround except pulling develop branch? Thank you in advance


Solution

  • Your JSON currently looks like this:

    {"name"=>"CGCentOs", "capabilities"=>nil, "parameters"=>nil, "interfaces"=>nil, "puppetclasses"=>nil, "domain_id"=>1, "architecture_id"=>1, "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1, "all_puppetclasses"=>nil, "config_groups"=>nil, "apiv"=>"v2", :host=>{"name"=>"CGCentOs", "domain_id"=>1, "architecture_id"=>1, "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1}}
    

    But is has to be:

    {"host"=>{"name"=>"CGCentOs", "capabilities"=>nil, "parameters"=>nil, "interfaces"=>nil, "puppetclasses"=>nil, "domain_id"=>1, "architecture_id"=>1, "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1, "all_puppetclasses"=>nil, "config_groups"=>nil, "apiv"=>"v2", :host=>{"name"=>"CGCentOs", "domain_id"=>1, "architecture_id"=>1, "operatingsystem_id"=>1, "hostgroup_id"=>1, "compute_resource_id"=>1}}}
    

    The reason is, there is a new key-value pair called "host" included with foreman 1.7.x.It is cause the API changed from foreman 1.6.x to 1.7.x

    Cheers, Chris