Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/functions/private/Auth/Get-DomeneshopConfig.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Get-DomeneshopConfig {
[CmdletBinding()]
param()

$vault = 'Domeneshop'
$id = '__Domeneshop.Config'
$config = Get-Context -ID $id -Vault $vault -ErrorAction SilentlyContinue

if (-not $config) {
$config = [ordered]@{
DefaultContext = $null
ApiBaseUri = 'https://api.domeneshop.no/v0'
}
Set-Context -ID $id -Vault $vault -Context $config
$config = Get-Context -ID $id -Vault $vault
}

$config
}
23 changes: 23 additions & 0 deletions src/functions/private/Auth/Resolve-DomeneshopContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function Resolve-DomeneshopContext {
[CmdletBinding()]
param(
[Parameter()]
[string] $Context
)

$resolvedContext = if ($Context) {
Get-DomeneshopContext -Context $Context
} else {
Get-DomeneshopContext
}

if (-not $resolvedContext) {
throw "No Domeneshop context found. Run 'Connect-DomeneshopAccount' first."
}

if ([string]::IsNullOrEmpty($resolvedContext.Token) -or -not ($resolvedContext.Secret -is [securestring])) {
throw "The Domeneshop context [$($resolvedContext.ID)] is missing valid credentials."
}

$resolvedContext
}
11 changes: 11 additions & 0 deletions src/functions/private/Auth/Set-DomeneshopDefaultContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Set-DomeneshopDefaultContext {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string] $Context
)

$config = Get-DomeneshopConfig
$config.DefaultContext = $Context
Set-Context -ID '__Domeneshop.Config' -Vault 'Domeneshop' -Context $config
}
14 changes: 14 additions & 0 deletions src/functions/private/Get-DomeneshopApiBaseUri.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function Get-DomeneshopApiBaseUri {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[object] $Context
)

$apiBaseUri = [string] $Context.ApiBaseUri
if ([string]::IsNullOrEmpty($apiBaseUri)) {
$apiBaseUri = 'https://api.domeneshop.no/v0'
}

$apiBaseUri.TrimEnd('/')
}
37 changes: 37 additions & 0 deletions src/functions/private/Invoke-DomeneshopApiRequest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function Invoke-DomeneshopApiRequest {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateSet('Get', 'Post', 'Put', 'Delete')]
[string] $Method,

[Parameter(Mandatory)]
[string] $Uri,

[Parameter(Mandatory)]
[object] $Context,

[Parameter()]
[AllowNull()]
[object] $Body
)

$credential = [pscredential]::new(
[string] $Context.Token,
[securestring] $Context.Secret
)

$params = @{
Method = $Method
Uri = $Uri
Authentication = 'Basic'
Credential = $credential
}

if ($PSBoundParameters.ContainsKey('Body')) {
$params['ContentType'] = 'application/json'
$params['Body'] = ($Body | ConvertTo-Json -Depth 100)
}

Invoke-RestMethod @params
}
56 changes: 56 additions & 0 deletions src/functions/public/Auth/Connect-DomeneshopAccount.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function Connect-DomeneshopAccount {
<#
.SYNOPSIS
Stores Domeneshop API credentials in a secure context.

.DESCRIPTION
Stores Domeneshop API credentials using the Context module and optionally sets the context as default.

.EXAMPLE
Connect-DomeneshopAccount -Token 'my-token' -Secret (Read-Host -AsSecureString)
#>
[OutputType([object])]
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string] $Token,

[Parameter(Mandatory)]
[object] $Secret,

[Parameter()]
[string] $Context = 'default',

[Parameter()]
[switch] $Default,

[Parameter()]
[switch] $PassThru
)

$secureSecret = switch ($Secret) {
{ $_ -is [securestring] } { $_; break }
{ $_ -is [string] } { ConvertTo-SecureString -AsPlainText $Secret -Force; break }
default { throw 'Secret must be a SecureString or String value.' }
}

$contextObject = [ordered]@{
Name = $Context
Token = $Token
Secret = $secureSecret
AuthType = 'BasicAuth'
ApiBaseUri = 'https://api.domeneshop.no/v0'
ConnectedAt = Get-Date
}

Set-Context -ID $Context -Vault 'Domeneshop' -Context $contextObject

$config = Get-DomeneshopConfig
if ($Default -or [string]::IsNullOrEmpty($config.DefaultContext)) {
Set-DomeneshopDefaultContext -Context $Context
}

if ($PassThru) {
Get-DomeneshopContext -Context $Context
}
}
35 changes: 35 additions & 0 deletions src/functions/public/Auth/Get-DomeneshopContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Get-DomeneshopContext {
<#
.SYNOPSIS
Gets stored Domeneshop contexts.

.DESCRIPTION
Returns one or more contexts stored in the Domeneshop context vault.

.EXAMPLE
Get-DomeneshopContext
#>
[OutputType([object])]
[CmdletBinding(DefaultParameterSetName = 'GetDefault')]
param(
[Parameter(Mandatory, ParameterSetName = 'GetNamed')]
[string] $Context,

[Parameter(Mandatory, ParameterSetName = 'ListAvailable')]
[switch] $ListAvailable
)

$id = switch ($PSCmdlet.ParameterSetName) {
'GetNamed' { $Context; break }
'ListAvailable' { '*'; break }
default {
$config = Get-DomeneshopConfig
if ([string]::IsNullOrEmpty($config.DefaultContext)) {
throw "No default Domeneshop context found. Run 'Connect-DomeneshopAccount' first."
}
$config.DefaultContext
}
}

Get-Context -ID $id -Vault 'Domeneshop' | Where-Object { $_.ID -ne '__Domeneshop.Config' } | Sort-Object -Property ID
}
32 changes: 32 additions & 0 deletions src/functions/public/Ddns/Update-DomeneshopDdns.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function Update-DomeneshopDdns {
<#
.SYNOPSIS
Updates dynamic DNS for a hostname.

.DESCRIPTION
Calls the Domeneshop DDNS update endpoint. The API creates the A/AAAA record if it does not exist.
#>
[OutputType([object])]
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[Alias('Host')]
[string] $Hostname,

[Parameter()]
[Alias('IP')]
[string] $MyIP,

[Parameter()]
[string] $Context
)

$resolvedContext = Resolve-DomeneshopContext -Context $Context
$apiBaseUri = Get-DomeneshopApiBaseUri -Context $resolvedContext
$uri = "$apiBaseUri/dyndns/update?hostname=$([uri]::EscapeDataString($Hostname))"
if ($PSBoundParameters.ContainsKey('MyIP')) {
$uri = "$uri&myip=$([uri]::EscapeDataString($MyIP))"
}

Invoke-DomeneshopApiRequest -Method Get -Uri $uri -Context $resolvedContext
}
24 changes: 24 additions & 0 deletions src/functions/public/Dns/Add-DomeneshopDnsRecord.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function Add-DomeneshopDnsRecord {
<#
.SYNOPSIS
Adds a DNS record to a Domeneshop domain.
#>
[OutputType([object])]
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[int] $DomainID,

[Parameter(Mandatory)]
[object] $Record,

[Parameter()]
[string] $Context
)

$resolvedContext = Resolve-DomeneshopContext -Context $Context
$apiBaseUri = Get-DomeneshopApiBaseUri -Context $resolvedContext
$uri = "$apiBaseUri/domains/$DomainID/dns"

Invoke-DomeneshopApiRequest -Method Post -Uri $uri -Context $resolvedContext -Body $Record
}
55 changes: 55 additions & 0 deletions src/functions/public/Dns/Get-DomeneshopDnsRecord.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function Get-DomeneshopDnsRecord {
<#
.SYNOPSIS
Gets DNS records for a Domeneshop domain.

.DESCRIPTION
Lists DNS records for a domain, or gets one DNS record by ID.
#>
[OutputType([object[]])]
[CmdletBinding(DefaultParameterSetName = 'List')]
param(
[Parameter(Mandatory)]
[int] $DomainID,

[Parameter(Mandatory, ParameterSetName = 'GetByID')]
[int] $RecordID,

[Parameter(ParameterSetName = 'List')]
[Alias('Host')]
[string] $RecordHost,

[Parameter(ParameterSetName = 'List')]
[string] $Type,

[Parameter(ParameterSetName = 'List')]
[string] $Data,

[Parameter()]
[string] $Context
)

$resolvedContext = Resolve-DomeneshopContext -Context $Context
$apiBaseUri = Get-DomeneshopApiBaseUri -Context $resolvedContext
$uri = "$apiBaseUri/domains/$DomainID/dns"

if ($PSCmdlet.ParameterSetName -eq 'GetByID') {
$uri = "$uri/$RecordID"
} else {
$queryParts = @()
if ($PSBoundParameters.ContainsKey('RecordHost')) {
$queryParts += "host=$([uri]::EscapeDataString($RecordHost))"
}
if ($PSBoundParameters.ContainsKey('Type')) {
$queryParts += "type=$([uri]::EscapeDataString($Type))"
}
if ($PSBoundParameters.ContainsKey('Data')) {
$queryParts += "data=$([uri]::EscapeDataString($Data))"
}
if ($queryParts.Count -gt 0) {
$uri = "${uri}?{0}" -f ($queryParts -join '&')
}
}

Invoke-DomeneshopApiRequest -Method Get -Uri $uri -Context $resolvedContext
}
26 changes: 26 additions & 0 deletions src/functions/public/Dns/Remove-DomeneshopDnsRecord.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function Remove-DomeneshopDnsRecord {
<#
.SYNOPSIS
Removes a DNS record from a Domeneshop domain.
#>
[OutputType([object])]
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param(
[Parameter(Mandatory)]
[int] $DomainID,

[Parameter(Mandatory)]
[int] $RecordID,

[Parameter()]
[string] $Context
)

$resolvedContext = Resolve-DomeneshopContext -Context $Context
$apiBaseUri = Get-DomeneshopApiBaseUri -Context $resolvedContext
$uri = "$apiBaseUri/domains/$DomainID/dns/$RecordID"

if ($PSCmdlet.ShouldProcess("Domain $DomainID DNS record $RecordID", 'Remove')) {
Invoke-DomeneshopApiRequest -Method Delete -Uri $uri -Context $resolvedContext
}
}
27 changes: 27 additions & 0 deletions src/functions/public/Dns/Set-DomeneshopDnsRecord.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Set-DomeneshopDnsRecord {
<#
.SYNOPSIS
Updates a DNS record on a Domeneshop domain.
#>
[OutputType([object])]
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[int] $DomainID,

[Parameter(Mandatory)]
[int] $RecordID,

[Parameter(Mandatory)]
[object] $Record,

[Parameter()]
[string] $Context
)

$resolvedContext = Resolve-DomeneshopContext -Context $Context
$apiBaseUri = Get-DomeneshopApiBaseUri -Context $resolvedContext
$uri = "$apiBaseUri/domains/$DomainID/dns/$RecordID"

Invoke-DomeneshopApiRequest -Method Put -Uri $uri -Context $resolvedContext -Body $Record
}
Loading
Loading