first working types

This commit is contained in:
2022-12-06 17:20:45 +03:00
parent a1b03b81c0
commit 7d0d1b53ae
14 changed files with 129 additions and 75 deletions

View File

@@ -2,16 +2,16 @@ function Invoke-Request {
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[methods]
[PS.IPAM.methods]
$Method,
[parameter(Mandatory=$true)]
[controllers]
[PS.IPAM.controllers]
$Controller,
[parameter(Mandatory=$false)]
[types]
[PS.IPAM.types]
$Type,
[parameter(Mandatory=$false)]
[subcontrollers]
[PS.IPAM.subcontrollers]
$SubController,
[Parameter(Mandatory=$false)]
[ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })]
@@ -25,7 +25,7 @@ function Invoke-Request {
if ($_tokenStatus -eq "NoToken") { throw "No session available!" }
if ($_tokenStatus -eq "Expired") { Update-Session }
$Controller = $Controller.ToLower()
$Controller = $Controller
$_uri = "$($script:ipamURL)/api/$($script:ipamAppID)/$Controller"
if ($SubController) { $_uri += "/$SubController" }
@@ -46,7 +46,7 @@ function Invoke-Request {
Headers = $_headers
}
if ($Method -eq [methods]::POST -or $Method -eq [methods]::PATCH) {
if ($Method -eq [PS.IPAM.methods]::POST -or $Method -eq [PS.IPAM.methods]::PATCH) {
if ($Params -is [PSCustomObject]) {
$_params = @{};
$Params | Get-Member -MemberType *Property | Where-Object {
@@ -60,10 +60,22 @@ function Invoke-Request {
$_response = Invoke-RestMethod @_arguments
if ($_response.code -match "20\d") {
if ($Type) {
{ $_response.data | ForEach-Object { New-Object -TypeName $Type -ArgumentList $_.psobject.properties.value } }
if ($Type -is [PS.IPAM.types]) {
switch ($Type) {
"vlan" {
$_response.data | ForEach-Object {
New-Object -TypeName ([PS.IPAM.Vlan]) -ArgumentList (@($_.psobject.properties.value[0..6]) + ($_ | Select-Object -Property custom_* -ExcludeProperty custom_*))
}; break
}
"subnetwork" {
$_response.data | ForEach-Object {
New-Object -TypeName ([PS.IPAM.Subnetwork]) -ArgumentList (@($_.psobject.properties.value[0..30]) + ($_ | Select-Object -Property custom_* -ExcludeProperty custom_*))
}; break
}
Default { $_response.data | ForEach-Object { New-Object -TypeName ("PS.IPAM.$Type") -ArgumentList $_.psobject.properties.value } }
}
} else {
$_response.data
return $_response.data
}
} else {
throw ("Error - $($_response.code)")

View File

@@ -1,6 +1,6 @@
function Get-Address {
[CmdletBinding(DefaultParameterSetName="ByID")]
[OutputType({[types]::address})]
[OutputType([PS.IPAM.address])]
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
[ValidateScript({ $_ -match "^\d+$" })]
@@ -35,29 +35,29 @@ function Get-Address {
)
process {
$_params = @{
Controller = [controllers]::addresses
Method = [methods]::GET
Type = [types]::address
Controller = [PS.IPAM.controllers]::addresses
Method = [PS.IPAM.methods]::GET
Type = [PS.IPAM.types]::address
}
switch ($PSCmdlet.ParameterSetName) {
"ByID" { $_identifiers = @($id) }
"ByIP" { $_identifiers = ("search",$IP) }
"ByHostName" { $_identifiers = ("search_hostname",$HostName) }
"ByTag" { $_identifiers = ("tags",$TagId,[controllers]::addresses) }
"ByTag" { $_identifiers = ("tags",$TagId,[PS.IPAM.controllers]::addresses) }
"BySubnetId" {
if ($IP) {
$_identifiers = ($IP,$SubnetId)
} else {
$_params.Item("Controller") = [controllers]::subnets
$_identifiers = ($SubnetId,[controllers]::addresses)
$_params.Item("Controller") = [PS.IPAM.controllers]::subnets
$_identifiers = ($SubnetId,[PS.IPAM.controllers]::addresses)
}
}
"BySubnetCIDR" {
$_params.Item("Controller") = [controllers]::subnets
$_params.Item("Controller") = [PS.IPAM.controllers]::subnets
$_subnetId = (Get-Subnet -CIDR $SubnetCIDR).id
if (!$_subnetId) { throw "Cannot find subnet!" }
$_identifiers = ($_subnetId,[controllers]::addresses)
$_identifiers = ($_subnetId,[PS.IPAM.controllers]::addresses)
}
}
$_params.Add("Identifiers",$_identifiers)

View File

@@ -10,19 +10,25 @@ function Get-FirstFreeIP {
[ValidateScript({ $_ -match "^\d+$" })]
[ValidateNotNullOrEmpty()]
[string]
$Id
$Id,
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")]
[ValidateNotNullOrEmpty()]
[PS.IPAM.Subnetwork]
$SubnetObject
)
process {
$_params = @{
Controller = "subnets"
Method = "GET"
Controller = [PS.IPAM.controllers]::subnets
Method = [PS.IPAM.methods]::GET
}
switch ($PSCmdlet.ParameterSetName) {
"ByID" { $_subnetId = $Id }
"ByID" { $_subnetId = $Id; break }
"ByCIDR" {
$_subnetId = (Get-Subnet -CIDR $CIDR).id
if (!$_subnetId) { throw "Cannot find subnet!" }
break
}
"BySubnetObject" { $_subnetId = $SubnetObject.Id; break }
}
$_identifiers = @($_subnetId,"first_free")
$_params.Add("Identifiers",$_identifiers)

View File

@@ -1,26 +1,29 @@
function Get-L2Domain {
[CmdletBinding(DefaultParameterSetName="ByID")]
[CmdletBinding(DefaultParameterSetName="NoParams")]
[OutputType([PS.IPAM.Domain])]
param (
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
[ValidateScript({ $_ -match "^\d+$" })]
[ValidateNotNullOrEmpty()]
[string]
[int]
$Id
)
process {
[string[]]$visiblePropertiesList = @('Id','Name','Description')
$visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList)
$_params = @{
Controller = "l2domains"
Method = "GET"
Controller = [PS.IPAM.controllers]::l2domains
Method = [PS.IPAM.methods]::GET
Type = [PS.IPAM.types]::Domain
}
$_identifiers = @($Id)
switch ($PSCmdlet.ParameterSetName) {
"ByID" {
$_identifiers = @($Id)
}
}
$_params.Add("Identifiers",$_identifiers)
Invoke-Request @_params | `
Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru
Invoke-Request @_params
}
}
Export-ModuleMember -Function Get-L2Domain

View File

@@ -1,5 +1,6 @@
function Get-Subnet {
[CmdletBinding(DefaultParameterSetName="ByID")]
[CmdletBinding(DefaultParameterSetName="NoParams")]
[OutputType([PS.IPAM.Subnetwork])]
param (
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByCIDR")]
[ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})]
@@ -52,13 +53,10 @@ function Get-Subnet {
$Recurse
)
process {
[string[]]$visiblePropertiesList = @('Id','Subnet','Mask','Description')
$visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList)
$_params = @{
Controller = "subnets"
Method = "GET"
Type = "subnet"
Controller = [PS.IPAM.controllers]::subnets
Method = [PS.IPAM.methods]::GET
Type = [PS.IPAM.types]::Subnetwork
}
switch ($PSCmdlet.ParameterSetName) {
"ByCIDR" {

View File

@@ -1,6 +1,6 @@
function Get-Tag {
[CmdletBinding(DefaultParameterSetName="NoParams")]
[OutputType({[types]::tag})]
[OutputType([PS.IPAM.Tag])]
param (
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
[ValidateScript({ $_ -match "^\d+$" })]
@@ -18,16 +18,24 @@ function Get-Tag {
)
process {
$_params = @{
Controller = [controllers]::addresses
Method = [methods]::GET
Type = [types]::tag
Controller = [PS.IPAM.controllers]::addresses
Method = [PS.IPAM.methods]::GET
Type = [PS.IPAM.types]::tag
}
$_identifiers = @("tags")
switch ($PSCmdlet.ParameterSetName) {
"ByID" { $_identifiers += $Id }
"ByID" { $_identifiers += $Id; break }
"ByAddressObject" {
if ($InputObject.TagId) {
$_identifiers += $InputObject.TagId
if ($AddressObject.TagId) {
$_identifiers += $AddressObject.TagId
} else {
return $null
}
break
}
"BySubnetObject" {
if ($SubnetObject.TagId) {
$_identifiers += $SubnetObject.TagId
} else {
return $null
}

View File

@@ -1,5 +1,6 @@
function Get-Vlan {
[CmdletBinding(DefaultParameterSetName="ByID")]
[OutputType([PS.IPAM.Vlan])]
param (
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
[ValidateScript({ $_ -match "^\d+$" })]
@@ -15,31 +16,40 @@ function Get-Vlan {
[ValidateScript({ $_ -match "^\d+$" })]
[ValidateNotNullOrEmpty()]
[string]
$L2DomainId
$L2DomainId,
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")]
[ValidateNotNullOrEmpty()]
[PS.IPAM.Subnetwork]
$SubnetObject
)
process {
[string[]]$visiblePropertiesList = @('Id','Name','Description')
$visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList)
$_params = @{
Controller = "vlan"
Method = "GET"
Controller = [PS.IPAM.controllers]::vlan
Method = [PS.IPAM.methods]::GET
Type = [PS.IPAM.types]::Vlan
}
switch ($PSCmdlet.ParameterSetName) {
"ByID" { $_identifiers = @($Id) }
"ByNumber" { $_identifiers = @("search",$Number) }
"ByID" { $_identifiers = @($Id); break }
"ByNumber" { $_identifiers = @("search",$Number); break }
"ByL2Domain"{
$_params.Item("Controller") = "l2domains"
$_params.Item("Controller") = [PS.IPAM.controllers]::l2domains
$_l2domainId = $L2DomainId
$_identifiers = @($_l2domainId,"vlans")
$_identifiers = @($_l2domainId,[PS.IPAM.subcontrollers]::vlans)
break
}
"BySubnetObject" {
if ($SubnetObject.VlanId) {
$_identifiers = @($SubnetObject.VlanId); break
} else {
return $null
}
}
}
$_params.Add("Identifiers",$_identifiers)
Invoke-Request @_params | Select-Object @{n="id";e={$_.vlanId}},* | Select-Object -ExcludeProperty vlanId | `
Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru
Invoke-Request @_params
}
}
Export-ModuleMember -Function Get-Vlan