require 'oci'

def constructSSHIngressSecurityList ()
  ingressRule = OCI::Core::Models::IngressSecurityRule.new ()
  ingressRule.protocol = "6"
  ingressRule.source = "0.0.0.0/0"
  myTcpOptions = OCI::Core::Models::TcpOptions.new ()
  myTcpOptions.destination_port_range = OCI::Core::Models::PortRange.new ({:max=>22, :min=>22})
  ingressRule.tcp_options = myTcpOptions
  ingressRule
end

def constructEMIngressSecurityList ()
  ingressRule = OCI::Core::Models::IngressSecurityRule.new ()
  ingressRule.protocol = "6"
  ingressRule.source = "0.0.0.0/0"
  myTcpOptions = OCI::Core::Models::TcpOptions.new ()
  myTcpOptions.destination_port_range = OCI::Core::Models::PortRange.new ({:max=>5500, :min=>5500})
  ingressRule.tcp_options = myTcpOptions
  ingressRule
end

def constructPrivateSSHIngressSecurityList ()
  ingressRule = OCI::Core::Models::IngressSecurityRule.new ()
  ingressRule.protocol = "6"
  ingressRule.source = "10.0.2.0/24"
  myTcpOptions = OCI::Core::Models::TcpOptions.new ()
  myTcpOptions.destination_port_range = OCI::Core::Models::PortRange.new ({:max=>22, :min=>22})
  ingressRule.tcp_options = myTcpOptions
  ingressRule
end

def constructMySQLSecurityList ()
  mySQLRule = OCI::Core::Models::IngressSecurityRule.new ()
  mySQLRule.protocol = "6"
  mySQLRule.source = "10.0.2.0/24"
  myTcpOptions = OCI::Core::Models::TcpOptions.new ()
  myTcpOptions.destination_port_range = OCI::Core::Models::PortRange.new ({:max=>3306, :min=>3306})
  mySQLRule.tcp_options = myTcpOptions
  mySQLRule
end

def constructNodeSecurityList ()
  nodeJSRule = OCI::Core::Models::IngressSecurityRule.new ()
  nodeJSRule.protocol = "6"
  nodeJSRule.source = "0.0.0.0/0"
  myTcpOptions = OCI::Core::Models::TcpOptions.new ()
  myTcpOptions.destination_port_range = OCI::Core::Models::PortRange.new ({:max=>8080, :min=>8080})
  nodeJSRule.tcp_options = myTcpOptions
  nodeJSRule
end

def constructAllEgressSecurityList ()
  egressRule = OCI::Core::Models::EgressSecurityRule.new ()
  egressRule.protocol = "all"
  egressRule.destination = "0.0.0.0/0"
  egressRule
end

def constructPrivateMySQLEgressSecurityList ()
  egressRule = OCI::Core::Models::EgressSecurityRule.new ()
  egressRule.protocol = "6"
  egressRule.destination = "10.0.2.0/24"
  myTcpOptions = OCI::Core::Models::TcpOptions.new ()
  myTcpOptions.destination_port_range = OCI::Core::Models::PortRange.new ({:max=>3306, :min=>3306})
  egressRule.tcp_options = myTcpOptions
  egressRule
end

def constructDBSecurityList(vcn_client, compartment_id, vcn, my_config)
  puts 'Construct Database Security List ...'
  sl = OCI::Core::Models::CreateSecurityListDetails.new ()
  sl.compartment_id = compartment_id
  sl.display_name = "AlphaDBSL"
  sl.vcn_id = vcn.id
  ingressArray = Array.new
  ingressArray.push (constructSSHIngressSecurityList)
  ingressArray.push (constructEMIngressSecurityList)
  ingressArray.push (constructMySQLSecurityList)
  sl.ingress_security_rules = ingressArray
  egressArray = Array.new
  egressArray.push (constructAllEgressSecurityList)
  sl.egress_security_rules = egressArray
  sl
end

def constructMWSecurityList(vcn_client, compartment_id, vcn, my_config)
  puts 'Construct Middleware Security List ...'
  sl = OCI::Core::Models::CreateSecurityListDetails.new ()
  sl.compartment_id = compartment_id
  sl.display_name = "AlphaMWSL"
  sl.vcn_id = vcn.id
  ingressArray = Array.new
  ingressArray.push (constructSSHIngressSecurityList)
  ingressArray.push (constructNodeSecurityList)
  sl.ingress_security_rules = ingressArray
  egressArray = Array.new
  egressArray.push (constructAllEgressSecurityList)
  sl.egress_security_rules = egressArray
  sl
end

def constructDBUpdatedSecurityList(vcn_client, sl)
  puts 'Construct Updated Database Security List ...'
  updateSL = OCI::Core::Models::UpdateSecurityListDetails.new
  updateSL.display_name = sl.display_name
  ingressArray = Array.new
  ingressArray.push (constructPrivateSSHIngressSecurityList)
  ingressArray.push (constructMySQLSecurityList)
  updateSL.ingress_security_rules = ingressArray
  egressArray = Array.new
  egressArray.push (constructPrivateMySQLEgressSecurityList)
  #egressArray.push (constructAllEgressSecurityList)
  updateSL.egress_security_rules = egressArray
  updateSL
end
