Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / domains.rb @ bf81b9cf

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 DomainsCommand < BaseCommand
30

    
31
    FULLNAME = 'Full Name'
32
    ORGANIZATIONS = 'Organizations'
33

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

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

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

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

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

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

    
93
        print "done\n" if option_verbose?
94
      end
95
    rescue RuntimeError => e
96
      raise "#{e}\n       #{line}"
97
    end
98
  end
99

    
100
  HammerCLI::MainCommand.subcommand("csv:domains", "ping the katello server", HammerCLICsv::DomainsCommand)
101
end