Class: Collins::CLI::IPAM
- Inherits:
-
Object
- Object
- Collins::CLI::IPAM
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
-
#initialize ⇒ IPAM
constructor
A new instance of IPAM.
- #parse!(argv = ARGV) ⇒ Object
- #run! ⇒ Object
- #validate! ⇒ Object
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
#initialize ⇒ IPAM
19 20 21 22 23 |
# File 'lib/collins/cli/ipam.rb', line 19 def initialize = DEFAULT_OPTIONS.clone @parsed, @validated = false, false @parser = nil end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/collins/cli/ipam.rb', line 17 def end |
#parser ⇒ Object (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. = "Usage: #{PROG_NAME} [options]" opts.separator "" opts.on('-s','--show-pools',"Show IP pools") {|v| [:mode] = :show } opts.on('-H','--show-header',"Show header fields in --show-pools output") {|v| [:show_header] = true } opts.on('-a','--allocate POOL',String,"Allocate addresses in POOL") {|v| [:mode] = :allocate ; [:pool] = v } opts.on('-n','--number [NUM]',Integer,"Allocate NUM addresses (Defaults to 1 if omitted)") {|v| [:num] = v || 1 } opts.on('-d','--delete [POOL]',String,"Delete addresses in POOL. Deletes ALL addresses if POOL is omitted") {|v| [:mode] = :delete ; [:pool] = v } opts.separator "" opts.separator "General:" opts.on('-t','--tags TAG[,...]',Array,"Tags to work on, comma separated") {|v| [:tags] = v.map(&:to_sym)} opts.on('-C','--config CONFIG',String,'Use specific Collins config yaml for Collins::Client') {|v| [:config] = v} opts.on('-h','--help',"Help") {[: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? [:mode] && ([:tags].nil? or [:tags].empty?) # read tags from stdin. first field on the line is the tag input = ARGF.readlines [: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 [:mode] when :help puts parser when :show pools = collins.ipaddress_pools format_pools(pools, :show_header => [:show_header]) when :allocate [:tags].each do |t| res = api_call("allocating #{options[:num]} IP in #{options[:pool]}",:ipaddress_allocate!,t,[:pool],[:num]) do |addresses| "Allocated #{addresses.map(&:address).join(' ')}" end success = false unless res end when :delete [:tags].each do |t| res = api_call("deleting all IPs#{" in #{options[:pool]}" unless options[:pool].nil?}",:ipaddress_delete!,t,[: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 [:mode].nil? raise "No asset tags found via ARGF" if [:allocate,:delete].include?([:mode]) && ([:tags].nil? or [:tags].empty?) @validated = true self end |