require 'oci'

my_config = OCI::ConfigFileLoader.load_config(config_file_location:'./.oci/config', profile_name:'DEFAULT')

compartment_id = "ocid1.compartment.oc1..aaaaaaaakgheggu32wbfvthjv7shxhfq46bizan2vwcwmazyl3zikeb2xxla"
compute_client = OCI::Core::ComputeClient.new(config:my_config)
vcn_client = OCI::Core::VirtualNetworkClient.new(config:my_config)

def displayInstances(compute_client, vcn_client, compartment_id)
  puts 'Display Instances...'

  response = compute_client.list_instances(compartment_id)
  response.data.each do |instance|
    puts "Instance: '#{instance}'"
    puts "Instance: '#{instance.display_name}' [#{instance.id} - #{instance.lifecycle_state}]"
    if (instance.lifecycle_state == "RUNNING")
      compute_client.list_vnic_attachments(instance.compartment_id, {:instance_id=>instance.id}).each do |vnicResponse|
        vnicResponse.data.each do |vnic_attachment|
          vnic = vcn_client.get_vnic(vnic_attachment.vnic_id)
          puts "  Private IP = '#{vnic.data.private_ip}'"
          puts "  Public IP  = '#{vnic.data.public_ip}'"
      end
    end
  end

  end
    puts ' '
end
    
def displayVCN(vcn_client, compartment_id)
  puts 'Display VCN...'

  vcnsResponse = vcn_client.list_vcns(compartment_id)
  vcnsResponse.data.each do |vcn|
    puts "VCN: #{vcn.display_name}"
    slResponse = vcn_client.list_security_lists(compartment_id, vcn.id)
    slResponse.data.each do |sl|
      puts "  Security List: #{sl.display_name}"
      sl.ingress_security_rules.each do |ingress|
        puts "    Ingress Rule: #{ingress}"
      end
      sl.egress_security_rules.each do |egress|
        puts "    Egress Rule: #{egress}"
      end
    end
  end
end

displayInstances(compute_client, vcn_client, compartment_id)
displayVCN(vcn_client, compartment_id)
