Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / puppet_environments.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
# -= Environments CSV =-
26
#
27
# Columns
28
#   Name
29
#     - Environment 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 PuppetEnvironmentsCommand < BaseCommand
42

    
43
    def execute
44
      super
45
      csv_export? ? export : import
46
      HammerCLI::EX_OK
47
    end
48

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

    
60
    def import
61
      @existing = {}
62
      @f_environment_api.index({:per_page => 999999}, HEADERS)[0].each do |environment|
63
        @existing[environment['name']] = environment['id']
64
      end
65

    
66
      thread_import do |line|
67
        create_environments_from_csv(line)
68
      end
69
    end
70

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

    
97
  HammerCLI::MainCommand.subcommand("csv:puppetenvironments", "Import or export puppet environments", HammerCLICsv::PuppetEnvironmentsCommand)
98
end