class Chef::Knife::OneandoneFirewallCreate

Public Instance Methods

run() click to toggle source
# File lib/chef/knife/oneandone_firewall_create.rb, line 44
def run
  $stdout.sync = true

  validate(config[:name], '-n NAME')
  validate(config[:protocol], 'at least one value for --protocol [PROTOCOL]')

  protocols = split_delimited_input(config[:protocol])
  ports_from = split_delimited_input(config[:port_from])
  ports_to = split_delimited_input(config[:port_to])
  sources = split_delimited_input(config[:source])

  validate_rules(ports_from, ports_to, protocols)

  rules = []

  for i in 0..(protocols.length - 1)
    rule = {
      'protocol' => protocols[i].upcase,
      'port_from' => ports_from[i].nil? ? nil : ports_from[i].to_i,
      'port_to' => ports_to[i].nil? ? nil : ports_to[i].to_i,
      'source' => sources[i]
    }
    rules << rule
  end

  init_client

  firewall = OneAndOne::Firewall.new
  response = firewall.create(name: config[:name], description: config[:description], rules: rules)

  if config[:wait]
    firewall.wait_for
    formated_output(firewall.get, true)
    puts "Firewall policy #{response['id']} is #{ui.color('created', :bold)}"
  else
    formated_output(response, true)
    puts "Firewall policy #{response['id']} is #{ui.color('being created', :bold)}"
  end
end
validate_rules(ports_from, ports_to, protocols) click to toggle source
# File lib/chef/knife/oneandone_firewall_create.rb, line 84
def validate_rules(ports_from, ports_to, protocols)
  if ports_from.length != ports_to.length
    ui.error('You must supply equal number of --port-from and --port-to values!')
    exit 1
  end

  if protocols.length < ports_from.length
    ui.error('It is required that the value count of --protocol >= --port-from value count!')
    exit 1
  end
end