def create_templates_from_csv(line)
organizations = collect_column(line[ORGANIZATIONS]) do |organization|
foreman_organization(:name => organization)
end
locations = collect_column(line[LOCATIONS]) do |location|
foreman_location(:name => location)
end
line[COUNT].to_i.times do |number|
name = namify(line[NAME], number)
if !@existing.include? name
print "Creating provisioning template '#{name}'..." if option_verbose?
template_id = @api.resource(:config_templates)\
.call(:create, {
'name' => name,
'snippet' => line[KIND] == 'snippet',
'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
'organization_ids' => organizations,
'location_ids' => locations,
'template' => line[TEMPLATE]
})['id']
else
print "Updating provisioning template '#{name}'..." if option_verbose?
template_id = @api.resource(:config_templates)\
.call(:update, {
'id' => @existing[name],
'name' => name,
'snippet' => line[KIND] == 'snippet',
'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
'organization_ids' => organizations,
'location_ids' => locations,
'template' => line[TEMPLATE]
})['id']
end
@existing[name] = template_id
template_organizations ||= {}
organizations.each do |organization_id|
if template_organizations[organization_id].nil?
template_organizations[organization_id] = @api.resource(:organizations)\
.call(:show, {
'id' => organization_id
})['config_templates'].collect do |template|
template['id']
end
end
if !template_organizations[organization_id].include? template_id
template_organizations[organization_id] += [template_id]
@api.resource(:organizations)\
.call(:update, {
'id' => organization_id,
'organization' => {
'config_template_ids' => template_organizations[organization_id]
}
})
end
end
template_locations ||= {}
locations.each do |location_id|
if template_locations[location_id].nil?
template_locations[location_id] = @api.resource(:locations)\
.call(:show, {
'id' => location_id
})['config_templates'].collect do |template|
template['id']
end
end
if !template_locations[location_id].include? template_id
template_locations[location_id] += [template_id]
@api.resource(:locations)\
.call(:update, {
'id' => location_id,
'location' => {
'config_template_ids' => template_locations[location_id]
}
})
end
end
print "done\n" if option_verbose?
end
rescue RuntimeError => e
raise "#{e}\n #{line[NAME]}"
end