replaced hardcoded text by enums in some functions

This commit is contained in:
2022-12-05 17:48:20 +03:00
parent 4fc6bac4b5
commit a1b03b81c0
4 changed files with 39 additions and 27 deletions

12
classlib/enum/types.cs Normal file
View File

@@ -0,0 +1,12 @@
namespace PS.IPAM;
using System;
public enum types {
address,
domain,
section,
subnetwork,
tag,
vlan,
vrf
}

View File

@@ -5,16 +5,13 @@ function Invoke-Request {
[methods] [methods]
$Method, $Method,
[parameter(Mandatory=$true)] [parameter(Mandatory=$true)]
[ValidateSet("user","vlan","subnets","addresses","sections","vrf","l2domains","tools")] [controllers]
[string]
$Controller, $Controller,
[parameter(Mandatory=$false)] [parameter(Mandatory=$false)]
[ValidateSet("address","tag","subnet","vlan","vrf","section")] [types]
[string]
$Type, $Type,
[parameter(Mandatory=$false)] [parameter(Mandatory=$false)]
[ValidateSet("nameservers")] [subcontrollers]
[string]
$SubController, $SubController,
[Parameter(Mandatory=$false)] [Parameter(Mandatory=$false)]
[ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })]
@@ -49,7 +46,7 @@ function Invoke-Request {
Headers = $_headers Headers = $_headers
} }
if ($Method -eq [methods]::POST -or $Method -eq [methods]) { if ($Method -eq [methods]::POST -or $Method -eq [methods]::PATCH) {
if ($Params -is [PSCustomObject]) { if ($Params -is [PSCustomObject]) {
$_params = @{}; $_params = @{};
$Params | Get-Member -MemberType *Property | Where-Object { $Params | Get-Member -MemberType *Property | Where-Object {
@@ -63,11 +60,10 @@ function Invoke-Request {
$_response = Invoke-RestMethod @_arguments $_response = Invoke-RestMethod @_arguments
if ($_response.code -match "20\d") { if ($_response.code -match "20\d") {
switch ($Type) { if ($Type) {
"address" { $_response.data | ForEach-Object { New-Object -TypeName "Address" -ArgumentList $_.psobject.properties.value } } { $_response.data | ForEach-Object { New-Object -TypeName $Type -ArgumentList $_.psobject.properties.value } }
"subnet" { $_response.data | ForEach-Object { New-Object -TypeName "Subnetwork" -ArgumentList $_.psobject.properties.value } } } else {
"tag" { $_response.data | ForEach-Object { New-Object -TypeName "Tag" -ArgumentList $_.psobject.properties.value } } $_response.data
Default { $_response.data }
} }
} else { } else {
throw ("Error - $($_response.code)") throw ("Error - $($_response.code)")

View File

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

View File

@@ -1,6 +1,6 @@
function Get-Tag { function Get-Tag {
[CmdletBinding(DefaultParameterSetName="NoParams")] [CmdletBinding(DefaultParameterSetName="NoParams")]
[OutputType('tag')] [OutputType({[types]::tag})]
param ( param (
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
[ValidateScript({ $_ -match "^\d+$" })] [ValidateScript({ $_ -match "^\d+$" })]
@@ -9,14 +9,18 @@ function Get-Tag {
$Id, $Id,
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[ps.ipam.address] [PS.IPAM.Address]
$InputObject $AddressObject,
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")]
[ValidateNotNullOrEmpty()]
[PS.IPAM.Subnetwork]
$SubnetObject
) )
process { process {
$_params = @{ $_params = @{
Controller = "addresses" Controller = [controllers]::addresses
Method = "GET" Method = [methods]::GET
Type = "tag" Type = [types]::tag
} }
$_identifiers = @("tags") $_identifiers = @("tags")
switch ($PSCmdlet.ParameterSetName) { switch ($PSCmdlet.ParameterSetName) {