require 'oci'
require_relative 'alphaSecurityList'

def createVCN(vcn_client, compartment_id, my_config)
  puts 'Creating VCN...'
  request = OCI::Core::Models::CreateVcnDetails.new(config:my_config)
  request.cidr_block = "10.0.0.0/16"
  request.display_name = "AlphaVCN"
  request.compartment_id = compartment_id
  response = vcn_client.create_vcn(request)

  vcn = response.data
  vcn_client.get_vcn(vcn.id).wait_until(:lifecycle_state, OCI::Core::Models::Vcn::LIFECYCLE_STATE_AVAILABLE, max_wait_seconds: 180)
  puts "Created VCN '#{vcn.display_name}' [#{vcn.id}]"
  vcn
end

def createInternetGateway(vcn_client, compartment_id, vcn, my_config)
  puts 'Creating Internet Gateway...'
  request = OCI::Core::Models::CreateInternetGatewayDetails.new(config:my_config)
  request.display_name = "AlphaIGW"
  request.compartment_id = compartment_id
  request.is_enabled = true
  request.vcn_id = vcn.id
  response = vcn_client.create_internet_gateway(request)

  internet_gateway = response.data
  vcn_client.get_internet_gateway(internet_gateway.id).wait_until(:lifecycle_state, OCI::Core::Models::InternetGateway::LIFECYCLE_STATE_AVAILABLE, max_wait_seconds: 180)
  puts "Created Internet Gateway '#{internet_gateway.display_name}' [#{internet_gateway.id}]"
  internet_gateway
end

def addIGWRouteRule(vcn_client, internet_gateway, vcn, my_config)
  puts 'Updating route table...'
  route_rule = OCI::Core::Models::RouteRule.new(config:my_config)
  route_rule.cidr_block = '0.0.0.0/0'
  route_rule.network_entity_id = internet_gateway.id

  request = OCI::Core::Models::UpdateRouteTableDetails.new(config:my_config)
  request.route_rules = [route_rule]
  vcn_client.update_route_table(vcn.default_route_table_id, request)

  vcn_client.get_route_table(vcn.default_route_table_id).wait_until(:lifecycle_state, OCI::Core::Models::RouteTable::LIFECYCLE_STATE_AVAILABLE, max_wait_seconds: 180)
  puts "Updated route table '#{vcn.default_route_table_id}'"
end

def createDBSecurityList(vcn_client, compartment_id, vcn, my_config)
  puts 'Create Database Security List ...'
  dbSecurityList = constructDBSecurityList(vcn_client, compartment_id, vcn, my_config)
  response = vcn_client.create_security_list(dbSecurityList)
  security_list = response.data
  vcn_client.get_security_list(security_list.id).wait_until(:lifecycle_state, OCI::Core::Models::SecurityList::LIFECYCLE_STATE_AVAILABLE, max_wait_seconds: 180)
  puts "Created Security List '#{security_list.display_name}' [#{security_list.id}]"
  security_list
end

def createMWSecurityList(vcn_client, compartment_id, vcn, my_config)
  puts 'Create Middleware Security List ...'
  mwSecurityList = constructMWSecurityList(vcn_client, compartment_id, vcn, my_config)
  response = vcn_client.create_security_list(mwSecurityList)
  security_list = response.data
  vcn_client.get_security_list(security_list.id).wait_until(:lifecycle_state, OCI::Core::Models::SecurityList::LIFECYCLE_STATE_AVAILABLE, max_wait_seconds: 180)
  puts "Created Security List '#{security_list.display_name}' [#{security_list.id}]"
  security_list
end

def createDBSubnet(vcn_client, vcn, compartment_id, my_config, sl_id, availability_domain)
  puts 'Creating DB Subnet...'
  request = OCI::Core::Models::CreateSubnetDetails.new(config:my_config)
  request.cidr_block = "10.0.1.0/24"
  request.availability_domain = availability_domain
  request.display_name = "AlphaDBSubnet1"
  request.compartment_id = compartment_id
  request.route_table_id = vcn.default_route_table_id
  request.vcn_id = vcn.id
  slArray = Array.new
  slArray.push (sl_id)
  request.security_list_ids = slArray
  response = vcn_client.create_subnet(request)

  subnet = response.data
  vcn_client.get_subnet(subnet.id).wait_until(:lifecycle_state, OCI::Core::Models::Subnet::LIFECYCLE_STATE_AVAILABLE, max_wait_seconds: 180)
  puts "Created DB Subnet '#{subnet.display_name}' [#{subnet.id}]"
  subnet
end

def createMWSubnet(vcn_client, vcn, compartment_id, my_config, sl_id, availability_domain)
  puts 'Creating MW Subnet...'
  request = OCI::Core::Models::CreateSubnetDetails.new(config:my_config)
  request.cidr_block = "10.0.2.0/24"
  request.availability_domain = availability_domain
  request.display_name = "AlphaMWSubnet1"
  request.compartment_id = compartment_id
  request.route_table_id = vcn.default_route_table_id
  request.vcn_id = vcn.id
  slArray = Array.new
  slArray.push (sl_id)
  request.security_list_ids = slArray
  response = vcn_client.create_subnet(request)

  subnet = response.data
  vcn_client.get_subnet(subnet.id).wait_until(:lifecycle_state, OCI::Core::Models::Subnet::LIFECYCLE_STATE_AVAILABLE, max_wait_seconds: 180)
  puts "Created MW Subnet '#{subnet.display_name}' [#{subnet.id}]"
  subnet
end

def updateDBSecurityLists(vcn_client, security_list)
  updateSL = constructDBUpdatedSecurityList(vcn_client, security_list)
  updateResponse = vcn_client.update_security_list(security_list.id, updateSL)
end

