#!/usr/bin/env nu
# Info: AWS

use lib.nu *
use ../../../../core/nulib/lib_provisioning/config/accessor.nu *

export def aws_start_cache_info [
   settings: record 
   server: record 
] {
  ( $"# To start from scratch set 'vpc' 'subnet' 'sg.id' to '?' then new AWS settings will be collected. This will create 'sg.perms'.\n" + 
    $"# Removing 'provider_path' and 'defs/aws_data.k' would fallback to defaults with no settings for 'sg.name' and 'sg.perms', etc.\n"
  )
} 

export def aws_create_cache [
  settings: record 
  server: record 
  error_exit: bool
] { 
   if $settings == null {
      if (is-debug-enabled) { print $"❗ No settings found " } 
      return
   }
   let provider_path = (get_provider_data_path $settings $server) 
   #use lib_provisioning/utils/settings.nu load_provider_env
   let data = (load_provider_env $settings $server $provider_path)
   if ($data | is-empty) or ($data | get -o main | get -o vpc) == "?"  { 
      aws_scan_settings "create" $provider_path $settings $server false
      let new_data = (load_provider_env $settings $server $provider_path)
      if ($new_data | is-empty) or ($new_data | get -o main | get -o vpc) == "?"  { 
         print $"❗AWS no valid provider settings for (_ansi red)($server.hostname)(_ansi reset)"
         exit 1
      }
   } else { 
      if (is-debug-enabled) {
         print $"aws main data already exists in ($provider_path | path basename)"
      }
   }
   aws_scan_servers $provider_path $settings $server 
   if (is-debug-enabled) {  print $"Cache for ($server.provider) on ($server.hostname) saved in: ($provider_path | path basename)" }
   # load_provider_env $settings $server $provider_path
}
export def aws_read_cache [
  settings: record 
  server: record 
  error_exit: bool
] { 
   if $settings == null {
      print $"❗ No settings found "
      return
   }
}
export def aws_clean_cache [
  settings: record 
  server: record 
  error_exit: bool
] { 
   if $settings == null {
      print $"❗ No settings found "
      return
   }
   let provider_path = (get_provider_data_path $settings $server) 
   let data = if ($provider_path | path exists) { 
      open $provider_path
   } else { 
      { servers: null }
   }
   if ($data.servers? != null) and ($data.servers | where {|it| ($it.hostname? | default "") == $server.hostname} | length) == 0 { 
      if (is-debug-enabled) {
         print $"❗server ($server.hostname) already deleted from ($provider_path | path basename)"  
      }
   }
   let all_servers = ( $data.servers? | default []  | where {|it| $it.hostname != $server.hostname}) 
   if (is-debug-enabled) {  print $"Cache for ($server.provider) delete ($server.hostname) in: ($provider_path | path basename)" }
   let new_data = if ($all_servers | length) == 0 { 
      aws_delete_settings "all" $provider_path $settings $server
      {}
   } else {
      ( $data | merge { servers: $all_servers})
   }
   save_provider_env $new_data $settings $provider_path
}
export def aws_ip_from_cache [
  settings: record 
  server: record 
  error_exit: bool
] { 
   let prov_settings = ($settings.providers | find $server.provider ) #|  get -o settings) 
   if ($prov_settings | is-empty) == null { return "" }
   ($prov_settings | flatten | find $server.hostname | select -o ip_addresses | find "public"| get -o address | get -o 0 | default "")
}