Plugins/Aurora.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
function Get-CurrentPluginType { 'dns-01' }

function Add-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, Position = 0)]
        [String]$RecordName,
        
        [Parameter(Mandatory, Position = 1)]
        [String]$TxtValue,
        
        [Parameter(Mandatory, Position = 2)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$AuroraCredential = [System.Management.Automation.PSCredential]::Empty,
        
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [String]$AuroraApi = 'api.auroradns.eu',
        
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    Write-Debug "convert the Credential to normal String values"
    $auroraAuthorization = @{ Api = $AuroraApi; Key = $AuroraCredential.UserName; Secret = $AuroraCredential.GetNetworkCredential().Password }
 
    Write-Debug "Attempting to find hosted zone for $RecordName"
    try {
        $zone = Invoke-AuroraFindZone -RecordName $RecordName @auroraAuthorization
        $ZoneID = $zone.id
        $zoneName = $zone.name
    } catch { throw }
    
    if ((-not $ZoneID) -or (-not $zoneName)) {
        throw "Unable to find Aurora hosted zone for $RecordName"
    }
    
    Write-Debug "Separate the portion of the name that doesn't contain the zone name"
    $recordPath = ($RecordName -ireplace [regex]::Escape($zoneName), [String]::Empty).TrimEnd('.')
    Write-Debug "[recordPath:$recordPath][RecordName:$RecordName]"

    Write-Debug "Query the existing record(s)"
    try {
        $records = @()
        $records += Invoke-AuroraGetRecord -RecordName $recordPath -ZoneID $ZoneID @auroraAuthorization
    } catch { throw }

    Write-Debug "Check if our value is already in there"
    if ($TxtValue -in $records.content) {
        Write-Debug "Record $RecordName already contains $TxtValue. Nothing to do."
        return
    } else {
        Write-Debug "Record $RecordName does not contain $TxtValue. Adding it."
        try {
            $recAdded = Invoke-AuroraAddRecord -ZoneID $ZoneID -Name $recordPath -Content $TxtValue -Type 'TXT' -TTL 300 @auroraAuthorization
        } catch { throw }
    }
    <#
    .SYNOPSIS
        Add a DNS TXT record to Aurora DNS.
    .DESCRIPTION
        Add a DNS TXT record to Aurora DNS.
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
    .PARAMETER TxtValue
        The value of the TXT record.
    .PARAMETER Credential
        The Key and Secret in a PSCredential (secure) parameter
    .PARAMETER Key
        The Aurora DNS API key for your account.
    .PARAMETER Secret
        The Aurora DNS API secret key for your account.
    .PARAMETER Api
        The Aurora DNS API hostname.
        Default (if not specified): api.auroradns.eu
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
    .EXAMPLE
        $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
        PS C:\>Add-DnsTxt '_acme-challenge.example.com' 'txt-value' @auroraAuthorization
        Adds a TXT record for the specified site with the specified value.
    #>

}

function Remove-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, Position = 0)]
        [String]$RecordName,
        
        [Parameter(Mandatory, Position = 1)]
        [String]$TxtValue,
        
        [Parameter(Mandatory, Position = 2)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$AuroraCredential = [System.Management.Automation.PSCredential]::Empty,
        
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [String]$AuroraApi = 'api.auroradns.eu',
        
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    Write-Debug "convert the Credential to normal String values"
    $auroraAuthorization = @{ Api = $AuroraApi; Key = $AuroraCredential.UserName; Secret = $AuroraCredential.GetNetworkCredential().Password }

    Write-Debug "Attempting to find hosted zone for $RecordName"
    try {
        $zone = Invoke-AuroraFindZone -RecordName $RecordName @auroraAuthorization
        $ZoneID = $zone.id
        $zoneName = $zone.name
    } catch { Write-Debug "Caught an error, $($_.Exception.Message)"; throw }
    
    if ((-not $ZoneID) -or (-not $zoneName)) {
        throw "Unable to find Aurora hosted zone for $RecordName"
    }
    
    Write-Debug "Separate the portion of the name that doesn't contain the zone name"
    $recordPath = ($RecordName -ireplace [regex]::Escape($zoneName), [String]::Empty).TrimEnd('.')
    Write-Debug "[recordPath:$recordPath][RecordName:$RecordName]"

    Write-Debug "Query the existing record(s)"
    try {
        $records = @()
        $records += Invoke-AuroraGetRecord -RecordName $recordPath -ZoneID $ZoneID @auroraAuthorization
    } catch { Write-Debug "Caught an error, $($_.Exception.Message)"; throw }

    Write-Debug "Check for the value to delete"
    if ($records.Count -eq 0) {
        Write-Debug "Record $RecordName doesn't exist. Nothing to do."
        return
    } else {
        if ($TxtValue -notin $records.content) {
            Write-Debug "Records with the name $RecordName do not contain $TxtValue. Nothing to do."
        } else {
            Write-Debug "Record $RecordName with value $TxtValue must be deleted."
            $recordIDs = @(($records | Where-Object { $_.content -eq $TxtValue }).id)
            try {
                ForEach ($id in $recordIDs) {
                    $deletedRecords = Invoke-AuroraDeleteRecord -ZoneID $ZoneID -RecordId $id @auroraAuthorization
                }
            } catch { Write-Debug "Caught an error, $($_.Exception.Message)"; throw }
        }
    }
    <#
    .SYNOPSIS
        Remove a DNS TXT record from Aurora DNS.
    .DESCRIPTION
        Remove a DNS TXT record from Aurora DNS.
    .PARAMETER RecordName
        The fully qualified name of the TXT record.
    .PARAMETER TxtValue
        The value of the TXT record.
    .PARAMETER Credential
        The Key and Secret in a PSCredential (secure) parameter
    .PARAMETER Key
        The Aurora DNS API key for your account.
    .PARAMETER Secret
        The Aurora DNS API secret key for your account.
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
    .EXAMPLE
        $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
        PS C:\>Remove-DnsTxt '_acme-challenge.example.com' 'txt-value' @auroraAuthorization
        Removes a TXT record for the specified site with the specified value.
    #>

}

function Save-DnsTxt {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    <#
    .SYNOPSIS
        Not required.
    .DESCRIPTION
        This provider does not require calling this function to commit changes to DNS records.
    .PARAMETER ExtraParams
        This parameter can be ignored and is only used to prevent errors when splatting with more parameters than this function supports.
    #>

}

############################
# Helper Functions
############################

function Get-AuroraDNSAuthorizationHeader {
    <#
.SYNOPSIS
    Create headers required for Aurora DNS authorization
.DESCRIPTION
    Create headers required for Aurora DNS authorization
.PARAMETER Key
    The Aurora DNS API key for your account.
.PARAMETER Secret
    The Aurora DNS API secret key for your account.
.PARAMETER Method
    The method used for this action.
    Some of the most used: 'POST', 'GET' or 'DELETE'
.PARAMETER Uri
    The Uri used for this action.
    Example: '/zones'
.PARAMETER ContentType
    The content type.
    Default value (if not specified): 'application/json; charset=UTF-8'
.EXAMPLE
    $authorizationHeader = Get-AuroraDNSAuthorizationHeader -Key XXXXXXXXXX -Secret YYYYYYYYYYYYYYYY -Method GET -Uri /zones
.NOTES
    Function Name : Invoke-AuroraFindZone
    Version : v2021.0530.1330
    Author : John Billekens
    Requires : API Account => https://cp.pcextreme.nl/auroradns/users
.LINK
    https://github.com/j81blog/Posh-AuroraDNS
#>
  
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [String]$Key,
        
        [Parameter(Mandatory)]
        [String]$Secret,
        
        [Parameter(Mandatory)]
        [String]$Method,
        
        [Parameter(Mandatory)]
        [String]$Uri,
        
        [Parameter()]
        [String]$ContentType = "application/json; charset=UTF-8",
        
        [Parameter(DontShow)]
        [String]$TimeStamp = $((get-date).ToUniversalTime().ToString("yyyyMMddTHHmmssZ")),
        
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    $Message = '{0}{1}{2}' -f $Method, $Uri, $TimeStamp
    $hmac = New-Object System.Security.Cryptography.HMACSHA256
    $hmac.key = [Text.Encoding]::UTF8.GetBytes($Secret)
    $Signature = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($Message))
    $SignatureB64 = [Convert]::ToBase64String($signature)
    $AuthorizationString = '{0}:{1}' -f $Key, $SignatureB64
    $Authorization = [Text.Encoding]::UTF8.GetBytes($AuthorizationString)
    $AuthorizationB64 = [Convert]::ToBase64String($Authorization)
    
    $headers = @{
        'X-AuroraDNS-Date' = $TimeStamp
        'Authorization'    = $('AuroraDNSv1 {0}' -f $AuthorizationB64)
        'Content-Type'     = $ContentType
    }
    Write-Output $headers
}

function Invoke-AuroraAddRecord {
    <#
.SYNOPSIS
    Get Aurora DNS Record
.DESCRIPTION
    Get Aurora DNS Record
.PARAMETER Key
    The Aurora DNS API key for your account.
.PARAMETER Secret
    The Aurora DNS API secret key for your account.
.PARAMETER ZoneID
    Specify a specific Aurora DNS Zone ID (GUID).
.PARAMETER Name
    Specify a name fo the new record.
.PARAMETER Content
    Specify the content for the nwe record.
.PARAMETER TTL
    Specify a Time To Live value in seconds.
    Default (if not specified): 3600
.PARAMETER Type
    Specify the record type.
    Can contain one of the following values: "A", "AAAA", "CNAME", "MX", "NS", "SOA", "SRV", "TXT", "DS", "PTR", "SSHFP", "TLSA"
    Default (if not specified): "A"
.PARAMETER Api
    The Aurora DNS API hostname.
    Default (if not specified): api.auroradns.eu
.EXAMPLE
    $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
    PS C:\>$record = Invoke-AuroraAddRecord -ZoneID 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' -Name www -Content 198.51.100.85 @auroraAuthorization
    Create an 'A' record with the name 'www' and content '198.51.100.85'
.EXAMPLE
    $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
    PS C:\>$record = Invoke-AuroraAddRecord -ZoneID 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' -Content 'v=spf1 include:_spf.google.com' -Type TXT @auroraAuthorization
    Create an 'TXT' for the domain (no record name) and content 'v=spf1 include:_spf.google.com'
.NOTES
    Function Name : Invoke-AuroraAddRecord
    Version : v2021.0530.1330
    Author : John Billekens
    Requires : API Account => https://cp.pcextreme.nl/auroradns/users
.LINK
    https://github.com/j81blog/Posh-AuroraDNS
#>
 
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String]$Key,
        
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String]$Secret,
        
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [GUID[]]$ZoneID,
        
        [String]$Name = '',
        
        [String]$Content = '',
        
        [int]$TTL = 3600,

        [ValidateSet('A', 'AAAA', 'CNAME', 'MX', 'NS', 'SOA', 'SRV', 'TXT', 'DS', 'PTR', 'SSHFP', 'TLSA')]
        [String]$Type = 'A',

        [Parameter()]
        [String]$Api = 'api.auroradns.eu',
        
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    $UseBasic = @{ }
    if ('UseBasicParsing' -in (Get-Command Invoke-RestMethod).Parameters.Keys) {
        $UseBasic.UseBasicParsing = $true
    }
    $Method = 'POST'
    $Uri = '/zones/{0}/records' -f $ZoneID.Guid
    $ApiUrl = 'https://{0}{1}' -f $Api, $Uri
    $AuthorizationHeader = Get-AuroraDNSAuthorizationHeader -Key $Key -Secret $Secret -Method $Method -Uri $Uri
    $restError = ''

    $Payload = @{
        name    = $Name
        ttl     = $TTL
        type    = $Type
        content = $Content
    }
    $Body = $Payload | ConvertTo-Json
    Write-Debug "$Method URI: `"$ApiUrl`""
    try {
        [Object[]]$result = Invoke-RestMethod -Uri $ApiUrl -Headers $AuthorizationHeader -Method $Method -Body $Body -ErrorVariable restError @UseBasic
    } catch {
        $result = $null
        $OutError = $restError[0].Message | ConvertFrom-Json -ErrorAction SilentlyContinue
        Write-Debug $($OutError | Out-String)
        Throw ($OutError.errormsg)
    }
    if ( ($result.Count -gt 0) -and ($null -ne $result[0].id) -and (-not [String]::IsNullOrEmpty($($result[0].id))) ) {
        Write-Output $result
    } else {
        Write-Debug "The function generated no data"
        Write-Output $null
    }
}

function Invoke-AuroraDeleteRecord {
    <#
.SYNOPSIS
    Delete an Aurora DNS Record
.DESCRIPTION
    Delete an Aurora DNS Record
.PARAMETER Key
    The Aurora DNS API key for your account.
.PARAMETER Secret
    The Aurora DNS API secret key for your account.
.PARAMETER ZoneID
    Specify a specific Aurora DNS Zone ID (GUID).
.PARAMETER RecordID
    Specify a specific Aurora DNS Record ID (GUID).
.PARAMETER Api
    The Aurora DNS API hostname.
    Default (if not specified): api.auroradns.eu
.EXAMPLE
    $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
    PS C:\>Invoke-AuroraDeleteRecord -ZoneID aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee -RecordID vvvvvvvv-wwww-xxxx-yyyy-zzzzzzzzzzzz @auroraAuthorization
    Delete a record with the ID 'vvvvvvvv-wwww-xxxx-yyyy-zzzzzzzzzzzz' in zone 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
.NOTES
    Function Name : Invoke-AuroraDeleteRecord
    Version : v2021.0530.1330
    Author : John Billekens
    Requires : API Account => https://cp.pcextreme.nl/auroradns/users
.LINK
    https://github.com/j81blog/Posh-AuroraDNS
#>
 
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Key,
        
        [Parameter(Mandatory)]
        [string]$Secret,
        
        [Parameter(Mandatory)]
        [GUID[]]$ZoneID,
        
        [Parameter(Mandatory)]
        [GUID[]]$RecordId,
        
        [Parameter()]
        [String]$Api = 'api.auroradns.eu',
        
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    $UseBasic = @{ }
    if ('UseBasicParsing' -in (Get-Command Invoke-RestMethod).Parameters.Keys) {
        $UseBasic.UseBasicParsing = $true
    }
    $Method = 'DELETE'
    $Uri = '/zones/{0}/records/{1}' -f $ZoneID.Guid, $RecordId.Guid
    $ApiUrl = 'https://{0}{1}' -f $Api, $Uri
    $AuthorizationHeader = Get-AuroraDNSAuthorizationHeader -Key $Key -Secret $Secret -Method $Method -Uri $Uri
    $restError = ''
    
    Write-Debug "$Method URI: `"$ApiUrl`""
    try {
        $result = Invoke-RestMethod -Uri $ApiUrl -Headers $AuthorizationHeader -Method $Method -ErrorVariable restError @UseBasic
    } catch {
        $result = $null
        $OutError = $restError[0].Message | ConvertFrom-Json -ErrorAction SilentlyContinue
        Write-Debug $($OutError | Out-String)
        Throw ($OutError.errormsg)
    }
    if ([String]::IsNullOrWhiteSpace($($result.id))) {
        Write-Debug "The function generated no data"
        Write-Output $null
    } else {
        Write-Output $result
    }
}

function Invoke-AuroraFindZone {
    <#
.SYNOPSIS
    Get Aurora DNS Zone based on full record/host name
.DESCRIPTION
    Get Aurora DNS Zone based on full record/host name
.PARAMETER Key
    The Aurora DNS API key for your account.
.PARAMETER Secret
    The Aurora DNS API secret key for your account.
.PARAMETER Api
    The Aurora DNS API hostname.
    Default (if not specified): api.auroradns.eu
.EXAMPLE
    $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
    PS C:\>$zone = Invoke-AuroraFindZone -RecordName www.domain.com @auroraAuthorization
.NOTES
    Function Name : Invoke-AuroraFindZone
    Version : v2021.0530.1330
    Author : John Billekens
    Requires : API Account => https://cp.pcextreme.nl/auroradns/users
.LINK
    https://github.com/j81blog/Posh-AuroraDNS
#>
  
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, Position = 0)]
        [String]$RecordName,
        
        [Parameter(Mandatory, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [String]$Key,
        
        [Parameter(Mandatory, Position = 2)]
        [ValidateNotNullOrEmpty()]
        [String]$Secret,
        
        [Parameter(Position = 3)]
        [ValidateNotNullOrEmpty()]
        [String]$Api = 'api.auroradns.eu',
        
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    $auroraAuthorization = @{ Api = $Api; Key = $Key; Secret = $Secret }
    try {
        [Object[]]$zones = Invoke-AuroraGetZones @auroraAuthorization
    } catch { Write-Debug "Caught an error, $($_.Exception.Message)"; throw }
         
    Write-Debug "Search for the zone from longest to shortest set of FQDN pieces"
    $pieces = $RecordName.Split('.')
    for ($i = 0; $i -lt ($pieces.Count - 1); $i++) {
        $zoneTest = $pieces[$i..($pieces.Count - 1)] -join '.'
        Write-Debug "Checking $zoneTest"
        try {
            Write-Debug "Check for results"
            [Object[]]$result = @($Zones | Where-Object { $_.name -eq $zoneTest })
            if ($result.Count -gt 0) {
                Write-Output $result
            }
        } catch {
            Write-Debug "Caught an error, $($_.Exception.Message)"
            throw
        }
    }
}

function Invoke-AuroraGetRecord {
    <#
.SYNOPSIS
    Get Aurora DNS Record
.DESCRIPTION
    Get Aurora DNS Record
.PARAMETER Key
    The Aurora DNS API key for your account.
.PARAMETER Secret
    The Aurora DNS API secret key for your account.
.PARAMETER RecordID
    Specify a specific Aurora DNS Record ID (GUID).
.PARAMETER RecordName
    Specify a specific Aurora DNS Record Name (String).
.PARAMETER ZoneID
    Specify a specific Aurora DNS Zone ID (GUID).
.PARAMETER Api
    The Aurora DNS API hostname.
    Default (if not specified): api.auroradns.eu
.EXAMPLE
    $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
    PS C:\>$record = Invoke-AuroraGetRecord -ZoneID 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' @auroraAuthorization
    List all records by not specifying 'RecordID' or 'RecordName' with a value
.EXAMPLE
    $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
    PS C:\>$record = Invoke-AuroraGetRecord -ZoneID 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' -RecordName 'www' @auroraAuthorization
    Get record with name 'www' in zone 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
.EXAMPLE
    $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
    PS C:\>$record = Invoke-AuroraGetRecord -ZoneID 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' -RecordID 'vvvvvvvv-wwww-xxxx-yyyy-zzzzzzzzzzzz' @auroraAuthorization
    Get record with ID 'vvvvvvvv-wwww-xxxx-yyyy-zzzzzzzzzzzz' in zone 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
.NOTES
    Function Name : Invoke-AuroraGetRecord
    Version : v2021.0530.1330
    Author : John Billekens
    Requires : API Account => https://cp.pcextreme.nl/auroradns/users
.LINK
    https://github.com/j81blog/Posh-AuroraDNS
#>
  
    [CmdletBinding(DefaultParameterSetName = 'All')]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String]$Key,
        
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String]$Secret,
        
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [GUID[]]$ZoneID,

        [Parameter(ParameterSetName = 'GUID', Mandatory)]
        [GUID[]]$RecordID,
        
        [Parameter(ParameterSetName = 'Named')]
        [String]$RecordName,
        
        [Parameter(ParameterSetName = 'Named')]
        [String]$Co,
        
        [Parameter()]
        [String]$Api = 'api.auroradns.eu',
        
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    $UseBasic = @{ }
    if ('UseBasicParsing' -in (Get-Command Invoke-RestMethod).Parameters.Keys) {
        $UseBasic.UseBasicParsing = $true
    }
    $Method = 'GET'
    if ($PSCmdlet.ParameterSetName -like "GUID") {
        $Uri = '/zones/{0}/records/{1}' -f $ZoneID.Guid, $RecordID.Guid
    } else {
        $Uri = '/zones/{0}/records' -f $ZoneID.Guid
    }
    Write-Verbose "$Uri"
    $ApiUrl = 'https://{0}{1}' -f $Api, $Uri
    $AuthorizationHeader = Get-AuroraDNSAuthorizationHeader -Key $Key -Secret $Secret -Method $Method -Uri $Uri
    $restError = ''
    try {
        Write-Debug "$Method URI: `"$ApiUrl`""
        [Object[]]$result = Invoke-RestMethod -Uri $ApiUrl -Headers $AuthorizationHeader -Method $Method -ErrorVariable restError @UseBasic
        if ($PSBoundParameters.ContainsKey('distributionalgorithm')) { $Payload.Add('distributionalgorithm', $distributionalgorithm) }

        if ($PSCmdlet.ParameterSetName -like "Named") {
            [Object[]]$result = $result | Where-Object { $_.name -eq $RecordName }
        }
    } catch {
        $result = $null
        $OutError = $restError[0].Message | ConvertFrom-Json -ErrorAction SilentlyContinue
        Write-Debug $($OutError | Out-String)
        if ($OutError.error -eq 'NoSuchRecordError') {
            $result = $null
        } else {
            Throw ($OutError.errormsg)
        }
    } 
    if ( ($result.Count -gt 0) -and ($null -ne $result[0].id) -and (-not [String]::IsNullOrEmpty($($result[0].id))) ) {
        Write-Output $result
    } else {
        Write-Debug "The function generated no data"
        Write-Output $null
    }
}

function Invoke-AuroraGetZones {
    <#
.SYNOPSIS
    Get Aurora DNS Zones
.DESCRIPTION
    Get Aurora DNS Zones
.PARAMETER Key
    The Aurora DNS API key for your account.
.PARAMETER Secret
    The Aurora DNS API secret key for your account.
.PARAMETER Api
    The Aurora DNS API hostname.
    Default (if not specified): api.auroradns.eu
.EXAMPLE
    $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
    PS C:\>$zones = Invoke-AuroraGetZones @auroraAuthorization
.NOTES
    Function Name : Invoke-AuroraGetZones
    Version : v2021.0530.1330
    Author : John Billekens
    Requires : API Account => https://cp.pcextreme.nl/auroradns/users
.LINK
    https://github.com/j81blog/Posh-AuroraDNS
#>
  
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String]$Key,
        
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String]$Secret,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [String]$Api = 'api.auroradns.eu',
        
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    $UseBasic = @{ }
    if ('UseBasicParsing' -in (Get-Command Invoke-RestMethod).Parameters.Keys) {
        $UseBasic.UseBasicParsing = $true
    }
    $Method = 'GET'
    $Uri = '/zones'
    $ApiUrl = 'https://{0}{1}' -f $Api, $Uri
    $AuthorizationHeader = Get-AuroraDNSAuthorizationHeader -Key $Key -Secret $Secret -Method $Method -Uri $Uri
    $restError = ''
    Write-Debug "$Method URI: `"$ApiUrl`""
    try {
        [Object[]]$result = Invoke-RestMethod -Uri $ApiUrl -Headers $AuthorizationHeader -Method $Method -ErrorVariable restError @UseBasic
    } catch {
        $result = $null
        $OutError = $restError[0].Message | ConvertFrom-Json -ErrorAction SilentlyContinue
        Write-Debug $($OutError | Out-String)
        Throw ($OutError.errormsg)
    }
    if ( ($result.Count -gt 0) -and ($null -ne $result[0].id) -and (-not [String]::IsNullOrEmpty($($result[0].id))) ) {
        Write-Output $result
    } else {
        Write-Debug "The function generated no data"
        Write-Output $null
    }
}

function Invoke-AuroraSetRecord {
    <#
.SYNOPSIS
    Set Aurora DNS Record with new values
.DESCRIPTION
    Get Aurora DNS Record with new values
.PARAMETER Key
    The Aurora DNS API key for your account.
.PARAMETER Secret
    The Aurora DNS API secret key for your account.
.PARAMETER ZoneID
    Specify a specific Aurora DNS Zone ID (GUID).
.PARAMETER RecordID
    Specify a specific Aurora DNS Record ID (GUID).
.PARAMETER Name
    Specify a name fo the new record.
.PARAMETER Content
    Specify the content for the nwe record.
.PARAMETER TTL
    Specify a Time To Live value in seconds.
    Default (if not specified): 3600
.PARAMETER Type
    Specify the record type.
    Can contain one of the following values: "A", "AAAA", "CNAME", "MX", "NS", "SOA", "SRV", "TXT", "DS", "PTR", "SSHFP", "TLSA"
    Default (if not specified): "A"
.PARAMETER Api
    The Aurora DNS API hostname.
    Default (if not specified): api.auroradns.eu
.EXAMPLE
    $auroraAuthorization = @{ Api='api.auroradns.eu'; Key='XXXXXXXXXX'; Secret='YYYYYYYYYYYYYYYY' }
    PS C:\>$record = Invoke-AuroraSetRecord -ZoneID 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' -RecordID 'vvvvvvvv-wwww-xxxx-yyyy-zzzzzzzzzzzz' -Content 198.51.100.85 @auroraAuthorization
    Set an existing record with new content '198.51.100.85'
.NOTES
    Function Name : Invoke-AuroraAddRecord
    Version : v2021.0530.1330
    Author : John Billekens
    Requires : API Account => https://cp.pcextreme.nl/auroradns/users
.LINK
    https://github.com/j81blog/Posh-AuroraDNS
#>
 
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Key,
        
        [Parameter(Mandatory)]
        [string]$Secret,
        
        [Parameter(Mandatory)]
        [GUID[]]$ZoneID,
        
        [Parameter(Mandatory)]
        [GUID[]]$RecordId,
        
        [String]$Name,
        
        [String]$Content,
        
        [int]$TTL = 3600,

        [ValidateSet('A', 'AAAA', 'CNAME', 'MX', 'NS', 'SOA', 'SRV', 'TXT', 'DS', 'PTR', 'SSHFP', 'TLSA')]
        [String]$Type = 'A',

        [Parameter()]
        [String]$Api = 'api.auroradns.eu',

        [Switch]$PassThru,
        
        [Parameter(ValueFromRemainingArguments, DontShow)]
        $ExtraParams
    )
    $UseBasic = @{ }
    if ('UseBasicParsing' -in (Get-Command Invoke-RestMethod).Parameters.Keys) {
        $UseBasic.UseBasicParsing = $true
    }
    $Method = 'PUT'
    $Uri = '/zones/{0}/records/{1}' -f $ZoneId.Guid, $RecordId.Guid
    $ApiUrl = 'https://{0}{1}' -f $Api, $Uri
    $AuthorizationHeader = Get-AuroraDNSAuthorizationHeader -Key $Key -Secret $Secret -Method $Method -Uri $Uri
    $restError = ''

    $Payload = @{ }
    if ($PSBoundParameters.ContainsKey('Name')) { $Payload.Add('name', $Name) } 
    if ($PSBoundParameters.ContainsKey('TTL')) { $Payload.Add('ttl', $TTL) } 
    if ($PSBoundParameters.ContainsKey('Type')) { $Payload.Add('type', $Type) } 
    if ($PSBoundParameters.ContainsKey('Content')) { $Payload.Add('content', $Content) } 

    $Body = $Payload | ConvertTo-Json

    Write-Debug "$Method URI: `"$ApiUrl`""
    try {
        [Object[]]$result = Invoke-RestMethod -Uri $ApiUrl -Headers $AuthorizationHeader -Method $Method -Body $Body -ErrorVariable restError @UseBasic
        if ($PassThru -and (($result.Count -eq 0) -or ([string]::IsNullOrWhiteSpace($result)))) {
            [Object[]]$result = Invoke-AuroraGetRecord -ZoneID $ZoneID -RecordID $RecordID -Key $Key -Secret $Secret -Api $Api
        }
    } catch {
        $result = $null
        $OutError = $restError[0].Message | ConvertFrom-Json -ErrorAction SilentlyContinue
        Write-Debug $($OutError | Out-String)
        Throw ($OutError.errormsg)
    }
    if ( ($result.Count -gt 0) -and ($null -ne $result[0].id) -and (-not [String]::IsNullOrEmpty($($result[0].id))) ) {
        Write-Output $result
    } else {
        Write-Debug "The function generated no data"
        Write-Output $null
    }
}