Project

General

Profile

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

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

1
module HammerCLICsv
2
  class CsvCommand
3
    class PuppetEnvironmentsCommand < BaseCommand
4
      command_name 'puppet-environments'
5
      desc         'import or export puppet environments'
6

    
7
      ORGANIZATIONS = 'Organizations'
8
      LOCATIONS = 'Locations'
9

    
10
      def export(csv)
11
        csv << [NAME, ORGANIZATIONS, LOCATIONS]
12
        @api.resource(:environments).call(:index, {:per_page => 999999})['results'].each do |environment|
13
          environment = @api.resource(:environments).call(:show, {:id => environment['id']})
14
          name = environment['name']
15
          organizations = export_column(environment, 'organizations', 'name')
16
          locations = export_column(environment, 'locations', 'name')
17
          csv << [name, organizations, locations]
18
        end
19
      end
20

    
21
      def import
22
        @existing = {}
23
        @api.resource(:environments).call(:index, {:per_page => 999999})['results'].each do |environment|
24
          @existing[environment['name']] = environment['id'] if environment
25
        end
26

    
27
        thread_import do |line|
28
          create_environments_from_csv(line)
29
        end
30
      end
31

    
32
      def create_environments_from_csv(line)
33
        organizations = collect_column(line[ORGANIZATIONS]) do |organization|
34
          foreman_organization(:name => organization)
35
        end
36
        locations = collect_column(line[LOCATIONS]) do |location|
37
          foreman_location(:name => location)
38
        end
39

    
40
        count(line[COUNT]).times do |number|
41
          name = namify(line[NAME], number)
42
          if !@existing.include? name
43
            print "Creating environment '#{name}'..." if option_verbose?
44
            id = @api.resource(:environments).call(:create, {
45
                                             'environment' => {
46
                                               'name' => name,
47
                                               'organization_ids' => organizations
48
                                             }
49
                                           })['id']
50
          else
51
            print "Updating environment '#{name}'..." if option_verbose?
52

    
53
            environment = @api.resource(:environments).call(:show, {'id' => @existing[name]})
54
            environment['organizations'].collect do |organization|
55
              organizations << organization['id']
56
            end
57
            organizations.uniq!
58
            environment['locations'].collect do |location|
59
              locations << location['id']
60
            end
61
            locations.uniq!
62

    
63
            @api.resource(:environments).call(:update, {
64
                                         'id' => @existing[name],
65
                                         'environment' => {
66
                                           'name' => name,
67
                                           'organization_ids' => organizations,
68
                                           'location_ids' => locations
69
                                         }
70
                                       })
71
          end
72

    
73
          puts "done" if option_verbose?
74
        end
75
      end
76
    end
77
  end
78
end