PoShDynDnsApi.psm1

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
#Requires -Version 5.1

#region info
<#
The following members are exported via the module's data file (.psd1)
    Functions
    TypeData
    FormatData
#>

#endregion info

#region discover module name
$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
$ModuleName = $ExecutionContext.SessionState.Module
Write-Verbose -Message "Loading module $ModuleName"
#endregion discover module name

#Set-StrictMode -Version Latest
try {
    Add-Type -AssemblyName System.Net.Http -ErrorAction Stop
}
catch {
    $PSCmdlet.ThrowTerminatingError($_)
}

#region load module variables
Write-Verbose -Message "Creating modules variables"
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$DynDnsSession = [ordered]@{
    ClientUrl           = 'https://api.dynect.net'
    User                = $null
    Customer            = $null
    ApiVersion          = $null
    AuthToken           = $null
    StartTime           = $null
    ElapsedTime         = $null
    RefreshTime         = $null
}

[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$DynDnsHistoryList = [System.Collections.Generic.List[object]]::new()
#endregion load module variables

#region Handle Module Removal
$OnRemoveScript = {
# Remove-Variable -Name DynDnsSession -Scope Script -Force
}
$ExecutionContext.SessionState.Module.OnRemove += $OnRemoveScript
Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action $OnRemoveScript
#endregion Handle Module Removal

#region dot source public and private function definition files, export publich functions
try {
    foreach ($Scope in 'Public','Private') {
        Get-ChildItem (Join-Path -Path $ScriptPath -ChildPath $Scope) -Filter *.ps1 | ForEach-Object {
            . $_.FullName
            if ($Scope -eq 'Public') {
                Export-ModuleMember -Function $_.BaseName -ErrorAction Stop
            }
        }
    }
}
catch {
    Write-Error ("{0}: {1}" -f $_.BaseName,$_.Exception.Message)
    exit 1
}
#endregion dot source public and private function definition files, export publich functions

#region PSEdition detection
if ($PSEdition -eq 'Core') {
    Set-Alias -Name 'Invoke-DynDnsRequest' -Value 'Invoke-DynDnsRequestCore'
} else {
    Set-Alias -Name 'Invoke-DynDnsRequest' -Value 'Invoke-DynDnsRequestDesktop'
}
#endregion PSEdition detection

#region classes
class DynDnsRawData {
    hidden [PSCustomObject]$RawData
}

class DynDnsRecord : DynDnsRawData {
    [string]$Zone
    [string]$Name
    [string]$Type
    [int]$TTL
    hidden [string]$RecordId

    DynDnsRecord () {}
    DynDnsRecord ([PSCustomObject]$DnsRecord) {
        $this.Zone = $DnsRecord.zone
        $this.Name = $DnsRecord.fqdn
        $this.Type = $DnsRecord.record_type
        $this.TTL = $DnsRecord.ttl
        $this.RecordId = $DnsRecord.record_id
        $this.RawData = $DnsRecord
    }
}

class DynDnsRecord_A : DynDnsRecord {
    [ipaddress]$Address

    DynDnsRecord_A () {  }
    DynDnsRecord_A ([PSCustomObject]$DnsRecord) {
        $this.Zone = $DnsRecord.zone
        $this.Name = $DnsRecord.fqdn
        $this.Type = $DnsRecord.record_type
        $this.TTL = $DnsRecord.ttl
        $this.Address = $DnsRecord.rdata.address
        $this.RecordId = $DnsRecord.record_id
        $this.RawData = $DnsRecord
    }
}

class DynDnsRecord_TXT : DynDnsRecord {
    [string[]]$Strings

    DynDnsRecord_TXT () {  }
    DynDnsRecord_TXT ([PSCustomObject]$DnsRecord) {
        $this.Zone = $DnsRecord.zone
        $this.Name = $DnsRecord.fqdn
        $this.Type = $DnsRecord.record_type
        $this.Strings = $DnsRecord.rdata.txtdata
        $this.TTL = $DnsRecord.ttl
        $this.RecordId = $DnsRecord.record_id
        $this.RawData = $DnsRecord
    }
}

class DynDnsRecord_CNAME : DynDnsRecord {
    [string]$NameHost

    DynDnsRecord_CNAME () {  }
    DynDnsRecord_CNAME ([PSCustomObject]$DnsRecord) {
        $this.Zone = $DnsRecord.zone
        $this.Name = $DnsRecord.fqdn
        $this.Type = $DnsRecord.record_type
        $this.NameHost = $DnsRecord.rdata.cname
        $this.TTL = $DnsRecord.ttl
        $this.RecordId = $DnsRecord.record_id
        $this.RawData = $DnsRecord
    }
}

class DynDnsRecord_MX : DynDnsRecord {
    [string]$Exchange
    [int]$Preference

    DynDnsRecord_MX () {  }
    DynDnsRecord_MX ([PSCustomObject]$DnsRecord) {
        $this.Zone = $DnsRecord.zone
        $this.Name = $DnsRecord.fqdn
        $this.Type = $DnsRecord.record_type
        $this.Exchange = $DnsRecord.rdata.exchange
        $this.Preference = $DnsRecord.rdata.preference
        $this.TTL = $DnsRecord.ttl
        $this.RecordId = $DnsRecord.record_id
        $this.RawData = $DnsRecord
    }
}

class DynDnsRecord_SRV : DynDnsRecord {
    [string]$Target
    [int]$Port
    [int]$Priority
    [int]$Weight

    DynDnsRecord_SRV () {  }
    DynDnsRecord_SRV ([PSCustomObject]$DnsRecord) {
        $this.Zone = $DnsRecord.zone
        $this.Name = $DnsRecord.fqdn
        $this.Type = $DnsRecord.record_type
        $this.Target = $DnsRecord.rdata.target
        $this.Port = $DnsRecord.rdata.port
        $this.Priority = $DnsRecord.rdata.priority
        $this.Weight = $DnsRecord.rdata.weight
        $this.TTL = $DnsRecord.ttl
        $this.RecordId = $DnsRecord.record_id
        $this.RawData = $DnsRecord
    }
}

class DynDnsRecord_PTR : DynDnsRecord {
    [string]$NameHost

    DynDnsRecord_PTR () {  }
    DynDnsRecord_PTR ([PSCustomObject]$DnsRecord) {
        $this.Zone = $DnsRecord.zone
        $this.Name = $DnsRecord.fqdn
        $this.Type = $DnsRecord.record_type
        $this.NameHost = $DnsRecord.rdata.ptrdname
        $this.TTL = $DnsRecord.ttl
        $this.RecordId = $DnsRecord.record_id
        $this.RawData = $DnsRecord
    }
}

class DynDnsRecord_NS : DynDnsRecord {
    [string]$NameHost
    [string]$Authoritative

    DynDnsRecord_NS () {  }
    DynDnsRecord_NS ([PSCustomObject]$DnsRecord) {
        $this.Zone = $DnsRecord.zone
        $this.Name = $DnsRecord.fqdn
        $this.Type = $DnsRecord.record_type
        $this.NameHost = $DnsRecord.rdata.nsdname
        $this.Authoritative = $DnsRecord.service_class
        $this.TTL = $DnsRecord.ttl
        $this.RecordId = $DnsRecord.record_id
        $this.RawData = $DnsRecord
    }
}

class DynDnsRecord_SOA : DynDnsRecord {
    [string]$Administrator
    [int]$SerialNumber
    [string]$PrimaryServer
    [int]$TimeToExpiration
    [int]$TimeToZoneFailureRetry
    [int]$TimeToZoneRefresh
    [int]$DefaultTTL

    DynDnsRecord_SOA () {  }
    DynDnsRecord_SOA ([PSCustomObject]$DnsRecord) {
        $this.Zone = $DnsRecord.zone
        $this.Name = $DnsRecord.fqdn
        $this.Type = $DnsRecord.record_type
        $this.Administrator = $DnsRecord.rdata.rname
        $this.SerialNumber = $DnsRecord.rdata.serial
        $this.PrimaryServer = $DnsRecord.rdata.mname
        $this.TimeToExpiration = $DnsRecord.rdata.expire
        $this.TimeToZoneFailureRetry = $DnsRecord.rdata.retry
        $this.TimeToZoneRefresh = $DnsRecord.rdata.refresh
        $this.DefaultTTL = $DnsRecord.rdata.minimum
        $this.TTL = $DnsRecord.ttl
        $this.RawData = $DnsRecord
    }
}

class DynDnsHistory {
    [datetime]$Timestamp
    [string]$Command
    [string]$Status
    [string]$JobId
    [string]$Method
    [string]$Body
    [string]$Uri
    [string]$StatusCode
    [string]$StatusDescription
    [string]$ElapsedTime
    [hashtable]$Arguments

    DynDnsHistory () {}
    DynDnsHistory ([PsCustomObject]$DynDnsHistory) {
        $this.Timestamp = [System.DateTime]::Now
        $this.Command = $DynDnsHistory.Command
        $this.Status = $DynDnsHistory.Status
        $this.JobId = $DynDnsHistory.JobId
        $this.Method = $DynDnsHistory.Method
        $this.Body = $DynDnsHistory.Body
        $this.Uri = $DynDnsHistory.Uri
        $this.StatusCode = $DynDnsHistory.StatusCode
        $this.StatusDescription = $DynDnsHistory.StatusDescription
        $this.ElapsedTime = $DynDnsHistory.ElapsedTime
        $this.Arguments = $DynDnsHistory.Arguments
    }
}

class DynDnsTask : DynDnsRawData {
    [int]$TaskId
    [object]$Created
    [object]$Modified
    [string]$CustomerName
    [string]$Zone
    [string]$TaskName
    [string]$Status
    [string]$Message
    [string]$Blocking
    [int]$Steps
    [int]$StepCount
    [object[]]$Arugments
    [string]$Debug

    DynDnsTask () {  }
    DynDnsTask ([PSCustomObject]$DynTask) {
        [datetime]$origin = '1970-01-01 00:00:00'

        $this.TaskId = $DynTask.task_id
        $this.Created = $origin.AddSeconds($DynTask.created_ts).ToLocalTime()
        $this.Modified = $origin.AddSeconds($DynTask.modified_ts).ToLocalTime()
        $this.CustomerName = $DynTask.customer_name
        $this.Zone = $DynTask.zone_name
        $this.TaskName = $DynTask.name
        $this.Status = $DynTask.status
        $this.Message = $DynTask.message
        $this.Blocking = $DynTask.blocking
        $this.Steps = $DynTask.total_steps
        $this.StepCount = $DynTask.step_count
        $this.Arugments = $DynTask.args
        $this.Debug = $DynTask.debug
        $this.RawData = $DynTask
    }
}

class DynDnsZone : DynDnsRawData {
    [string]$Zone
    [int]$SerialNumber
    [string]$SerialStyle
    [string]$Type

    DynDnsZone () {  }
    DynDnsZone ([PSCustomObject]$DynDnsZone) {
        $this.Zone = $DynDnsZone.zone
        $this.SerialNumber = $DynDnsZone.serial
        $this.SerialStyle = $DynDnsZone.serial_style
        $this.Type = $DynDnsZone.zone_type
        $this.RawData = $DynDnsZone
    }
}

class DynDnsZoneNote : DynDnsRawData {
    [string]$Zone
    [object]$Timestamp
    [string]$Type
    [string]$User
    [string]$SerialNumber
    [string]$Note

    DynDnsZoneNote () {  }
    DynDnsZoneNote ([PSCustomObject]$DynDnsZoneNote) {
        [datetime]$origin = '1970-01-01 00:00:00'

        $this.Zone = $DynDnsZoneNote.zone
        $this.Timestamp = $origin.AddSeconds($DynDnsZoneNote.timestamp).ToLocalTime()
        $this.Type = $DynDnsZoneNote.type
        $this.User = $DynDnsZoneNote.user_name
        $this.SerialNumber = $DynDnsZoneNote.serial
        $this.Note = $DynDnsZoneNote.note.trim()
        $this.RawData = $DynDnsZoneNote
    }
}

class DynDnsZoneChanges : DynDnsRawData {
    [string]$Zone
    [string]$UserId
    [string]$Type
    [string]$Name
    [string]$SerialNumber
    [int]$TTL
    [pscustomobject]$RecordData

    DynDnsZoneChanges () {  }
    DynDnsZoneChanges ([PSCustomObject]$DynDnsZoneChanges) {
        $this.Zone = $DynDnsZoneChanges.zone
        $this.UserId = $DynDnsZoneChanges.user_id
        $this.Type = $DynDnsZoneChanges.rdata_type
        $this.Name = $DynDnsZoneChanges.fqdn
        $this.SerialNumber = $DynDnsZoneChanges.serial
        $this.TTL = $DynDnsZoneChanges.ttl
        $this.RecordData = $DynDnsZoneChanges.rdata
        $this.RawData = $DynDnsZoneChanges
    }
}

class DynDnsHttpRedirect  : DynDnsRawData {
    [string]$Zone
    [string]$Name
    [string]$Url
    [string]$ResponseCode
    [object]$IncludeUri

    DynDnsHttpRedirect () {  }
    DynDnsHttpRedirect ([PSCustomObject]$DynDnsHttpRedirect) {
        if ($DynDnsHttpRedirect.keep_uri -match 'Y') {
            $KeepUri = $true
        } else {
            $KeepUri = $false
        }
        $this.Zone = $DynDnsHttpRedirect.zone
        $this.Name = $DynDnsHttpRedirect.fqdn
        $this.Url = $DynDnsHttpRedirect.url
        $this.ResponseCode = $DynDnsHttpRedirect.code
        $this.IncludeUri = $KeepUri
        $this.RawData = $DynDnsHttpRedirect
    }
}

class DynDnsUser : DynDnsRawData {
    [string]$User
    [string]$Status
    [string]$CustomerName
    [string]$Nickname
    [string]$FirstName
    [string]$LastName
    [string]$Email
    [string]$Phone
    [string[]]$Groups

    DynDnsUser () {}
    DynDnsUser ([PSCustomObject]$DynDnsUser) {
        $this.User = $DynDnsUser.user_name
        $this.Status = $DynDnsUser.status
        $this.CustomerName = $DynDnsUser.organization
        $this.Nickname = $DynDnsUser.nickname
        $this.FirstName = $DynDnsUser.first_name
        $this.LastName = $DynDnsUser.last_name
        $this.Email = $DynDnsUser.email
        $this.Phone = $DynDnsUser.phone
        $this.Groups = $DynDnsUser.group_name
        $this.RawData = $DynDnsUser
    }
}

class DynDnsHttpResponse {
    [string]$Method
    [string]$Body
    [string]$Uri
    [string]$StatusCode
    [string]$StatusDescription

    DynDnsHttpResponse () {}
    DynDnsHttpResponse ([PsCustomObject]$DynDnsHttpResponse) {
        $this.Method = $DynDnsHttpResponse.Method
        $this.Body = $DynDnsHttpResponse.Body
        $this.Uri = $DynDnsHttpResponse.Uri
        $this.StatusCode = $DynDnsHttpResponse.StatusCode
        $this.StatusDescription = $DynDnsHttpResponse.StatusDescription
    }
}

class DynDnsRestResponse {
    [PSCustomObject]$Response
    [PSCustomObject]$Data
    [decimal]$ElapsedTime

    DynDnsRestResponse () {  }
    DynDnsRestResponse ([PSCustomObject]$DynDnsRestResponse) {
        $this.Response = $DynDnsRestResponse.Response
        $this.Data = $DynDnsRestResponse.Data
        $this.ElapsedTime = $DynDnsRestResponse.ElapsedTime
    }
}
#endregion classes