require 'oci'

compartment_id = "ocid1.compartment.oc1..aaaaaaaa7u3evhcuylmvk32miyjzqwkwov3ckkij3t4hv3ytffuum7p7ni3a"
gsebmcs00001_13_compartment_id = "ocid1.compartment.oc1..aaaaaaaaymztafvorbatzvgosdfsujarypdn3nvsj73qklegjzf4fppzbtaq"
gsebmcs00001_17_compartment_id = "ocid1.compartment.oc1..aaaaaaaadh7wv3lc4qykncennuvhp5clvd5fba3h5d2afgpdsfjgjt6cdb7a"
gsebmcs00001_18_compartment_id = "ocid1.compartment.oc1..aaaaaaaarfzuy7lq6i6vq7kgneuqein3366jnjlidvayv2duamlgt4hef3tq"
gsebmcs00001_19_compartment_id = "ocid1.compartment.oc1..aaaaaaaalrxsqcqqrhae4nfs63y66hjj3qaiyku2qkzz3iw7k677ts56ruja"
gsebmcs00001_25_compartment_id = "ocid1.compartment.oc1..aaaaaaaap4fkhnu6sdldjs5awxfyuryt3ecfyofo6docri7472337lq6vpxq"
gsebmcs00001_41_compartment_id = "ocid1.compartment.oc1..aaaaaaaaudh5wwpzbwtkjbic5hdfcxiv6du5pyjgb6fwqqvb3ac6o5t3obsa"
gsebmcs00001_47_compartment_id = "ocid1.compartment.oc1..aaaaaaaaw4c5mupzoaiin2lkzcpidgva673gjc6olylstd7rgqxqzeckplyq"
gsebmcs00001_48_compartment_id = "ocid1.compartment.oc1..aaaaaaaakxjgdofbhi3kc3xcepkjk4bsjhgbqv2fahvppoi6olwx5bz5tkoa"
gsebmcs00002_02_compartment_id = "ocid1.compartment.oc1..aaaaaaaa4kkb7nptzln7n5ewdh3b3d6aclkktjojnfoced2wykcmozif37sq"
gsebmcs00002_03_compartment_id = "ocid1.compartment.oc1..aaaaaaaadb3mounhnfjqiet6qoywdezq6tgvv6fektxcwlxrafy7p4zo5zra"
gsebmcs00008_38_compartment_id = "ocid1.compartment.oc1..aaaaaaaahdg5uw6eia2ccxespaeglgnuubakkukf232hzkzrkuohzwizfjxq"
gsebmcs00008_47_compartment_id = "ocid1.compartment.oc1..aaaaaaaauo3fg4vumpv4zw7obhuqmc5hevrwm3tze6xhidelvkcgskgzvnsq"
gsebmcs00009_13_compartment_id = "ocid1.compartment.oc1..aaaaaaaakgheggu32wbfvthjv7shxhfq46bizan2vwcwmazyl3zikeb2xxla"
gsebmcs00009_48_compartment_id = "ocid1.compartment.oc1..aaaaaaaa7u3evhcuylmvk32miyjzqwkwov3ckkij3t4hv3ytffuum7p7ni3a"

my_config = OCI::ConfigFileLoader.load_config(config_file_location:'./.oci/config', profile_name:'DEFAULT')
compute_client = OCI::Core::ComputeClient.new(config:my_config)
vcn_client = OCI::Core::VirtualNetworkClient.new(config:my_config)

def deleteInstances(compute_client, compartment_id)
  puts 'Delete instances...'

  response = compute_client.list_instances(compartment_id)
  puts 'List Instance Status: ' + response.status.to_s
  response.data.each do |instance|
    #puts 'Instance: ' + instance.to_s
    $stdout.flush
    delResponse = compute_client.terminate_instance (instance.id)
    print "Waiting to reach terminated state."
    $stdout.flush
    compute_client.get_instance(instance.id).wait_until(:lifecycle_state, OCI::Core::Models::Instance::LIFECYCLE_STATE_TERMINATED) { |response|
      print '.'
      $stdout.flush
    }
  end
end

def deleteNetwork(vcn_client, compartment_id)
  puts 'Delete VCN...'

  response = vcn_client.list_vcns(compartment_id)
  puts 'List VCN Status: ' + response.status.to_s
  response.data.each do |instance|
    puts 'VCN: ' + instance.to_s
    vcnId = instance.id
    subnetResponse = vcn_client.list_subnets(compartment_id, vcnId)
    subnetResponse.data.each do |subnetInstance|
      puts "Terminating Subnet '#{subnetInstance.display_name}' [#{subnetInstance.id}]"
      vcn_client.delete_subnet(subnetInstance.id)
      print "Waiting to reach terminated state."
      $stdout.flush
      begin
        vcn_client.get_subnet(subnetInstance.id).wait_until(:lifecycle_state, OCI::Core::Models::Subnet::LIFECYCLE_STATE_TERMINATED) { |response|
          print '.'
          $stdout.flush
        }
      rescue
        sleep (10)
      end
    end 
    slResponse = vcn_client.list_security_lists(compartment_id, vcnId)
    slResponse.data.each do |slInstance|
      if !(slInstance.display_name.include? "Default")
        puts "Terminating Security List '#{slInstance.display_name}' [#{slInstance.id}]"
        vcn_client.delete_security_list(slInstance.id)
        print "Waiting to reach terminated state."
        $stdout.flush
        begin
          vcn_client.get_security_list(slInstance.id).wait_until(:lifecycle_state, OCI::Core::Models::SecurityList::LIFECYCLE_STATE_TERMINATED) { |response|
            print '.'
            $stdout.flush
          }
        rescue
          sleep (10)
        end
      end 
    end 
    puts "Terminating VCN '#{instance.display_name}' [#{instance.id}]"
    vcn_client.delete_vcn(instance.id)
    print "Waiting to reach terminated state."
    $stdout.flush
    begin
      vcn_client.get_vcn(instance.id).wait_until(:lifecycle_state, OCI::Core::Models::Vcn::LIFECYCLE_STATE_TERMINATED) { |response|
        print '.'
        $stdout.flush
      }
    rescue
      sleep (5)
    end
  end 
end

deleteInstances(compute_client, compartment_id)
deleteNetwork(vcn_client, compartment_id)
