Class: HammerCLICsv::CsvCommand::HostGroupsCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/host_groups.rb

Constant Summary collapse

PARENT =
'Parent Host Group'
ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'
SUBNET =
'Subnet'
DOMAIN =
'Domain'
OPERATING_SYSTEM =
'Operating System'
ENVIRONMENT =
'Puppet Environment'
COMPUTE_PROFILE =
'Compute Profile'
PARTITION_TABLE =
'Partition Table'
MEDIUM =
'Medium'
ARCHITECTURE =
'Architecture'
REALM =
'Realm'
PUPPET_PROXY =
'Puppet Proxy'
PUPPET_CA_PROXY =
'Puppet CA Proxy'
CONTENT_SOURCE =
'Content Source'
PASSWORD =
'Password'
PUPPET_CLASSES =
'Puppet Classes'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#apipie_check_param, #associate_locations, #associate_organizations, #authenticate_request, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_medium, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #help, help_columns, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #supported?, supported?, #thread_import

Methods included from Utils::Config

#api_connection

Instance Method Details

#create_from_csv(line) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hammer_cli_csv/host_groups.rb', line 77

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

  params = {
    'hostgroup' => {
      'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
      'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATING_SYSTEM]),
      'medium_id' => foreman_medium(:name => line[MEDIUM]),
      'ptable_id' => foreman_partitiontable(:name => line[PARTITION_TABLE]),
      'root_pass' => line[PASSWORD],
      'organization_ids' => collect_column(line[ORGANIZATIONS]) do |organization|
        foreman_organization(:name => organization)
      end,
      'location_ids' => collect_column(line[LOCATIONS]) do |location|
        foreman_location(:name => location)
      end
    }
  }

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

    if !@existing.include? name
      print "Creating host group '#{name}'..." if option_verbose?
      hostgroup = @api.resource(:hostgroups).call(:create, params)
      @existing[name] = hostgroup['id']
    else
      print "Updating host '#{name}'..." if option_verbose?
      params['id'] = @existing[name]
      hostgroup = @api.resource(:hostgroups).call(:update, params)
    end

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

    print "done\n" if option_verbose?
  end
end

#export(csv) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hammer_cli_csv/host_groups.rb', line 25

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

    name = hostgroup['name']
    organizations = export_column(hostgroup, 'organizations', 'name')
    locations = export_column(hostgroup, 'locations', 'name')
    subnet = hostgroup['subnet_name']
    operating_system = hostgroup['operatingsystem_name']
    domain = hostgroup['domain_name']
    puppet_environment = hostgroup['environment_name']
    compute_profile = hostgroup['compute_profile_name']
    partition_table = hostgroup['ptable_name']
    medium = hostgroup['medium_name']
    architecture = hostgroup['architecture_name']
    realm = hostgroup['realm_name']
    puppet_proxy = hostgroup['puppet_proxy_id'] ? foreman_host(:id => hostgroup['puppet_proxy_id']) : nil
    puppet_ca_proxy = hostgroup['puppet_ca_proxy_id'] ? foreman_host(:id => hostgroup['puppet_ca_proxy_id']) : nil
    content_source = hostgroup['content_source_id'] ? foreman_host(:id => hostgroup['content_source_id']) : nil
    parent = hostgroup['ancestry'] ? foreman_hostgroup(:id => hostgroup['ancestry']) : nil
    password = nil
    puppet_classes = export_column(hostgroup, 'puppetclasses') do |puppet_class|
      "#{puppet_class['module_name']}/#{puppet_class['name']}"
    end

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

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

#importObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/hammer_cli_csv/host_groups.rb', line 66

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

  thread_import do |line|
    create_from_csv(line)
  end
end