Class: Collins::CLI::IPAM

Inherits:
Object
  • Object
show all
Includes:
Formatter, Mixins
Defined in:
lib/collins/cli/ipam.rb

Constant Summary collapse

PROG_NAME =
'collins ipam'
DEFAULT_OPTIONS =
{
  :timeout => 120,
  :mode => nil,
  :show_header => false,
  :num => 1,
  :tags => [],
}

Constants included from Formatter

Formatter::ADDRESS_POOL_COLUMNS, Formatter::FORMATTING_DEFAULTS, Formatter::STATUS_STATE_COLUMNS

Constants included from Mixins

Mixins::COLORS, Mixins::ERROR, Mixins::SUCCESS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Formatter

#display_as_link, #display_as_robot_talk, #display_as_table, #format_assets, #format_pools, #format_states

Methods included from Mixins

#api_call, #as_query?, #collins, #convert_to_query

Constructor Details

#initializeIPAM



19
20
21
22
23
# File 'lib/collins/cli/ipam.rb', line 19

def initialize
  @options = DEFAULT_OPTIONS.clone
  @parsed, @validated = false, false
  @parser = nil
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/collins/cli/ipam.rb', line 17

def options
  @options
end

#parserObject (readonly)

Returns the value of attribute parser.



17
18
19
# File 'lib/collins/cli/ipam.rb', line 17

def parser
  @parser
end

Instance Method Details

#parse!(argv = ARGV) ⇒ 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
# File 'lib/collins/cli/ipam.rb', line 25

def parse!(argv = ARGV)
  @parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{PROG_NAME} [options]"
    opts.separator ""
    opts.on('-s','--show-pools',"Show IP pools") {|v| @options[:mode] = :show }
    opts.on('-H','--show-header',"Show header fields in --show-pools output") {|v| @options[:show_header] = true }
    opts.on('-a','--allocate POOL',String,"Allocate addresses in POOL") {|v| @options[:mode] = :allocate ; @options[:pool] = v }
    opts.on('-n','--number [NUM]',Integer,"Allocate NUM addresses (Defaults to 1 if omitted)") {|v| @options[:num] = v || 1 }
    opts.on('-d','--delete [POOL]',String,"Delete addresses in POOL. Deletes ALL addresses if POOL is omitted") {|v| @options[:mode] = :delete ; @options[:pool] = v }

    opts.separator ""
    opts.separator "General:"
    opts.on('-t','--tags TAG[,...]',Array,"Tags to work on, comma separated") {|v| @options[:tags] = v.map(&:to_sym)}
    opts.on('-C','--config CONFIG',String,'Use specific Collins config yaml for Collins::Client') {|v| @options[:config] = v}
    opts.on('-h','--help',"Help") {@options[:mode] = :help}

    opts.separator ""
    opts.separator "Examples:"
    opts.separator "  Show configured IP address pools:"
    opts.separator "    #{PROG_NAME} --show-pools -H"
    opts.separator "  Allocate 2 IPs on each asset"
    opts.separator "    #{PROG_NAME} -t 001234,003456,007895 -a DEV_POOL -n2"
    opts.separator "  Deallocate IPs in DEV_POOL pool on assets:"
    opts.separator "    #{PROG_NAME} -t 001234,003456,007895 -d DEV_POOL"
    opts.separator "  Deallocate ALL IPs on assets:"
    opts.separator "    #{PROG_NAME} -t 001234,003456,007895 -d"
  end
  @parser.parse!(argv)

  # only read tags from ARGF if we are going to do something with the tags
  if [:allocate,:delete].include? options[:mode] && (options[:tags].nil? or options[:tags].empty?)
    # read tags from stdin. first field on the line is the tag
    input = ARGF.readlines
    @options[:tags] = input.map{|l| l.split(/\s+/)[0] rescue nil}.compact.uniq
  end
  @parsed = true
  self
end

#run!Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/collins/cli/ipam.rb', line 71

def run!
  raise "Options not yet parsed with #parse!" unless @parsed
  raise "Options not yet validated with #validate!" unless @validated
  success = true
  case options[:mode]
  when :help
    puts parser
  when :show
    pools = collins.ipaddress_pools
    format_pools(pools, :show_header => options[:show_header])
  when :allocate
    options[:tags].each do |t|
      res = api_call("allocating #{options[:num]} IP in #{options[:pool]}",:ipaddress_allocate!,t,options[:pool],options[:num]) do |addresses|
        "Allocated #{addresses.map(&:address).join(' ')}"
      end
      success = false unless res
    end
  when :delete
    options[:tags].each do |t|
      res = api_call("deleting all IPs#{" in #{options[:pool]}" unless options[:pool].nil?}",:ipaddress_delete!,t,options[:pool]) { |count| "Deleted #{count} IPs" }
      success = false unless res
    end
  end
  success
end

#validate!Object



64
65
66
67
68
69
# File 'lib/collins/cli/ipam.rb', line 64

def validate!
  raise "You need to tell me to do something!" if @options[:mode].nil?
  raise "No asset tags found via ARGF" if [:allocate,:delete].include?(options[:mode]) && (options[:tags].nil? or options[:tags].empty?)
  @validated = true
  self
end