Class: HammerCliBluecat::BluecatCommand::SyncNetworkCommand

Inherits:
HammerCLI::AbstractCommand
  • Object
show all
Includes:
HammerCliBluecat
Defined in:
lib/hammer_cli_bluecat.rb

Constant Summary

Constants included from HammerCliBluecat

VERSION

Instance Method Summary collapse

Methods included from HammerCliBluecat

#bluecat, #domain_ids, #foreman, #location_ids, #organization_ids, #tftp_id

Instance Method Details

#add_to_catalog(entry) ⇒ Object



108
109
110
# File 'lib/hammer_cli_bluecat.rb', line 108

def add_to_catalog(entry)
  @catalog[entry[:resource][:network]] = entry
end

#bluecat_to_foreman_catalog(network) ⇒ Object

Foreman response from Bluecat into a unit of work.



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
# File 'lib/hammer_cli_bluecat.rb', line 78

def bluecat_to_foreman_catalog(network)
  cidr = NetAddr::CIDR.create(network[:ip_range])

  if network[:name]
    name = "#{network[:name]} (#{network[:ip_range]})"
  else
    name = network[:ip_range]
  end

  {
    :id => nil,
    :action => 'create',
    :resource => {
        :name => name,
        :network_type => 'IPv4',
        :network => cidr.network,
        :mask => cidr.wildcard_mask,
        :gateway => cidr.nth(1),
        :dns_primary => '152.1.14.14',
        :dns_secondary => '152.1.14.21',
        :ipam => 'None',
        :domain_ids => @domain_ids,
        :location_ids => @location_ids,
        :organization_ids => @organization_ids,
        :tftp_id => @tftp_id,
        :boot_mode => 'DHCP'
    }
  }
end

#executeObject

TODO: Consider tftp_id as an option instead of magik-ing it.



14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/hammer_cli_bluecat.rb', line 14

def execute
  # Get current domain identifiers...
  @domain_ids = domain_ids
  @location_ids = location_ids
  @organization_ids = organization_ids

  @tftp_id = tftp_id

  @catalog = {}

  # Build a list of subnet resources from Bluecat...
  bluecat do |client|
    client.ip4_networks(38537).map { |network| bluecat_to_foreman_catalog network }
      .each { |entry| add_to_catalog entry }
  end

  # Gather current Foreman subnets
  subnet_networks = foreman.resource(:subnets).call(:index, :per_page => 9999)['results'].map do |s|
    {
      :id => s['id'],
      :network => s['network'],
    }
  end

  # Mark catalog entries for update or deletion
  subnet_networks.each do |network|
    if @catalog.has_key?(network[:network])
      if has_changed network
        @catalog[network[:network]][:action] = 'update'
        @catalog[network[:network]][:id] = network[:id]
      end
    else
      @catalog[network[:network]] = {
        :id => network[:id],
        :action => 'delete'
      }
    end
  end

  process_catalog
  HammerCLI::EX_OK
end

#has_changed(network) ⇒ Object

Stub to determine whether an update is required



73
74
75
# File 'lib/hammer_cli_bluecat.rb', line 73

def has_changed(network)
  true
end

#process_catalogObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hammer_cli_bluecat.rb', line 57

def process_catalog
  @catalog.each do |network, entry|
    case entry[:action]
      when 'create'
        puts "Creating #{entry}.\n"
        foreman.resource(:subnets).call(:create, { :subnet => entry[:resource] })
      when 'update'
        puts "Updating #{entry}.\n"
        foreman.resource(:subnets).call(:update, { :id => entry[:id], :subnet => entry[:resource] })
      when 'delete'
        puts "#{entry} marked for deletion, skipping.\n"
    end
  end
end