ConfigManager.psm1

Import-Module $PSScriptRoot\Utils.psm1
Import-Module $PSScriptRoot\ClusUtils.psm1

$script:CMap = @{}

$script:CMap["turnKeySdnPath"] = ""
$script:CMap["storePath"] = ""
$script:CMap["state"] = "state.json"
$script:CMap["credential"] = "cred.json"
$script:CMap["workloadcredential"] = "workloadcred.json"
$script:CMap["userConfigPath"] = "$PSScriptRoot\config\deployment"
$script:CMap["deploymentConfigPath"] = "$PSScriptRoot\config\deployment"
$script:CMap["deploymentFile"] = "deployment.json"
$script:CMap["hnvpaNetworkFile"] = "networkconfig\logicalnetworks\hnvpa.json"
$script:CMap["mgmtNetworkFile"] = "networkconfig\logicalnetworks\management.json"
$script:CMap["greNetworkFile"] = "networkconfig\logicalnetworks\grevip.json"
$script:CMap["publicVIPNetworkFile"] = "networkconfig\logicalnetworks\publicvip.json"
$script:CMap["macpoolFile"] = "networkconfig\macpools\defaultMacPool.json"
$script:CMap["sdnConfigFile"] = "sdnconfig.json"

$script:CMap["sdnExpressConfigPath"] = "$PSScriptRoot\config\sdnexpress"
$script:CMap["sdnExpressTemplateFile"] = "template.psd1"

$script:CMap["userWorkloadConfigPath"] = "$PSScriptRoot\config\workload"
$script:CMap["workloadConfigPath"] = "$PSScriptRoot\config\workload"
$script:CMap["workload"] = "workloadconfig.json"

$script:CMap["vhdStore"] = ""

$script:MgmtHostNicName = "TurnKeySdn-Mgmt"

$script:DefaultTestVhdShare = "\\skyshare.redmond.corp.microsoft.com\Beanstalk\SDN_ADOArtifacts\TestArtifacts\mariner_workload_vhd"
$script:DefaultTestVhdFile = "full-2.0.20231004.vhdx"

$script:InstallStateInstalled = "installed"
$script:InstallStateInstallFailed = "installFailed"
$script:InstallStateInstalling = "installing"
$script:InstallStateNone = "none"

# Corp network routes, used on the management nic of gateway VMs to allow access to corp network
$script:MSFTCorpRoutes = @("10.0.0.0/8")
function Initialize-StorePath {

    if (-not [String]::IsNullOrEmpty($script:CMap["turnKeySdnPath"]) -and `
            -not ([String]::IsNullOrEmpty($script:CMap["storePath"]))) {
        return
    }

    $csv = Get-CSV

    if ($csv -ne $null) {
        $script:CMap["turnKeySdnPath"] = Join-Path $csv "TurnKeySdn"
        $script:CMap["vhdStore"] = Join-Path $csv "TurnKeySdnVhdStore"
    }
    else {
        $script:CMap["turnKeySdnPath"] = Join-Path $Env:SystemDrive "TurnKeySdn"
        $script:CMap["vhdStore"] = Join-Path $Env:SystemDrive "TurnKeySdnVhdStore"
    }

    $script:CMap["storePath"] = Join-Path $script:CMap["turnKeySdnPath"] ".store"

    if (Test-IsInitialized) {
        $script:CMap["deploymentConfigPath"] = Join-Path $script:CMap["turnKeySdnPath"] "DeploymentConfig"
        $script:CMap["workloadConfigPath"] = Join-Path $script:CMap["turnKeySdnPath"] "WorkloadConfig"
    }
}

function Undo-StorePathInitialization {
    $script:CMap["turnKeySdnPath"] = ""
    $script:CMap["storePath"] = ""
    $script:CMap["deploymentConfigPath"] = $script:CMap["userConfigPath"] 
    $script:CMap["workloadConfigPath"] = $script:CMap["userWorkloadConfigPath"]
    $script:CMap["vhdStore"] = ""

}

function Get-StorePath {
    Initialize-StorePath
    return $script:CMap["storePath"]
}

function Get-TurnKeySdnPath {
    Initialize-StorePath
    return $script:CMap["turnKeySdnPath"]
}

function Get-InternalDeploymentConfigPath {
    Initialize-StorePath
    return $script:CMap["deploymentConfigPath"]
}

function Get-InternalWorkloadConfigPath {
    Initialize-StorePath
    return $script:CMap["workloadConfigPath"]
}

function Get-VhdStore {
    Initialize-StorePath
    return $script:CMap["vhdStore"]
}

function Get-SdnExpressPath {
    $infraconfig = Get-DeploymentConfig
    $sdnExpPath = $infraconfig.sdnexpressPath
    if ([String]::IsNullOrEmpty($sdnExpPath)) {
        $sdnExpPath = Join-Path $PSScriptRoot "..\..\SDNExpress"
    }

    return $sdnExpPath
}

function Get-SdnExpressModule {
    $sdnExpPath = Get-SdnExpressPath
    $sdnexpress = Join-Path $sdnExpPath SDNExpress.psm1
    return $sdnexpress
}

function Get-SdnExpressScript {
    $sdnExpPath = Get-SdnExpressPath
    $sdnexpress = Join-Path $sdnExpPath SDNExpress.ps1
    return $sdnexpress
}

function Get-RestoreReplayScriptPath {
    $infraconfig = Get-DeploymentConfig
    $restoreReplayScriptPath = $infraconfig.restoreReplayScriptPath

    if ([String]::IsNullOrEmpty($restoreReplayScriptPath)) {
        $restoreReplayScriptPath = Join-Path $PSScriptRoot "..\BCDR"
    }

    return $restoreReplayScriptPath
}

function Get-RestoreReplayScript {
    $restoreReplayScriptPath = Get-RestoreReplayScriptPath
    $restoreReplay = Join-Path $restoreReplayScriptPath RestoreReplay.ps1
    return $restoreReplay
}

function Get-DefaultNetworkControllerPsModulePath {
    return Join-Path $env:SystemRoot "system32\WindowsPowerShell\v1.0\Modules\NetworkControllerFc"
}
function Get-DefaultNetworkControllerPackageLocation {
    return (Join-Path $env:SystemRoot "NetworkController")
}

function Get-DefaultNetworkControllerDBLocation {
    $csv = Get-CSV

    if ($csv -ne $null) {
        return (Join-Path $csv "FCNCDB")
    }

    return (Join-Path $env:SystemDrive "FCNCDB")
}

function Get-AddressRandmoizerSeed {

    $internalConfig = Get-TurnKeySdnInternalConfig -configType state
    if (-not [String]::IsNullOrEmpty($internalConfig.randomizerSeed)) {
        return [int]$internalConfig.randomizerSeed
    }

    $curhost = (hostname)
    $hostIndex = $curhost.Substring($($curhost.Length - 2))
    $result = 0

    if (-not [int]::TryParse($hostIndex, [ref]$result)) {
        $result = Get-Random -Minimum 1 -Maximum 60
    }

    return $result
}

function Get-RandomizedIPv4Address {
    param(
        $address,
        $randomizer
    )

    $addresses = $address.Split(".")
    $addresses[1] = ([int]$addresses[1] + $randomizer) % 255
    return $addresses -join '.'
}

function Update-IPv4Address {
    param(
        $Address
    )

    $depConfig = Get-DeploymentConfig

    if (-not $depConfig.randomizeAddresses) {
        return $Address
    }

    $randomizer = Get-AddressRandmoizerSeed
    $addr = Get-RandomizedIPv4Address -address $Address -randomizer $randomizer
    return $addr
}
function Update-LogicalNetworkAddresses {
    param(
        $network
    )

    $depConfig = Get-DeploymentConfig

    if (-not $depConfig.randomizeAddresses) {
        return $network
    }

    $randomizer = Get-AddressRandmoizerSeed
    $network.properties.subnets | ForEach-Object {
        $subnetProp = $_.properties
        $subnetProp.addressPrefix = Get-RandomizedIPv4Address -address $subnetProp.addressPrefix -randomizer $randomizer

        $newGateways = @()
        $subnetProp.defaultGateways | ForEach-Object {
            $newGateways += Get-RandomizedIPv4Address -address $_ -randomizer $randomizer
        }

        $subnetProp.defaultGateways = $newGateways

        $subnetProp.ipPools | Foreach-Object {
            $poolProp = $_.Properties
            $poolProp.startIpAddress = Get-RandomizedIPv4Address -address $poolProp.startIpAddress -randomizer $randomizer
            $poolProp.endIpAddress = Get-RandomizedIPv4Address -address $poolProp.endIpAddress -randomizer $randomizer
        }
    }
    return $network
}

function Set-TurnKeySdnInternalConfig {

    param(
        [ValidateSet("workloadcredential","credential", "state")]
        [parameter(Mandatory = $true)][string] $configType,
        [parameter(Mandatory = $true)] $config
    )

    $configFile = Join-Path (Get-StorePath) $script:CMap[$configType]
    $value = $(ConvertTo-Json $config -Depth 10)
    Set-Content -Path $configFile -Value $value -Encoding UTF8
}

function Get-TurnKeySdnInternalConfig {

    param(
        [ValidateSet("workloadcredential","credential", "state")]
        [parameter(Mandatory = $true)][string] $configType
    )

    $configFile = Join-Path (Get-StorePath) $script:CMap[$configType]
    return $(Get-Config $configFile)
}

function Get-TurnKeySdnCred {
    $configFile = Join-Path (Get-StorePath) $script:CMap["credential"]
    $c = Get-Config $configFile
    try {
        if ([String]::IsNullOrEmpty($c.username) -or [String]::IsNullOREmpty($c.password)) {
            throw "Credential is null"
        }
        $cred = [pscredential]::new($c.username, $($c.password | ConvertTo-SecureString -ErrorAction Stop))
    }
    catch {
        throw "Get-TurnKeySdnCred: Failed, retry after running Set-TurnKeySdnCredentials. Error $_"
    }
    return $cred
}
function Get-TurnKeySdnWorkloadVmCred {

    # todo: find a better way to store workload credentials
    # $configFile = Join-Path (Get-StorePath) $script:CMap["workloadcredential"]
    # $c = Get-Config $configFile
    
    # if([string]::IsNullOrEmpty($c.username) -or [string]::IsNullOrEmpty($c.password)) {

    # throw "Workload VMcredentials is not initialized. Please make sure $Env:TURNKEY_WORKLOAD_PASSWORD is set on the machine. Then run initialize again for TurnKey SDN"

    # } else {
    # $vmUserName = $c.username
    # $vmUserPassword = $c.password
    # }
    $vmUserName = $Env:TURNKEY_WORKLOAD_USERNAME
    $vmUserPassword = $Env:TURNKEY_WORKLOAD_PASSWORD
    return  [pscredential]::new($vmUserName, $($vmUserPassword | ConvertTo-SecureString  -AsPlainText -Force))
}


function Get-Config {
    param(
        [parameter(Mandatory = $true)][string] $file
    )

    Test-FileExist -path $file
    $config = Get-Content $file -ErrorAction Stop | ConvertFrom-Json
    return $config
}

function Get-GreVipLogicalNetworkConfig {
    param(
        [Parameter(Mandatory = $false)][bool] $RandomizeAddress = $true
    )

    $greNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["greNetworkFile"]
    $network = Get-Config $greNetworkFile
    if ($RandomizeAddress) {
        $network = Update-LogicalNetworkAddresses $network
    }
    return $network
}

function Get-HnvPaNetworkConfig {
    param(
        [Parameter(Mandatory = $false)][bool] $RandomizeAddress = $true
    )

    $hnvpaNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["hnvpaNetworkFile"]
    $hnvPaNetwork = Get-Config $hnvpaNetworkFile
    if ($RandomizeAddress) {
        $hnvPaNetwork = Update-LogicalNetworkAddresses $hnvPaNetwork
    }
    return $hnvPaNetwork
}

function Set-HnvPaNetworkConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$Network
    )

    $hnvpaNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["hnvpaNetworkFile"]
    $Network | ConvertTo-Json -Depth 100 | Out-File $hnvpaNetworkFile -Force
}

function Get-MgmtNetworkConfig {
    param(
        [Parameter(Mandatory = $false)][bool] $RandomizeAddress = $true
    )
    $mgmtNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["mgmtNetworkFile"]
    $mgmtNetwork = Get-Config $mgmtNetworkFile
    if ($RandomizeAddress) {
        $mgmtNetwork = Update-LogicalNetworkAddresses $mgmtNetwork
    }
    return $mgmtNetwork
}
function Set-MgmtNetworkConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$Network
    )

    $mgmtNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["mgmtNetworkFile"]
    $Network | ConvertTo-Json -Depth 100 | Out-File $mgmtNetworkFile -Force
}
function Get-PublicVipNetworkConfig {
    $publicVipNetworkFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["publicVipNetworkFile"]
    $publicVipNetwork = Get-Config $publicVipNetworkFile
    return $publicVipNetwork
}

function Get-DeploymentConfig {
    $config = Get-Config $(Get-DeploymentFile)
    return $config
}

function Update-MacAddress {
    param($mac)

    $macEle = $mac.Split("-")
    $rand = Get-AddressRandmoizerSeed
    $strRand = [string]$rand

    if ($strRand.Length -eq 1) {
        $strRand = "0$strRand"
    }

    $macEle[3] = $strRand
    $result = $macEle -join '-'

    return $result
}

function Get-DefaultMacPoolConfig {
    $configFileName = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["macpoolFile"]
    $config = Get-Config $configFileName

    $depConfig = Get-DeploymentConfig

    if ($depConfig.randomizeAddresses) {
        $config.properties.startMacAddress = Update-MacAddress -mac $config.properties.startMacAddress
        $config.properties.endMacAddress = Update-MacAddress -mac $config.properties.endMacAddress
    }

    return $config
}

function Get-SdnConfig {
    $configFileName = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["sdnConfigFile"]
    $config = Get-Config $configFileName
    return $config
}

function Get-TurnKeySdnDeploymentId {

    $configFileName = Join-Path (Get-StorePath) $script:CMap["state"]
    $config = Get-Config $configFileName
    if ([String]::IsNullOrEmpty($config.depId)) {
        $config.depId = ((New-Guid).guid).split("-")[1]
        Set-TurnKeySdnInternalConfig -configType state -config $config
    }

    return $config.depId
}

function Get-StateFileWithoutInit {
    $csv = Get-CSV

    if ($csv -ne $null) {
        $installPath = Join-Path $csv "TurnKeySdn"
    }
    else {
        $installPath = Join-Path $Env:SystemDrive "TurnKeySdn"
    }

    $storePath = Join-Path $installPath ".store"
    $configFileName = Join-Path $storePath $script:CMap["state"]

    return $configFileName
}

function Remove-TurnKeySdnStore {

    try {
        $depConfig = Get-DeploymentConfig
        $deployFolder = $depConfig.vmLocation

        if (-not [string]::IsNullOrEmpty($deployFolder)) {
            Write-TraceLog "Remove-TurnKeySdnStore removing deployment folder $deployFolder"
            Remove-Item $depConfig.vmLocation -Recurse -Force -ErrorAction SilentlyContinue
        }
    }
    catch {
        Write-TraceLog "Remove-TurnKeySdnStore: Failed to remove vmLocation $deployFolder, error $_"
    }

    try {
        $workloadConfig = Get-TurnKeySdnWorkloadConfig
        $workloadDeployFolder = $workloadConfig.deploymentpath

        if (-not [String]::IsNullOrEmpty($workloadDeployFolder)) {
            Write-TraceLog "Remove-TurnKeySdnStore removing workload folder $workloadDeployFolder"
            Remove-Item $workloadDeployFolder -Recurse -Force -ErrorAction SilentlyContinue
        }
    }
    catch {
        Write-TraceLog "Remove-TurnKeySdnStore: Failed to remove deploymentpath $workloadDeployFolder, error $_"
    }

    try {
        $storePath = Get-TurnKeySdnPath

        if (-not [String]::IsNullOrEmpty($storePath)) {
            Write-TraceLog "Remove-TurnKeySdnStore removing storePath $storePath"
            Remove-Item $storePath -Recurse -Force -ErrorAction SilentlyContinue
        }
    }
    catch {
        Write-TraceLog "Remove-TurnKeySdnStore: Failed to remove storePath $storePath, error $_"
    }
}

function Get-TurnKeySdnRestEndpoint {
    $sdnConfig = Get-SdnConfig

    $endpoint = $sdnConfig.networkController.restName

    if (-not [String]::IsNullOrEmpty($endpoint)) {
        return $endpoint
    }

    if ([String]::IsNullOrEmpty($sdnConfig.networkController.restIpAddress)) {
        throw "Invalid sdn configuration, both restname and IP are empty"
    }

    return $sdnConfig.networkController.restIpAddress
}
function Get-TurnKeySdnNCRuntime() {
    $sdnConfig = Get-SdnConfig
    return $sdnConfig.networkController.runtime
}

function Set-TurnKeySdnConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$sdnConfig
    )

    $configString = $sdnConfig | ConvertTo-Json -Depth 10
    $configFile = Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["sdnConfigFile"]
    Set-Content -Path $configFile -Value $configString -Encoding UTF8
}

function Set-DeploymentConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$deploymentConfig
    )    

    $configString = $deploymentConfig | ConvertTo-Json -Depth 10
    Set-Content -Path $(Get-DeploymentFile) -Value $configString -Encoding UTF8
}

function LoadSdnExpressConfig {
    $sdnexpress = Import-LocalizedData `
        -BaseDirectory $script:CMap["sdnExpressConfigPath"] `
        -FileName $script:CMap["sdnExpressTemplateFile"]

    return $sdnexpress
}

function Get-WorkloadConfigPath {
    param(
        [ValidateSet("LoadBalancer", "VNET", "LogicalNetwork", "Gateway", "None")]
        [Parameter(Mandatory = $false)] $WorkloadType,
        [Parameter(Mandatory = $false)] $WorkloadFolder
    )

    if ($WorkloadType -eq "None" -or [String]::IsNullOrEmpty($WorkloadType)) {
        $WorkloadType = "LoadBalancer"
    }

    if ([String]::IsNullOrEmpty($WorkloadFolder)) {
        $WorkloadFolder = $WorkloadType
    }

    $path = Join-Path(Get-InternalWorkloadConfigPath) $WorkloadFolder

    if (-not (Test-Path $path)) {
        throw "Workload $WorkloadType not found at $path"
    }

    return $path
}
function Get-TurnKeySdnWorkloadConfig {
    $workloadConfigFile = Join-Path (Get-InternalWorkloadConfigPath) $script:CMap["workload"]
    $workloadConfig = Get-Config $workloadConfigFile
    return $workloadConfig
}
function Get-SdnWorkloadDeploymentPath {
    $workloadConfig = Get-TurnKeySdnWorkloadConfig

    if (-not [String]::isnullorempty($workloadConfig.deploymentpath)) {
        return $workloadConfig.deploymentpath
    }
}

function Set-TurnKeySdnWorkloadConfig {
    param(
        [parameter(Mandatory = $true)][PSCustomObject]$config
    )    

    $configString = $config | ConvertTo-Json -Depth 10
    $configFile = Join-Path (Get-InternalWorkloadConfigPath) $script:CMap["workload"]
    Set-Content -Path $configFile -Value $configString -Encoding UTF8
}

function Get-SdnWorkloadVirtualGateways {
    param(
        [Parameter(Mandatory = $true)] $WorkloadConfigPath
    )

    $jsonPath = Join-Path $workloadConfigPath "jsons\virtualgateways"

    if (-not $(Test-Path $jsonPath)) {
        return $null
    }

    Get-ChildItem $jsonPath
}
function Get-SdnWorkloadLogicalNetworks {
    param(
        [Parameter(Mandatory = $true)] $WorkloadConfigPath
    )

    $jsonPath = Join-Path $workloadConfigPath "jsons\logicalnetworks"

    if (-not $(Test-Path $jsonPath)) {
        return $null
    }

    Get-ChildItem $jsonPath
}

function Get-SdnWorkloadNetworkInterfaces {
    param(
        [Parameter(Mandatory = $true)] $WorkloadConfigPath
    )

    $nicJsonPath = Join-Path $workloadConfigPath "jsons\networkinterfaces"

    if (-not $(Test-Path $nicJsonPath)) {
        return $null
    }

    Get-ChildItem $nicJsonPath
}


function Get-SdnWorkloadLoadbalancers {
    param(
        [Parameter(Mandatory=$true)] $WorkloadConfigPath
    )

    $lbJsonPath = Join-Path $workloadConfigPath "jsons\loadbalancers"

    if (-not $(Test-Path $lbJsonPath)) {
        return $null
    }

    Get-ChildItem $lbJsonPath
}

function Get-TurnKeySdnHyperVHosts {
    $depConfig = Get-DeploymentConfig
    return $depConfig.hyperVHosts
}

function Get-SdnSwitchName {
    $depConfig = Get-DeploymentConfig
    return $depConfig.sdnSwitchName
}

function Initialize-TurnKeySdnDeploymentId {

    $config = Get-TurnKeySdnInternalConfig -configType state
    $resetDepId = $false
    if ([String]::IsNullOrEmpty($config.depIdEncoded)) {
        $resetDepId = $true        
    }
    else {
        $resetDepId = ($config.depIdEncoded  | ConvertTo-SecureString -ErrorAction SilentlyContinue) -eq $null        
    }

    if ($resetDepId) {
        $id = ((New-Guid).guid).split("-")[1]
        $idEncoded = $id | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
        $config.depId = $id
        $config.depIdEncoded = $idEncoded
        $config.randomizerSeed = Get-AddressRandmoizerSeed
        Set-TurnKeySdnInternalConfig -configType state -config $config
    }
}

function Initialize-Store {

    Initialize-StorePath

    if (-not $(Test-Path (Get-TurnKeySdnPath))) {
        New-Item (Get-TurnKeySdnPath) -ItemType Directory -ErrorAction Stop | Out-Null
    }

    if (-not $(Test-Path (Get-StorePath))) {
        New-Item (Get-StorePath) -ItemType Directory -ErrorAction Stop | Out-Null
    }

    $script:CMap["deploymentConfigPath"] = Join-Path $script:CMap["turnKeySdnPath"] "DeploymentConfig"
    $script:CMap["workloadConfigPath"] = Join-Path $script:CMap["turnKeySdnPath"] "WorkloadConfig"

    $internalConfigPath = $script:CMap["deploymentConfigPath"]
    $internalWorkloadConfigPath = $script:CMap["workloadConfigPath"]

    if (-not (Test-Path $internalConfigPath)) {
        New-Item $internalConfigPath -ItemType Directory -ErrorAction Stop | Out-Null
    }

    if (-not (Test-Path $internalWorkloadConfigPath)) {
        New-Item $internalWorkloadConfigPath -ItemType Directory -ErrorAction Stop | Out-Null
    }

    $userConfigPath = $script:CMap["userConfigPath"]
    Write-TraceLog "Copying user config from $userConfigPath to $internalConfigPath"

    robocopy $userConfigPath $script:CMap["deploymentConfigPath"] /E /XO /NC /NS /NP /NFL /NDL /NJH /NJS

    $userWorkloadConfigPath = $script:CMap["userWorkloadConfigPath"]
    Write-TraceLog "Copying user workload config from $userWorkloadConfigPath to $internalWorkloadConfigPath"

    robocopy $userWorkloadConfigPath $internalWorkloadConfigPath /E /XO /NC /NS /NP /NFL /NDL /NJH /NJS

    $vhdStore = Get-VhdStore

    if (-not (Test-Path $vhdStore)) {
        New-Item $vhdStore -ItemType Directory -ErrorAction Stop | Out-Null
    }

    $credFile = Join-Path (Get-StorePath) $script:CMap["credential"]
    if (-not $(Test-Path $credFile)) {
        $cred = @{}
        $cred["username"] = ""
        $cred["password"] = ""
        Set-TurnKeySdnInternalConfig -configType credential -config $cred
    }

    $stateFile = Join-Path (Get-StorePath) $script:CMap["state"]
    if (-not $(Test-Path $stateFile)) {
        $state = @{}
        $state["depId"] = ""
        $state["depIdEncoded"] = ""
        $state["randomizerSeed"] = ""
        $state["initialized"] = $false
        $state["installState"] = $script:InstallStateNone
        Set-TurnKeySdnInternalConfig -configType state -config $state
    }

    Initialize-TurnKeySdnDeploymentId
}

function Get-DeploymentFile {
    return Join-Path (Get-InternalDeploymentConfigPath) $script:CMap["deploymentFile"]
}

function Get-MgmtNicName {
    return $script:MgmtHostNicName
}

function Get-DefaultWorkloadVhdShare {
    return $script:DefaultTestVhdShare
}

function Get-DefaultWorkloadVhdFile {
    return $script:DefaultTestVhdFile
}

function Get-StateInternal {
    $stateFile = Get-StateFileWithoutInit
    $config = Get-Config $stateFile
    return $config
}

function Set-InstallState {
    param(
        [ValidateSet("installed", "installFailed", "installing", "none")]
        [parameter(Mandatory = $true)][string] $state
    )

    $config = Get-StateInternal
    $config.installState = $state
    Set-TurnKeySdnInternalConfig -configType state -config $config
}

function Test-IsInstalled {
    try {
        $config = Get-StateInternal
    }
    catch {
        return $false
    }
    return $config.installState -ieq "installed"
}

function Test-IsInitialized {

    try {
        $config = Get-StateInternal
    }
    catch {
        return $false
    }

    return $config.initialized
}

function Set-Initialized {
    $config = Get-TurnKeySdnInternalConfig -configType state
    $config.initialized = $true
    Set-TurnKeySdnInternalConfig -configType state -config $config
}

function Get-MsftCorpRoutes {
    return $script:MSFTCorpRoutes
}