Foreman api v2 post filter

Hi All,

Currently i try to automate any tasks over foreman api v2.I want to create
a usergroup, a role and any filtersI already done to create a usergroup and
a role code snippet below.

Usergroup creation

usergroupurl="https://#{user}:#{password}@localhost/api/v2/usergroups"

def checkusergroup(usergroupurl, usergroup, user, password)
restget = RestClient.get "#{usergroupurl}"
if restget.include?(usergroup)
return true
else
return false
end
end

def setusergroup(usergroupurl, usergroup, user, password)
body = { :usergroup => {
:name => "#{usergroup}"
}
}
response = RestClient.post("#{usergroupurl}", body.to_json,
{:content_type => 'application/json', :accept => 'application/json'})
end

unless checkusergroup(usergroupurl, usergroup, user, password)
setusergroup(usergroupurl, usergroup, user, password)
end

Role creation

rolesurl="https://#{user}:#{password}@localhost/api/v2/roles"

def checkroles(rolesurl, role, user, password)
restget = RestClient.get "#{rolesurl}"
if restget.include?(role)
return true
else
return false
end
end

def setrole(rolesurl, role, user, password)
body = { :role => {
:name => "#{role}"
}
}
response = RestClient.post("#{rolesurl}", body.to_json, {:content_type =>
'application/json', :accept => 'application/json'})
end

unless checkroles(rolesurl, role , user, password)
setrole(rolesurl, role, user, password)
end

Unfortunately i have problems to create my own filter. (code below)

hostfilterurl="https://#{user}:#{password}@localhost/api/v2/filters/"
def sethostfilter(hostfilterurl, role, user, password)
body = { :filter => {
:role_id => "700"
}
}
response = RestClient.post("#{hostfilterurl}", body.to_json,
{:content_type => 'application/json', :accept => 'application/json'})
end
sethostfilter(hostfilterurl, role, user, password)

In this case i get always a errormessage.
Message: 422 Unprocessable Entity

Can anybody help?

Regards
mobios

Vollständigen Inhalt anzeigen

> Hi All,
>
>
> Currently i try to automate any tasks over foreman api v2.
> I want to create a usergroup, a role and any filters
> I already done to create a usergroup and a role code snippet below.

>
>
>
> ## Usergroup creation
> usergroupurl="https://#{user}:#{password}@localhost/api/v2/usergroups"
>
> def checkusergroup(usergroupurl, usergroup, user, password)
> restget = RestClient.get "#{usergroupurl}"
> if restget.include?(usergroup)
> return true
> else
> return false
> end
> end
>
> def setusergroup(usergroupurl, usergroup, user, password)
> body = { :usergroup => {
> :name => "#{usergroup}"
> }
> }
> response = RestClient.post("#{usergroupurl}", body.to_json,
> {:content_type => 'application/json', :accept => 'application/json'})
> end
>
> unless checkusergroup(usergroupurl, usergroup, user, password)
> setusergroup(usergroupurl, usergroup, user, password)
> end
>
>
> ## Role creation
> rolesurl="https://#{user}:#{password}@localhost/api/v2/roles"
>
> def checkroles(rolesurl, role, user, password)
> restget = RestClient.get "#{rolesurl}"
> if restget.include?(role)
> return true
> else
> return false
> end
> end
>
> def setrole(rolesurl, role, user, password)
> body = { :role => {
> :name => "#{role}"
> }
> }
> response = RestClient.post("#{rolesurl}", body.to_json, {:content_type
> => 'application/json', :accept => 'application/json'})
> end
>
> unless checkroles(rolesurl, role , user, password)
> setrole(rolesurl, role, user, password)
> end
>
>
>
> Unfortunately i have problems to create my own filter. (code below)
>
>
> hostfilterurl="https://#{user}:#{password}@localhost/api/v2/filters/"
> def sethostfilter(hostfilterurl, role, user, password)
> body = { :filter => {
> :role_id => "700"
> }
> }
> response = RestClient.post("#{hostfilterurl}", body.to_json,
> {:content_type => 'application/json', :accept => 'application/json'})
> end
> sethostfilter(hostfilterurl, role, user, password)
>
>
>
> /In this case i get always a errormessage./
> /Message: 422 Unprocessable Entity/
>

Hello,
check body of the error response. It contains error messages that should
give you hint about what's wrong.

Alternatively you can look into server log files
(/var/log/foreman/production.log).

BTW check out project apipie-bindings [1]. It can be helpful if you're
writing a ruby script that integrates with foreman api.

Regards
Tomas

[1] https://github.com/Apipie/apipie-bindings

··· On 11/14/2016 01:01 PM, mobios wrote:

Can anybody help?

Regards
mobios

Vollständigen Inhalt anzeigen


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
mailto:foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com
mailto:foreman-users@googlegroups.com.
Visit this group at https://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/d/optout.

Thank for your answer Thomas.

My Problem is resolved.
I used Curl against the Restapi and i realized i need one more parameter in
my http request.

Your ideas were very usefull.

Best regards
mobios