# CNPROV middleware generated by 'make_middleware' on 2024-04-08_21:24:42
use ../aws/nulib/aws/env.nu
use ../aws/nulib/aws/servers.nu *
use ../aws/nulib/aws/cache.nu *
use ../aws/nulib/aws/prices.nu *
use ../local/nulib/local/env.nu
use ../local/nulib/local/servers.nu *
use ../upcloud/nulib/upcloud/env.nu
use ../upcloud/nulib/upcloud/servers.nu *
use ../upcloud/nulib/upcloud/cache.nu *
use ../upcloud/nulib/upcloud/prices.nu *
def provider_undefined [
    server: record
] {
    #use defs/lists.nu providers_list
    let str_providers_list = (providers_list "selection" | each { |it| $it.name} | str join " ")
    print ($"(_ansi blue_bold)($server.hostname)(_ansi reset) with provider " +
       $"(_ansi green_bold)($server.provider)(_ansi reset) ($server.zone) ")
    let text = ( $"expected to be one of available providers [(_ansi green_italic)($str_providers_list)(_ansi reset)], " +
        $"got (_ansi green_bold)($server.provider)(_ansi reset)")
    print $"Error 🛑 provider ($text)"
}
export def mw_query_servers [
    settings: record
    find?: string
    cols?: string
    --prov: string
    --serverpos: int
] {
    let str_find = if $find != null { $find } else { "" }
    let str_cols = if $cols != null { $cols } else { "" }
    $settings.data.servers | enumerate | each { |it|
    #let res = for idx in ..($settings.data.servers | length) {
        #let srv = ($settings.data.servers | get -o $idx)
        if $prov == null or $it.item.provider == $prov {
            if $serverpos == null or $serverpos == $it.index  {
                let res = match $it.item.provider {
                    "aws" => {
                        (aws_query_servers $str_find $str_cols)
                    },
                    "local" => {
                        (local_query_servers $str_find $str_cols)
                    },
                    "upcloud" => {
                        (upcloud_query_servers $str_find $str_cols)
                    },
                    _ => {
                        provider_undefined $it.item
                        []
                    }
                }
                if ($res | length) > 0 {
                    let result = if $str_find != "" {
                        $res | find $str_find
                    } else {
                        $res
                    }
                    if $str_cols != "" {
                        let field_list = ($str_cols | split row ",")
                        ($result | select -o $field_list)
                    } else {
                        $result
                    }
                }
            }
        }
     # $list | append  $srv
    } | flatten
}
export def mw_servers_ips [
    settings: record
    data: list
    prov?: string
    serverpos?: int
]: nothing -> list {
    mut index = -1
    mut result = []
    for srv in $data {
        $index += 1
        let settings_server = ($settings.data.servers | where {|it| $it.hostname == $srv.hostname})
        if ($settings_server | length) == 0 { continue }
        let provider = ($settings_server | get -o 0 | get -o provider | default "")
        if $prov != null and $provider != $prov { continue }
        if $serverpos != null and $serverpos != $index  { continue }
        match $provider {
            "aws" => {
                if $srv.ip_addresses? != null {
                    $result = ($result | append ($srv.ip_addresses? |
                        each {|it| { hostname: $srv.hostname, ip: $it.address, access: $it.access, family: $it.family }} |
                        flatten
                    ))
                }
            },
            "local" => {
                if $srv.ip_addresses? != null {
                    $result = ($result | append ($srv.ip_addresses? |
                        each {|it| { hostname: $srv.hostname, ip: $it.address, access: $it.access, family: $it.family }} |
                        flatten
                    ))
                }
            },
            "upcloud" => {
                if $srv.ip_addresses? != null {
                    $result = ($result | append ($srv.ip_addresses? |
                        each {|it| { hostname: $srv.hostname, ip: $it.address, access: $it.access, family: $it.family }} |
                        flatten
                    ))
                }
            },
            _ => {
                provider_undefined $srv.provider
                []
            }
        }
    }
    $result
}
export def mw_server_info [
    server: record
    check: bool
    find?: string
    cols?: string
]: nothing -> record {
    let str_find = if $find != null { $find } else { "" }
    let str_cols = if $cols != null { $cols } else { "" }
    let res = match $server.provider {
        "aws" => {
            (aws_server_info $server $check)
        },
        "local" => {
            (local_server_info $server $check)
        },
        "upcloud" => {
            (upcloud_server_info $server $check)
        },
        _ => {
           provider_undefined $server.hostname
           []
        }
    }
    if ($res | describe | str starts-with  "record") and $res.hostname? != null {
        let result = if $str_find != "" {
            $res | find $str_find
        } else {
            $res
        }
        let info = if $str_cols != "" {
            let field_list = ($str_cols | split row ",")
            ($result | select -o $field_list)
        } else {
            ($result)
        }
        let priv = match $server.provider {
            "aws" => {
                ($info | get -o private_ips | default [] | each {|it| ($it | select Description PrivateIpAddress VpcId SubnetId Groups) })
            },
            _ => ($info | get -o priv | default [])
        }
        let full_info = if ($priv | length) > 0 {
            ($info | merge { private_ips: $priv })
        } else {
            $info
        }
        let out = ($env | get -o PROVISIONING_OUT| default "")
        if ($out | is-empty) {
           print ($full_info | table -e)
        }
        if (not $check) {
           ($full_info | table -e)
        }
        $full_info
    } else {
        $res
    }
}
export def mw_servers_info [
    settings: record
    find?: string
    cols?: string
    --prov: string
    --serverpos: int
    --check
]: nothing -> list {
    let str_find = if $find != null { $find } else { "" }
    let str_cols = if $cols != null { $cols } else { "" }

   $settings.data.servers | enumerate | each { |it|
        if $prov == null or $it.item.provider == $prov {
            if $serverpos == null or $serverpos == $it.index  {
               mw_server_info $it.item $check $str_find $str_cols
            }
        }
    }
}
export def mw_create_server [
    settings: record
    server: record
    check: bool
    error_exit: bool
]: nothing -> bool {
    let zone = $server.zone? | default ""
    let res = match $server.provider {
        "aws" => {
            print (aws_on_prov_server $server)
            (aws_check_server_requirements $settings $server $check)
        },
        "local" => {
            print (local_on_prov_server $server)
            (local_check_server_requirements $settings $server $check)
        },
        "upcloud" => {
            print (upcloud_on_prov_server $server)
            (upcloud_check_server_requirements $settings $server $check)
        },
        _ => {
           provider_undefined $server
           if $error_exit { exit } else { false }
        }
    }
    if not $res {
        (throw-error $"🛑 ($server.provider) check requirements error"
        $"for server ($server.hostname)"
        "create_server" --span (metadata $server.provider).span)
        return false
    }
    print ($"Create (_ansi blue_bold)($server.hostname)(_ansi reset) with provider " +
       $"(_ansi green_bold)($server.provider)(_ansi reset) ($zone) ")
    return true
}
export def mw_server_state [
    server: record
    new_state: string
    error_exit: bool
    wait: bool
    settings: record
]: nothing -> bool {
    match $server.provider {
        "aws" => {
           (aws_server_state $server $new_state $error_exit $wait $settings)
        },
        "local" => {
           (local_server_state $server $new_state $error_exit $wait $settings)
        },
        "upcloud" => {
           (upcloud_server_state $server $new_state $error_exit $wait $settings)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return false }
        }
    }
    true
}
export def mw_server_exists [
    server: record
    error_exit: bool
]: nothing -> bool {
    match $server.provider {
       "aws" => {
            (aws_server_exists $server $error_exit)
        },
       "local" => {
            (local_server_exists $server $error_exit)
        },
       "upcloud" => {
            (upcloud_server_exists $server $error_exit)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { false }
        }
    }
}
export def mw_server_is_running [
    server: record
    error_exit: bool
]: nothing -> bool {
    match $server.provider {
        "aws" => {
            (aws_server_is_running $server $error_exit)
        },
        "local" => {
            (local_server_is_running $server $error_exit)
        },
        "upcloud" => {
            (upcloud_server_is_running $server $error_exit)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { false }
        }
    }
}
export def mw_get_ip [
    settings: record
    server: record
    ip_type: string
    error_exit: bool
]: nothing -> string {
    let use_type = match $ip_type {
        "$network_public_ip" => "public",
        "$network_private_ip" => "private",
        _ => $ip_type
    }
    let res = match $server.provider {
        "aws" => {
           (aws_server [ "get_ip", $use_type ] --server $server --settings $settings)
        },
        "local" => {
           (local_server [ "get_ip", $use_type ] --server $server --settings $settings)
        },
        "upcloud" => {
           (upcloud_server [ "get_ip", $use_type ] --server $server --settings $settings)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { "" }
        }
    }
    $"($res)" | str trim
}
export def mw_post_create_server [
    settings: record
    server: record
    check: bool
]: nothing -> bool {
   match $server.provider {
        "aws" => {
            (aws_post_create_server $settings $server $check)
        },
        "local" => {
            (local_post_create_server $settings $server $check)
        },
        "upcloud" => {
           (upcloud_post_create_server $settings $server $check)
        },
        _ => {
           provider_undefined $server.provider
           true
        }
    }
}
export def mw_modify_server [
    settings: record
    server: record
    new_values: list
    error_exit: bool
]: nothing -> bool {
   match $server.provider {
        "aws" => {
            (aws_modify_server $settings $server $new_values $error_exit)
        },
        "local" => {
            (local_modify_server $settings $server $new_values $error_exit)
        },
        "upcloud" => {
           (upcloud_modify_server $settings $server $new_values $error_exit)
        },
        _ => {
           provider_undefined $server.provider
           true
        }
    }
}
export def mw_delete_server_storage [
    settings: record
    server: record
    error_exit: bool
]: nothing -> bool {
    let zone = $server.zone? | default ""
    match $server.provider {
        "aws" => {
            print (aws_on_prov_server $server)
            (aws_delete_server_storage $settings $server $error_exit)
        },
        "local" => {
            print (local_on_prov_server $server)
            (local_delete_server_storage $settings $server $error_exit)
        },
        "upcloud" => {
            print (upcloud_on_prov_server $server)
            (upcloud_delete_server_storage $settings $server $error_exit)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { false }
        }
    }
}
export def mw_delete_server [
    settings: record
    server: record
    keep_storage: bool
    error_exit: bool
]: nothing -> bool {
    let zone = $server.zone? | default ""
    match $server.provider {
        "aws" => {
            print (aws_on_prov_server $server)
            (aws_delete_server $settings $server $keep_storage $error_exit)
        },
        "local" => {
            print (local_on_prov_server $server)
            (local_delete_server $settings $server $keep_storage $error_exit)
        },
        "upcloud" => {
            print (upcloud_on_prov_server $server)
            (upcloud_delete_server $settings $server $keep_storage $error_exit)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { false }
        }
   }
}
export def mw_load_infra_servers_info [
    settings: record
    server: record
    error_exit: bool
]: nothing -> record {
    match $server.provider {
        "aws" => {
            (aws_load_infra_servers_info $settings $server $error_exit)
        },
        "upcloud" => {
            (upcloud_load_infra_servers_info $settings $server $error_exit)
        },

        _ => {
            provider_undefined $server
            if $error_exit { exit } else { {} }
        }
   }
}
export def mw_load_infra_storages_info [
    settings: record
    server: record
    error_exit: bool
]: nothing -> record {
    match $server.provider {
        "aws" => {
            (aws_load_infra_storages_info $settings $server $error_exit)
        },
        "upcloud" => {
            (upcloud_load_infra_storages_info $settings $server $error_exit)
        },
        _ => {
           provider_undefined $server
            if $error_exit { exit } else { {} }
        }
    }
}
export def mw_get_infra_storage [
    server: record
    settings: record
    cloud_data: record
    error_exit: bool
]: nothing -> list  {
    match $server.provider {
        "aws" => {
           (aws_get_item_for_storage $server $settings $cloud_data)
        },
        "upcloud" => {
           (upcloud_get_item_for_storage $server $settings $cloud_data)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { [] }
        }
    }
}
export def mw_get_infra_item [
    server: record
    settings: record
    cloud_data: record
    error_exit: bool
]: nothing -> record {
    match $server.provider {
        "aws" => {
           (aws_get_item_for_server $server $settings $cloud_data)
        },
        "upcloud" => {
           (upcloud_get_item_for_server $server $settings $cloud_data)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return {} }
        }
    }
}
export def mw_get_infra_price [
    server: record
    data: record
    key:  string
    error_exit: bool
    price_col?: string
]: nothing -> float {
    if ($data | get -o item | is-empty) { return {} }
    match $server.provider {
        "aws" => {
           (aws_get_price $data $key $price_col)
       },
        "upcloud" => {
           (upcloud_get_price $data $key $price_col)
       },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return 0 }
        }
    }
}
export def mw_start_cache_info [
    settings: record
    server: record
]: nothing -> nothing {
    match $server.provider {
        "aws" => {
            (aws_start_cache_info $settings $server)
        },
        "upcloud" => {
            (upcloud_start_cache_info $settings $server)
        },
        _ => {
            provider_undefined $server
        }
    }
}
export def mw_create_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    match $server.provider {
        "aws" => {
            (aws_create_cache $settings $server $error_exit)
        },
        "upcloud" => {
            (upcloud_create_cache $settings $server $error_exit)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return 0 }
        }
    }
}
export def mw_read_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    match $server.provider {
        "aws" => {
            (aws_read_cache $settings $server $error_exit)
        },
        "upcloud" => {
            (upcloud_read_cache $settings $server $error_exit)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return }
       }
    }
}
export def mw_clean_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    match $server.provider {
        "aws" => {
            (aws_clean_cache $settings $server $error_exit)
        },
        "upcloud" => {
            (upcloud_clean_cache $settings $server $error_exit)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return }
        }
   }
}
export def mw_ip_from_cache [
    settings: record
    server: record
    error_exit: bool
]: nothing -> nothing {
    match $server.provider {
        "aws" => {
            (aws_ip_from_cache $settings $server $error_exit)
        },
        "upcloud" => {
            (upcloud_ip_from_cache $settings $server $error_exit)
        },
        "local" => {
            ($server | get -o network_public_ip | default "")
            #(local_ip_from_cache $settings $server $error_exit)
        },
        _ => {
            provider_undefined $server
            if $error_exit { exit } else { return }
        }
   }
}
