Import departments script
On this page
Introduction
Administrators can import departments via API. Each row of the CSV will create a new department to SWSD 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_departments.rb API_TOKEN departments.csv or for the eu datacenter: ruby import_departments.rb API_TOKEN departments.csv eu
Sync script examples
The script below is not supported by SolarWinds in any way. It is provided only as an example.
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_department(row:)
department = {
department: {
name: row["name"],
description: row["description"]
}
}
@samanage.create_department(payload: department)
rescue
log_to_csv(row: row.to_h.values)
end
csv_rows.map { |row| import_department(row: row) }