Class: Simp::Cli::Config::Item::ClientNets

Inherits:
ListItem show all
Defined in:
lib/simp/cli/config/item/client_nets.rb

Instance Attribute Summary

Attributes inherited from ListItem

#allow_empty_list

Attributes inherited from Simp::Cli::Config::Item

#allow_user_apply, #config_items, #description, #die_on_apply_fail, #fact, #fail_on_missing_answer, #key, #next_items_tree, #silent, #skip_apply, #skip_query, #skip_yaml, #value

Instance Method Summary collapse

Methods inherited from ListItem

#highline_question_type, #not_valid_message, #query_extras, #validate

Methods inherited from Simp::Cli::Config::Item

#apply, #default_value, #highline_question_type, #next_items, #not_valid_message, #print_banner, #print_summary, #puppet_value, #query, #query_ask, #query_extras, #query_status, #safe_apply, #say_blue, #say_green, #say_red, #say_yellow, #to_yaml_s, #validate

Constructor Details

#initializeClientNets

Returns a new instance of ClientNets.



9
10
11
12
13
14
15
16
17
# File 'lib/simp/cli/config/item/client_nets.rb', line 9

def initialize
  super
  @key         = 'client_nets'
  @description = %Q{
    A list of client networks for your systems, in CIDR notation.
    If you need this to be more (or less) restrictive for a given class,
    you can override it in Hiera.}.gsub(/^\s+/, '' )
  @allow_empty_list = false
end

Instance Method Details

#os_valueObject



19
20
21
22
23
# File 'lib/simp/cli/config/item/client_nets.rb', line 19

def os_value
  # NOTE: the logic that would normally go here is in recommended_value
  # client_nets is an administrative concept, not an os configuration
  nil
end

infer base network/CIDR values from IP/netmask



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simp/cli/config/item/client_nets.rb', line 26

def recommended_value
  begin
    address = @config_items.fetch('ipaddress').value
    nm      = @config_items.fetch('netmask').value
  rescue IndexError => e
    say_yellow("WARNING: #{e}") if !@silent
    return nil
  end

  # snarfed from:
  #   http://stackoverflow.com/questions/1825928/netmask-to-cidr-in-ruby
  subnet = IPAddr.new( nm ).to_i.to_s( 2 ).count('1')

  mucky_cidr = "#{address}/#{subnet}"
  cidr = "#{ IPAddr.new( mucky_cidr ).to_range.first.to_s}/#{subnet}"

  [ cidr ]
end

#validate_item(net) ⇒ Object

validate subnet



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/simp/cli/config/item/client_nets.rb', line 46

def validate_item( net )
  ### warn "'#{net}' doesn't end like a CIDR";
  return false if net !~ %r{/\d+$}

  ### warn "list item '#{net}' is not in proper CIDR notation";
  return false if net.split('/').size > 2

  subnet,cidr = net.split('/')

  # NOTE: if we support IPv6, we should redo netmask & validations
  ### warn "subnet '#{subnet}' is not a valid IP!";
  return false if !((subnet =~ Resolv::IPv4::Regex) || (subnet =~ Resolv::IPv6::Regex))

  ### warn "/#{cidr} is not a valid CIDR suffix";
  return false if !(cidr.to_i >= 0 && cidr.to_i <= 32)

  true
end