Project

General

Profile

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

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

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 CsvCommand
32
    class LocationsCommand < BaseCommand
33
      command_name 'locations'
34
      desc         'import or export locations'
35

    
36
      PARENT = 'Parent Location'
37

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

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

    
53
        thread_import do |line|
54
          create_locations_from_csv(line)
55
        end
56
      end
57

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