Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / puppet_environments.rb @ f8ecc788

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
# -= Environments CSV =-
14
#
15
# Columns
16
#   Name
17
#     - Environment 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 PuppetEnvironmentsCommand < BaseCommand
30

    
31
    ORGANIZATIONS = 'Organizations'
32

    
33
    def export
34
      CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
35
        csv << [NAME, COUNT, ORGANIZATIONS]
36
        @api.resource(:environments).call(:index, {:per_page => 999999})['results'].each do |environment|
37
          name = environment['name']
38
          count = 1
39
          raise "TODO: organizations"
40
          csv << [name, count]
41
        end
42
      end
43
    end
44

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

    
51
      thread_import do |line|
52
        create_environments_from_csv(line)
53
      end
54
    end
55

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

    
76
        # Update associated resources
77
        # TODO: Bug #4738: organization json does not include puppet environments
78
        #       http://projects.theforeman.org/issues/4738#change-15319
79
        #       Update below to match style of domains
80
        organization_ids = CSV.parse_line(line[ORGANIZATIONS]).collect do |organization|
81
          foreman_organization(:name => organization)
82
        end
83
        organization_ids += @api.resource(:environments).call(:show, {'id' => id})['organizations'].collect do |organization|
84
          organization['id']
85
        end
86
        organization_ids.uniq!
87

    
88
        @api.resource(:environments).call(:update, {
89
                                            'id' => id,
90
                                            'environment' => {
91
                                              'organization_ids' => organization_ids
92
                                            }
93
                                          })
94

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

    
102
  HammerCLICsv::CsvCommand.subcommand("puppet-environments",
103
                                      "import or export puppet environments",
104
                                      HammerCLICsv::PuppetEnvironmentsCommand)
105
end