Project

General

Profile

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

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

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
        search_options['search'] = "#{search_options['search']} AND #{option_search}" if option_search
32
        @api.resource(:hostgroups).call(:index, search_options)['results'].each do |hostgroup|
33
          hostgroup = @api.resource(:hostgroups).call(:show, {'id' => hostgroup['id']})
34
          raise "Host Group 'id=#{hostgroup['id']}' not found" if !hostgroup || hostgroup.empty?
35

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

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

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

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

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

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

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

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

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

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

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