please don't use shells as API clients

This post is "motivated" by a request for help on debugging an issue with a shell script that was supposed to trigger an action using the Katello API, more precisely the Assign the environment and content view to one or more hosts action.

The script looked like this:

_CLIUSER="admin"
_CLIPASS="changeme"
_URL="https://foreman.example.com"
_ORGID=1
_CCVID=2
_ENVID=1
_HOSTNAME="exapp01"


curl -H "Accept:application/json,version=2" -H "Content-Type:application/json" -X PUT -s -k -u $_CLIUSER:"$_CLIPASS" -d "{\"organization_id\":$_ORGID,\"included\":{\"search\":[\"name = $_HOSTNAME\"]},\"excluded\":[],\"content_view_id\":$_CCVID,\"environment_id\":$_ENVID}"  $_URL/api/hosts/bulk/environment_content_view

And when we run it, the script (well, actually the API) throws a nice error:

$ bash test1.sh
{"displayMessage":"Unsupported query object: [\"name = exapp01\"]!","errors":["Unsupported query object: [\"name = exapp01\"]!"]}

My initial reaction was "Ugh, that's pretty much unreadable!". But let's still try to fix that up!

What is the desired action here? Taking a bunch of hosts (those matching the search query for $_HOSTNAME) and assign them to a new Content View and a new Lifecycle Environment (what these actually are is irrelevant for the further excercise).

The user also reported that the same script worked in an earlier version of Katello, so it might be a broken API or a broken script (that only was accepted previously by accident).

So, let's try to recreate the same action, but using Ruby and the nice Apipie Bindings (because the Foreman and Katello APIs are documented with Apipie).

require 'apipie-bindings'

@api = ApipieBindings::API.new({:uri => _URL, :username => _CLIUSER, :password => _CLIPASS, :api_version => '2'}, {:verify_ssl => false})

puts @api.resource(:hosts_bulk_actions).call(:environment_content_view, {:organization_id => _ORGID, :content_view_id => _CCVID, :environment_id => _ENVID, :included => {:search => "name = #{_HOSTNAME}"}, :excluded => {}})

/opt/theforeman/tfm/root/usr/share/gems/gems/apipie-bindings-0.2.0/lib/apipie_bindings/action.rb:67:in `validate!': ApipieBindings::InvalidArgumentTypesError: excluded - Hash was expected (ApipieBindings::InvalidArgumentTypesError)

System Message: WARNING/2 (<string>, line 41); backlink

Inline interpreted text or phrase reference start-string without end-string.

/opt/theforeman/tfm/root/usr/share/gems/gems/rest-client-1.8.0/lib/restclient/abstract_response.rb:74:in `return!': 500 Internal Server Error (RestClient::InternalServerError)

System Message: WARNING/2 (<string>, line 43); backlink

Inline interpreted text or phrase reference start-string without end-string.
$ bash test2.sh
{"displayMessage":"no implicit conversion of Symbol into Integer","errors":["no implicit conversion of Symbol into Integer"]}

Comments

No comments.