This commit is contained in:
2022-12-05 08:48:37 +03:00
parent 800d788838
commit 09cd00dc66
35 changed files with 642 additions and 142 deletions

View File

@@ -1,4 +1,4 @@
function Invoke-PSIPAMRequest {
function Invoke-Request {
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
@@ -10,6 +10,10 @@ function Invoke-PSIPAMRequest {
[string]
$Controller,
[parameter(Mandatory=$false)]
[ValidateSet("address","tag","subnet")]
[string]
$Type,
[parameter(Mandatory=$false)]
[ValidateSet("nameservers")]
[string]
$SubController,
@@ -20,10 +24,10 @@ function Invoke-PSIPAMRequest {
[array]
$Identifiers
)
$_tokenStatus = Test-PSIPAMSession
$_tokenStatus = Test-Session
if ($_tokenStatus -eq "NoToken") { throw "No session available!" }
if ($_tokenStatus -eq "Expired") { Update-PSIPAMSession }
if ($_tokenStatus -eq "Expired") { Update-Session }
$Controller = $Controller.ToLower()
@@ -34,7 +38,10 @@ function Invoke-PSIPAMRequest {
$_headers = @{
"Accept" = "application/json"
"Content-Type" = "application/json"
"token" = $script:ipamToken
}
switch ($script:ipamAuthType) {
"Credentials" { $_headers.Add("token", $script:ipamToken) }
"Token" { $_headers.Add("phpipam-token", $script:ipamToken) }
}
$_arguments = @{
@@ -57,7 +64,12 @@ function Invoke-PSIPAMRequest {
$_response = Invoke-RestMethod @_arguments
if ($_response.code -match "20\d") {
return $_response.data
switch ($Type) {
"address" { $_response.data | ForEach-Object { New-Object -TypeName "PS.IPAM.Address" -ArgumentList $_.psobject.properties.value } }
"subnet" { $_response.data | ForEach-Object { New-Object -TypeName "PS.IPAM.Subnetwork" -ArgumentList $_.psobject.properties.value } }
"tag" { $_response.data | ForEach-Object { New-Object -TypeName "PS.IPAM.Tag" -ArgumentList $_.psobject.properties.value } }
Default { $_response.data }
}
} else {
throw ("Error - $($_response.code)")
}

View File

@@ -1,15 +0,0 @@
function Test-PSIPAMSession {
[CmdletBinding()]
param (
)
if ($script:ipamToken) {
if ($script:ipamExpires -lt (Get-Date)) {
return "Expired"
} else {
return "Valid"
}
} else {
return "NoToken"
}
}

View File

@@ -0,0 +1,19 @@
function Test-Session {
[CmdletBinding()]
param (
)
if ($script:ipamToken) {
if ($script:ipamExpires -eq "Never") {
return "Valid"
} else {
if ($script:ipamExpires -lt (Get-Date)) {
return "Expired"
} else {
return "Valid"
}
}
} else {
return "NoToken"
}
}

View File

@@ -1,18 +1,18 @@
function Update-PSIPAMSession {
function Update-Session {
[CmdletBinding()]
param (
[switch]
$Force
)
$_tokenStatus = Test-PSIPAMSession
$_tokenStatus = Test-Session
if ($_tokenStatus -eq "NoToken") {
throw "No session available!"
}
if ($_tokenStatus -eq "Valid") {
return (Invoke-PSIPAMRequest -Method PATCH -Controller user).expires
return (Invoke-Request -Method PATCH -Controller user).expires
}
if ($_tokenStatus -eq "Expired" -or $Force) {
New-PSIPAMSession -URL $script:ipamURL -AppID $script:ipamAppID -Credentials $script:ipamCredentials
New-Session -URL $script:ipamURL -AppID $script:ipamAppID -Credentials $script:ipamCredentials
return $script:ipamExpires
}
}