Import sites using Ruby script
On this page
Introduction
Administrators can import sites using a Ruby script.
Overview
This simple script imports other_asset into Samanage based on a CSV data source. Each row of the CSV will create a new site in Service Desk as a JSON object based on the template. In this example, each row has columns which are selected by row["Column Name"].
CSV template example
Instructions
To run the script enter:
ruby import_sites.rb API_TOKEN sites.csv or for the eu datacenter: ruby import_sites.rb API_TOKEN sites.csv eu
Import site script example
The script below is not supported by SolarWinds in any way. It is provided only as an example and is not designed to work in every use case without some modification to the script itself.
Ruby code
# frozen_string_literal: true
require "samanage"
require "csv"
api_token = ARGV[0]
input = ARGV[1]
datacenter = ARGV[2]
@samanage = Samanage::Api.new(token: api_token, datacenter: datacenter)
csv_rows = CSV.read(input, headers: true, encoding: "ISO-8859-1")
DEFAULT_FILENAME = "Results-#{input.split('.').first}-#{DateTime.now.strftime("%b-%d-%Y-%l%M")}.csv"
OUTPUT_HEADERS = csv_rows.headers << "Error"
def log_to_csv(row:, filename: DEFAULT_FILENAME)
CSV.open(filename, "a+") { |csv|
csv << OUTPUT_HEADERS if csv.count.eql? 0
csv << row
}
end
def import_site(row:)
site = {
site: {
name: row["name"],
description: row["description"]
}
}
@samanage.create_site(payload: site)
rescue
log_to_csv(row: row.to_h.values)
end
csv_rows.map { |row| import_site(row: row) }