Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / domains.rb @ 561a8ac9

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
# -= Domains CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Domain name
18
#     - May contain '%d' which will be replaced with current iteration number of Count
19
#     - eg. "os%d" -> "os1"
20
#   Count
21
#     - Number of times to iterate on this line of the CSV file
22
#
23

    
24
require 'hammer_cli'
25
require 'json'
26
require 'csv'
27

    
28
module HammerCLICsv
29
  class CsvCommand
30
    class DomainsCommand < BaseCommand
31
      command_name 'domains'
32
      desc         'import or export domains'
33

    
34
      FULLNAME = 'Full Name'
35
      ORGANIZATIONS = 'Organizations'
36

    
37
      def export
38
        CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
39
          csv << [NAME, COUNT, FULLNAME]
40
          @api.resource(:domains).call(:index, {:per_page => 999999})['results'].each do |domain|
41
            puts domain
42
            name = domain['name']
43
            count = 1
44
            fullname = domain['fullname']
45
            csv << [name, count, fullname]
46
          end
47
        end
48
      end
49

    
50
      def import
51
        @existing = {}
52
        @api.resource(:domains).call(:index, {:per_page => 999999})['results'].each do |domain|
53
          @existing[domain['name']] = domain['id'] if domain
54
        end
55

    
56
        thread_import do |line|
57
          create_domains_from_csv(line)
58
        end
59
      end
60

    
61
      def create_domains_from_csv(line)
62
        line[COUNT].to_i.times do |number|
63
          name = namify(line[NAME], number)
64
          if !@existing.include? name
65
            print "Creating domain '#{name}'..." if option_verbose?
66
            domain_id = @api.resource(:domains).call(:create, {
67
                                                       'name' => name
68
                                                     })['id']
69
          else
70
            print "Updating domain '#{name}'..." if option_verbose?
71
            domain_id = @api.resource(:domains).call(:update, {
72
                                                       'id' => @existing[name],
73
                                                       'name' => name
74
                                                     })['id']
75
          end
76

    
77
          # Update associated resources
78
          domains ||= {}
79
          CSV.parse_line(line[ORGANIZATIONS]).each do |organization|
80
            organization_id = foreman_organization(:name => organization)
81
            if domains[organization].nil?
82
              domains[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})['domains'].collect do |domain|
83
                domain['id']
84
              end
85
            end
86
            domains[organization] += [domain_id] if !domains[organization].include? domain_id
87

    
88
            @api.resource(:organizations).call(:update, {
89
                                                 'id' => organization_id,
90
                                                 'organization' => {
91
                                                   'domain_ids' => domains[organization]
92
                                                 }
93
                                               })
94
          end
95

    
96
          print "done\n" if option_verbose?
97
        end
98
      rescue RuntimeError => e
99
        raise "#{e}\n       #{line}"
100
      end
101
    end
102
  end
103
end