Class: Chef::Knife::OneandoneFirewallCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
OneandoneBase, Oneandone::Helpers
Defined in:
lib/chef/knife/oneandone_firewall_create.rb

Instance Method Summary collapse

Methods included from Oneandone::Helpers

#split_delimited_input, #validate

Methods included from OneandoneBase

#formated_output, included, #init_client

Instance Method Details

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# 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