Cloud/GoDaddy.ps1

function Get-GodaddyCreds {
    $apiKey = $env:GS_GODADDY_API_KEY
    $apiSecret = $env:GS_GODADDY_API_SECRET

    if (-not $apiKey) {
        $apiKey = Read-Host "[Not found environment variable GS_GODADDY_API_KEY] Enter your GoDaddy API Key"
    }
    if (-not $apiSecret) {
        $apiSecret = Read-Host "[Not found environment variable GS_GODADDY_API_SECRET] Enter your GoDaddy API Secret"
    }

    return @{ Key = $apiKey; Secret = $apiSecret }
}



function Update-GoDaddyPointer {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$FullDomainName,

        [Parameter(Mandatory = $false)]
        [ValidateSet("A", "CNAME")]
        [string]$Type = 'CNAME',

        [Parameter(Mandatory)]
        [string]$Target
    )

    $dns = $FullDomainName.Split('.', 2)
    $name = $dns[0]
    $domain = $dns[1]

    $creds = Get-GodaddyCreds
    $headers = @{ Authorization = "sso-key $($creds.Key):$($creds.Secret)" }

    $uri = "https://api.godaddy.com/v1/domains/$domain/records/$Type/$name"
    $existing = $null
    try {
        $existing = Invoke-RestMethod -Uri $uri -Headers $headers -Method GET -ErrorAction Stop
    }
    catch {}

    if (-not $existing) {
        Write-Log "Record does not exist. Cannot update." Error
        return
    }

    $record = [array]@{
        data = $Target
        name = $name
        ttl  = 3600
        type = $Type
    }
    


    $body = $record | ConvertTo-Json -Depth 2


    $patchUri = "https://api.godaddy.com/v1/domains/$domain/records/$Type/$name"
    try {
        Invoke-RestMethod -Uri $patchUri -Headers $headers -Method PUT -Body [$body] -ContentType 'application/json' 
        Write-Log "Updated $name.$domain -> $Target" Success
    }
    catch {
        Write-Log "Failed to update record: $_" Error
    }
}


function Remove-GoDaddyPointer {
    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter(Mandatory)]
        [string]$FullDomainName,

        [Parameter(Mandatory = $false)]
        [ValidateSet("A", "CNAME")]
        [string]$Type = 'CNAME'
    )

    $dns = $FullDomainName.Split('.', 2)
    $name = $dns[0]
    $domain = $dns[1]

    $creds = Get-GodaddyCreds
    $headers = @{ Authorization = "sso-key $($creds.Key):$($creds.Secret)" }

    $uri = "https://api.godaddy.com/v1/domains/$domain/records/$Type/$name"

    if ($PSCmdlet.ShouldProcess("$name.$domain", "Delete $Type record")) {
        try {
            Invoke-RestMethod -Uri $uri -Headers $headers -Method DELETE -ErrorAction Stop
            Write-Log "Deleted $name.$domain ($Type)" Success
        }
        catch {
            Write-Log "❌ Failed to delete record: $_" Error
        }
    }
}


function Add-GoDaddyPointer {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$FullDomainName,

        [Parameter(Mandatory = $false)]
        [ValidateSet("A", "CNAME")]
        [string]$Type = 'CNAME',

        [Parameter(Mandatory)]
        [string]$Target,
        [switch]$Force
    )

    $dns = $FullDomainName.Split('.', 2)
    $name = $dns[0]
    $domain = $dns[1]

    $creds = Get-GodaddyCreds
    $headers = @{ Authorization = "sso-key $($creds.Key):$($creds.Secret)" }

    $uri = "https://api.godaddy.com/v1/domains/$domain/records/$Type/$name"
    $existing = $null
    try { $existing = Invoke-RestMethod -Uri $uri -Headers $headers -Method GET -ErrorAction Stop } catch {}

    if ($existing -and -not $Force) {
        Write-Log "Record already exists: $($existing.data)" Error
        return
    }
   

    $body = @(
        @{ data = $Target; name = $name; ttl = 3600; type = $Type }
    ) | ConvertTo-Json -Depth 3


    if ($existing -and $Force) {
        Write-Log "An existing DNS record for '$FullDomainName' was found pointing to '$($existing.data)'. Proceeding to update it as the -Force flag is specified."  Warning
        
        $patchUri = "https://api.godaddy.com/v1/domains/$domain/records/$Type/$name"
        try {
            Invoke-RestMethod -Uri $patchUri -Headers $headers -Method PUT -Body [$body] -ContentType 'application/json' 
            Write-Log "Updated $name.$domain -> $Target" Success
        }
        catch {
            Write-Log "Failed to update record: $_" Error
        }
        return
    
    }

    $createUri = "https://api.godaddy.com/v1/domains/$domain/records"
    try {
        Invoke-RestMethod -Uri $createUri -Headers $headers -Method PATCH -Body [$body] -ContentType 'application/json'
        Write-Log "Created $name.$domain -> $Target" Success
    }
    catch {
        Write-Log "Failed to create record: $_" Error
    }
}



Export-ModuleMember -Function '*'