Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / domains.rb @ 0c7641a0

1
# Copyright (c) 2013 Red Hat
2
#
3
# MIT License
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#
24
#
25
# -= Domains CSV =-
26
#
27
# Columns
28
#   Name
29
#     - Domain name
30
#     - May contain '%d' which will be replaced with current iteration number of Count
31
#     - eg. "os%d" -> "os1"
32
#   Count
33
#     - Number of times to iterate on this line of the CSV file
34
#
35

    
36
require 'hammer_cli'
37
require 'json'
38
require 'csv'
39

    
40
module HammerCLICsv
41
  class DomainsCommand < BaseCommand
42

    
43
    FULLNAME = 'Full Name'
44

    
45
    def execute
46
      super
47
      csv_export? ? export : import
48
      HammerCLI::EX_OK
49
    end
50

    
51
    def export
52
      CSV.open(csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
53
        csv << [NAME, COUNT, FULLNAME]
54
        @f_domain_api.index({:per_page => 999999}, HEADERS)[0].each do |domain|
55
          name = domain['name']
56
          count = 1
57
          fullname = domain['fullname']
58
          csv << [name, count, fullname]
59
        end
60
      end
61
    end
62

    
63
    def import
64
      @existing = {}
65
      @f_domain_api.index({:per_page => 999999}, HEADERS)[0].each do |domain|
66
        @existing[domain['name']] = domain['id']
67
      end
68

    
69
      thread_import do |line|
70
        create_domains_from_csv(line)
71
      end
72
    end
73

    
74
    def create_domains_from_csv(line)
75
      line[COUNT].to_i.times do |number|
76
        name = namify(line[NAME], number)
77
        if !@existing.include? name
78
          print "Creating domain '#{name}'..." if verbose?
79
          @f_domain_api.create({
80
                             'domain' => {
81
                               'name' => name
82
                             }
83
                           }, HEADERS)
84
        else
85
          print "Updating domain '#{name}'..." if verbose?
86
          @f_domain_api.update({
87
                             'id' => @existing[name],
88
                             'domain' => {
89
                               'name' => name
90
                             }
91
                           }, HEADERS)
92
        end
93
        print "done\n" if verbose?
94
      end
95
    rescue RuntimeError => e
96
      raise RuntimeError.new("#{e}\n       #{line}")
97
    end
98
  end
99

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