Project

General

Profile

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

hammer-cli-csv / lib / hammer_cli_csv / host_groups.rb @ f4fb9c20

1
module HammerCLICsv
2
  class CsvCommand
3
    class HostGroupsCommand < BaseCommand
4
      command_name 'host-groups'
5
      desc         'import or export host-groups'
6

    
7
      PARENT = 'Parent Host Group'
8
      ORGANIZATIONS = 'Organizations'
9
      LOCATIONS = 'Locations'
10
      SUBNET = 'Subnet'
11
      DOMAIN = 'Domain'
12
      OPERATING_SYSTEM = 'Operating System'
13
      ENVIRONMENT = 'Puppet Environment'
14
      COMPUTE_PROFILE = 'Compute Profile'
15
      PARTITION_TABLE = 'Partition Table'
16
      MEDIUM = 'Medium'
17
      ARCHITECTURE = 'Architecture'
18
      REALM = 'Realm'
19
      PUPPET_PROXY = 'Puppet Proxy'
20
      PUPPET_CA_PROXY = 'Puppet CA Proxy'
21
      CONTENT_SOURCE = 'Content Source'
22
      PASSWORD = 'Password'
23
      PUPPET_CLASSES = 'Puppet Classes'
24

    
25
      def export(csv)
26
        csv << [NAME, PARENT, ORGANIZATIONS, LOCATIONS, SUBNET, DOMAIN, OPERATING_SYSTEM,
27
                ENVIRONMENT, COMPUTE_PROFILE, PARTITION_TABLE, MEDIUM, ARCHITECTURE, REALM,
28
                PUPPET_PROXY, PUPPET_CA_PROXY, CONTENT_SOURCE, PASSWORD, PUPPET_CLASSES]
29
        search_options = {:per_page => 999999}
30
        search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
31
        @api.resource(:hostgroups).call(:index, search_options)['results'].each do |hostgroup|
32
          hostgroup = @api.resource(:hostgroups).call(:show, {'id' => hostgroup['id']})
33
          raise "Host Group 'id=#{hostgroup['id']}' not found" if !hostgroup || hostgroup.empty?
34

    
35
          name = hostgroup['name']
36
          organizations = export_column(hostgroup, 'organizations', 'name')
37
          locations = export_column(hostgroup, 'locations', 'name')
38
          subnet = hostgroup['subnet_name']
39
          operating_system = hostgroup['operatingsystem_name']
40
          domain = hostgroup['domain_name']
41
          puppet_environment = hostgroup['environment_name']
42
          compute_profile = hostgroup['compute_profile_name']
43
          partition_table = hostgroup['ptable_name']
44
          medium = hostgroup['medium_name']
45
          architecture = hostgroup['architecture_name']
46
          realm = hostgroup['realm_name']
47
          puppet_proxy = hostgroup['puppet_proxy_id'] ? foreman_host(:id => hostgroup['puppet_proxy_id']) : nil
48
          puppet_ca_proxy = hostgroup['puppet_ca_proxy_id'] ? foreman_host(:id => hostgroup['puppet_ca_proxy_id']) : nil
49
          content_source = hostgroup['content_source_id'] ? foreman_host(:id => hostgroup['content_source_id']) : nil
50
          parent = hostgroup['ancestry'] ? foreman_hostgroup(:id => hostgroup['ancestry']) : nil
51
          password = nil
52
          puppet_classes = export_column(hostgroup, 'puppetclasses') do |puppet_class|
53
            "#{puppet_class['module_name']}/#{puppet_class['name']}"
54
          end
55

    
56
          # TODO: http://projects.theforeman.org/issues/6273
57
          # API call to get the smart class variable override values
58

    
59
          csv << [name, parent, organizations, locations, subnet, domain, operating_system,
60
                  puppet_environment, compute_profile, partition_table, medium, architecture,
61
                  realm, puppet_proxy, puppet_ca_proxy, content_source, password, puppet_classes]
62
        end
63
      end
64

    
65
      def import
66
        @existing = {}
67
        @api.resource(:hostgroups).call(:index, {:per_page => 999999})['results'].each do |host_group|
68
          @existing[host_group['name']] = host_group['id'] if host_group
69
        end
70

    
71
        thread_import do |line|
72
          create_from_csv(line)
73
        end
74
      end
75

    
76
      def create_from_csv(line)
77
        return if option_organization && !CSV.parse_line(line[ORGANIZATIONS], {:skip_blanks => true}).include?(option_organization)
78

    
79
        params = {
80
          'hostgroup' => {
81
            'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
82
            'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATING_SYSTEM]),
83
            'medium_id' => foreman_medium(:name => line[MEDIUM]),
84
            'ptable_id' => foreman_partitiontable(:name => line[PARTITION_TABLE]),
85
            'root_pass' => line[PASSWORD],
86
            'organization_ids' => collect_column(line[ORGANIZATIONS]) do |organization|
87
              foreman_organization(:name => organization)
88
            end,
89
            'location_ids' => collect_column(line[LOCATIONS]) do |location|
90
              foreman_location(:name => location)
91
            end
92
          }
93
        }
94

    
95
        count(line[COUNT]).times do |number|
96
          name = namify(line[NAME], number)
97
          params['hostgroup']['name'] = name
98

    
99
          if !@existing.include? name
100
            print "Creating host group '#{name}'..." if option_verbose?
101
            hostgroup = @api.resource(:hostgroups).call(:create, params)
102
            @existing[name] = hostgroup['id']
103
          else
104
            print "Updating host '#{name}'..." if option_verbose?
105
            params['id'] = @existing[name]
106
            hostgroup = @api.resource(:hostgroups).call(:update, params)
107
          end
108

    
109
          # TODO: puppet classes
110
          puppetclass_ids = collect_column(line[PUPPET_CLASSES]) do |puppet_class|
111
            module_name, name = puppet_class.split('/')
112
            foreman_puppet_class(:name => name)
113
          end
114
          existing_ids = hostgroup['puppet_classes'].collect { |puppet_class| puppet_class['id'] }
115
          # DELETE existing_ids - puppetclass_ids
116
          # POST puppetclass_ids - existing_ids
117

    
118
          print "done\n" if option_verbose?
119
        end
120
      end
121
    end
122
  end
123
end