Class: HammerCLICsv::CsvCommand::SubnetsCommand

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

Constant Summary collapse

ORGANIZATIONS =
'Organizations'
LOCATIONS =
'Locations'
NETWORK =
'Network'
NETWORK_MASK =
'Network Mask'
NETWORK_FROM =
'From'
NETWORK_TO =
'To'
DOMAINS =
'Domains'
GATEWAY =
'Gateway'
DHCP_PROXY =
'DHCP Proxy'
TFTP_PROXY =
'TFTP Proxy'
DNS_PROXY =
'DNS Proxy'
DNS_PRIMARY =
'DNS Primary'
DNS_SECONDARY =
'DNS Secondary'
VLAN_ID =
'VLAN ID'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_subnets_from_csv(line) ⇒ Object



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
# File 'lib/hammer_cli_csv/subnets.rb', line 79

def create_subnets_from_csv(line)
  line[DOMAINS] = (CSV.parse_line(line[DOMAINS]) || []).collect do |domain|
    foreman_domain(:name => domain)
  end

  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)
    if !@existing.include? name
      print "Creating subnet '#{name}'..." if option_verbose?
      id = @api.resource(:subnets)\
        .call(:create, {
                'subnet' => {
                  'name' => name
                }
              })['id']
    else
      print "Updating subnet '#{name}'..." if option_verbose?
      id = @api.resource(:subnets)\
        .call(:update, {
                'id' => @existing[name],
                'subnet' => {
                  'name' => name,
                  'network' => line[NETWORK],
                  'mask' => line[NETWORK_MASK],
                  'from' => line[NETWORK_FROM],
                  'to' => line[NETWORK_TO],
                  'domain_ids' => line[DOMAINS]
                }
              })['id']
    end

    # Update associated resources
    associate_organizations(id, line[ORGANIZATIONS], 'subnet')
    associate_locations(id, line[LOCATIONS], 'subnet')

    print "done\n" if option_verbose?
  end
rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#exportObject



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
65
66
# File 'lib/hammer_cli_csv/subnets.rb', line 37

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
    csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, NETWORK, NETWORK_MASK,
            NETWORK_FROM, NETWORK_TO, DOMAINS, GATEWAY, DHCP_PROXY, TFTP_PROXY, DNS_PROXY,
            DNS_PRIMARY, DNS_SECONDARY, VLAN_ID]
    @api.resource(:subnets).call(:index, {:per_page => 999999})['results'].each do |subnet|
      subnet = @api.resource(:subnets).call(:show, {'id' => subnet['id']})

      name = subnet['name']
      count = 1
      organizations = export_column(subnet, 'organizations', 'name')
      locations = export_column(subnet, 'locations', 'name')
      network = subnet['network']
      network_mask = subnet['mask']
      network_from = subnet['from']
      network_to = subnet['to']
      domains = export_column(subnet, 'domains', 'name')
      gateway = subnet['gateway']
      dhcp_proxy = (subnet['dhcp'] && subnet['dhcp'].key?('name')) ? subnet['dhcp']['name'] : ''
      tftp_proxy = (subnet['tftp'] && subnet['tftp'].key?('name')) ? subnet['tftp']['name'] : ''
      dns_proxy = (subnet['dns'] && subnet['dns'].key?('name')) ? subnet['dns']['name'] : ''
      dns_primary = subnet['dns_primary']
      dns_secondary = subnet['dns_secondary']
      vlan_id = subnet['vlanid']
      csv << [name, count, organizations, locations, network, network_mask,
              network_from, network_to, domains, gateway, dhcp_proxy, tftp_proxy, dns_proxy,
              dns_primary, dns_secondary, vlan_id]
    end
  end
end

#importObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/hammer_cli_csv/subnets.rb', line 68

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

  thread_import do |line|
    create_subnets_from_csv(line)
  end
end