class Object

Constants

IN4MASK
PORT_NAMES

Public Instance Methods

add_names(ports) click to toggle source
# File lib/terrafying/components/ports.rb, line 51
def add_names(ports)
  ports.map do |port|
    {
      type: 'tcp',
      name: PORT_NAMES.fetch(port[:upstream_port], port[:upstream_port].to_s)
    }.merge(port)
  end
end
add_redirects(ports) click to toggle source
# File lib/terrafying/components/ports.rb, line 28
def add_redirects(ports)
  ports.flat_map do |port|
    if port.key? :redirect_http_from_port
      redirect_port = redirect_http(port[:redirect_http_from_port], port[:upstream_port])
      port.delete(:redirect_http_from_port)
      return [port, redirect_port]
    end
    port
  end
end
add_upstream_downstream(ports) click to toggle source
# File lib/terrafying/components/ports.rb, line 16
def add_upstream_downstream(ports)
  ports.map do |port|
    port = { upstream_port: port, downstream_port: port } if port.is_a?(Numeric)

    if port.key?(:number)
      port[:upstream_port] = port[:number]
      port[:downstream_port] = port[:number]
    end
    port
  end
end
cidr_to_split_address(raw_cidr) click to toggle source
# File lib/terrafying/components/vpn.rb, line 11
def cidr_to_split_address(raw_cidr)
  cidr = NetAddr::CIDR.create(raw_cidr)

  masklen = 32 - cidr.bits
  maskaddr = ((IN4MASK >> masklen) << masklen)

  maskip = (0..3).map do |i|
    (maskaddr >> (24 - 8 * i)) & 0xff
  end.join('.')

  "#{cidr.first} #{maskip}"
end
enrich_ports(ports) click to toggle source
# File lib/terrafying/components/ports.rb, line 10
def enrich_ports(ports)
  ports = add_upstream_downstream(ports)
  ports = add_redirects(ports)
  add_names(ports)
end
from_port(port) click to toggle source
# File lib/terrafying/components/ports.rb, line 60
def from_port(port)
  return port unless port_range?(port)

  port.split('-').first.to_i
end
is_l4_port(port) click to toggle source
# File lib/terrafying/components/ports.rb, line 76
def is_l4_port(port)
  port[:type] == 'tcp' || port[:type] == 'udp'
end
is_l7_port(port) click to toggle source
# File lib/terrafying/components/ports.rb, line 80
def is_l7_port(port)
  port[:type] == 'http' || port[:type] == 'https'
end
port_range?(port) click to toggle source
# File lib/terrafying/components/ports.rb, line 72
def port_range?(port)
  port.is_a?(String) && port.match(/[0-9]+-[0-9]+/)
end
redirect_http(from_port, to_port) click to toggle source
# File lib/terrafying/components/ports.rb, line 39
def redirect_http(from_port, to_port)
  {
    upstream_port: from_port,
    downstream_port: from_port,
    type: 'http',
    action: {
      type: 'redirect',
      redirect: { port: to_port, protocol: 'HTTPS', status_code: 'HTTP_301' }
    }
  }
end
to_port(port) click to toggle source
# File lib/terrafying/components/ports.rb, line 66
def to_port(port)
  return port unless port_range?(port)

  port.split('-').last.to_i
end