Project

General

Profile

Download (2.88 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / locations.rb @ f8ecc788

1
# Copyright 2013-2014 Red Hat, Inc.
2
#
3
# This software is licensed to you under the GNU General Public
4
# License as published by the Free Software Foundation; either version
5
# 2 of the License (GPLv2) or (at your option) any later version.
6
# There is NO WARRANTY for this software, express or implied,
7
# including the implied warranties of MERCHANTABILITY,
8
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
9
# have received a copy of GPLv2 along with this software; if not, see
10
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11

    
12
#
13
# -= Locations CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Name of the location.
18
#     - May contain '%d' which will be replaced with current iteration number of Count
19
#     - eg. "location%d" -> "location1"
20
#   Count
21
#     - Number of times to iterate on this line of the CSV file
22
#   Parent
23
#     - Parent location
24
#
25

    
26
require 'hammer_cli'
27
require 'json'
28
require 'csv'
29

    
30
module HammerCLICsv
31
  class LocationsCommand < BaseCommand
32

    
33
    PARENT = 'Parent Location'
34

    
35
    def export
36
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
37
        csv << [NAME, COUNT, PARENT]
38
        @api.resource(:locations).call(:index, {:per_page => 999999})['results'].each do |location|
39
          csv << [location['name'], 1]
40
        end
41
      end
42
    end
43

    
44
    def import
45
      @existing = {}
46
      @api.resource(:locations).call(:index, {:per_page => 999999})['results'].each do |location|
47
        @existing[location['name']] = location['id'] if location
48
      end
49

    
50
      thread_import do |line|
51
        create_locations_from_csv(line)
52
      end
53
    end
54

    
55
    def create_locations_from_csv(line)
56
      line[COUNT].to_i.times do |number|
57
        name = namify(line[NAME], number)
58
        location_id = @existing[name]
59
        if !location_id
60
          print "Creating location '#{name}'... " if option_verbose?
61
          @api.resource(:locations).call(:create, {
62
                                           'location' => {
63
                                             'name' => name,
64
                                             'parent_id' => foreman_location(:name => line[PARENT])
65
                                           }
66
                                         })
67
        else
68
          print "Updating location '#{name}'... " if option_verbose?
69
          @api.resource(:locations).call(:update, {
70
                                           'id' => location_id,
71
                                           'location' => {
72
                                             'parent_id' => foreman_location(:name => line[PARENT])
73
                                           }
74
                                         })
75
        end
76
        print "done\n" if option_verbose?
77
      end
78
    end
79
  end
80

    
81
  HammerCLICsv::CsvCommand.subcommand("locations",
82
                                      "import or export locations",
83
                                      HammerCLICsv::LocationsCommand)
84
end