Project

General

Profile

Bug #2345 ยป change_foreman_group_param.rb

Stefan Held, 08/12/2013 10:45 AM

 
#!/usr/bin/env ruby

require 'rubygems'
require 'rest-client'
require 'json'
client = RestClient::Resource.new('http://myhost/api',
:user => 'admin',
:password => 'changeme',
:headers => { :accept => 'application/json, version=2' }
)

group = ARGV[0]
param_to_change = ARGV[1]
value_to_implement = ARGV[2]

# Early init
id_to_update = nil
group_id = nil

# Let us connect to theForeman and find out which group id we are using, we need that for apiV2
group_id = JSON.parse(client["hostgroups/#{group}"].get)['hostgroup']['id']

# Print Some Infos for the User
puts "updating #{group} with id #{group_id}. We change the following Parameter #{param_to_change} to have the following value #{value_to_implement}"

existing_parameters = JSON.parse(client["hostgroups/#{group_id}/parameters"].get)
# Seems like a array of hashes, now lets loop through and find
existing_parameters.each do |key, value|
id_to_update = value.detect { |p| "#{param_to_change}" == p['parameter']['name']}['parameter']['id']
end

# update the host
client["hostgroups/#{group_id}/parameters/#{id_to_update}"].put(:parameter => { :name => param_to_change, :value => value_to_implement } )
    (1-1/1)