From a1b03b81c0ae44b55688aed42f61e1a182e7e920 Mon Sep 17 00:00:00 2001 From: Arnike Date: Mon, 5 Dec 2022 17:48:20 +0300 Subject: [PATCH] replaced hardcoded text by enums in some functions --- classlib/enum/types.cs | 12 ++++++++++++ functions/private/Invoke-Request.ps1 | 20 ++++++++------------ functions/public/Get-Address.ps1 | 18 +++++++++--------- functions/public/Get-Tag.ps1 | 16 ++++++++++------ 4 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 classlib/enum/types.cs diff --git a/classlib/enum/types.cs b/classlib/enum/types.cs new file mode 100644 index 0000000..b438a03 --- /dev/null +++ b/classlib/enum/types.cs @@ -0,0 +1,12 @@ +namespace PS.IPAM; +using System; + +public enum types { + address, + domain, + section, + subnetwork, + tag, + vlan, + vrf +} \ No newline at end of file diff --git a/functions/private/Invoke-Request.ps1 b/functions/private/Invoke-Request.ps1 index 81640b2..1a7dff9 100644 --- a/functions/private/Invoke-Request.ps1 +++ b/functions/private/Invoke-Request.ps1 @@ -5,16 +5,13 @@ function Invoke-Request { [methods] $Method, [parameter(Mandatory=$true)] - [ValidateSet("user","vlan","subnets","addresses","sections","vrf","l2domains","tools")] - [string] + [controllers] $Controller, [parameter(Mandatory=$false)] - [ValidateSet("address","tag","subnet","vlan","vrf","section")] - [string] + [types] $Type, [parameter(Mandatory=$false)] - [ValidateSet("nameservers")] - [string] + [subcontrollers] $SubController, [Parameter(Mandatory=$false)] [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] @@ -49,7 +46,7 @@ function Invoke-Request { 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]) { $_params = @{}; $Params | Get-Member -MemberType *Property | Where-Object { @@ -63,11 +60,10 @@ function Invoke-Request { $_response = Invoke-RestMethod @_arguments if ($_response.code -match "20\d") { - switch ($Type) { - "address" { $_response.data | ForEach-Object { New-Object -TypeName "Address" -ArgumentList $_.psobject.properties.value } } - "subnet" { $_response.data | ForEach-Object { New-Object -TypeName "Subnetwork" -ArgumentList $_.psobject.properties.value } } - "tag" { $_response.data | ForEach-Object { New-Object -TypeName "Tag" -ArgumentList $_.psobject.properties.value } } - Default { $_response.data } + if ($Type) { + { $_response.data | ForEach-Object { New-Object -TypeName $Type -ArgumentList $_.psobject.properties.value } } + } else { + $_response.data } } else { throw ("Error - $($_response.code)") diff --git a/functions/public/Get-Address.ps1 b/functions/public/Get-Address.ps1 index a3efc3c..6c4610c 100644 --- a/functions/public/Get-Address.ps1 +++ b/functions/public/Get-Address.ps1 @@ -1,6 +1,6 @@ function Get-Address { [CmdletBinding(DefaultParameterSetName="ByID")] - [OutputType('address')] + [OutputType({[types]::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 = "addresses" - Method = "GET" - Type = "address" + Controller = [controllers]::addresses + Method = [methods]::GET + Type = [types]::address } switch ($PSCmdlet.ParameterSetName) { "ByID" { $_identifiers = @($id) } "ByIP" { $_identifiers = ("search",$IP) } "ByHostName" { $_identifiers = ("search_hostname",$HostName) } - "ByTag" { $_identifiers = ("tags",$TagId,"addresses") } + "ByTag" { $_identifiers = ("tags",$TagId,[controllers]::addresses) } "BySubnetId" { if ($IP) { $_identifiers = ($IP,$SubnetId) } else { - $_params.Item("Controller") = "subnets" - $_identifiers = ($SubnetId,"addresses") + $_params.Item("Controller") = [controllers]::subnets + $_identifiers = ($SubnetId,[controllers]::addresses) } } "BySubnetCIDR" { - $_params.Item("Controller") = "subnets" + $_params.Item("Controller") = [controllers]::subnets $_subnetId = (Get-Subnet -CIDR $SubnetCIDR).id if (!$_subnetId) { throw "Cannot find subnet!" } - $_identifiers = ($_subnetId,"addresses") + $_identifiers = ($_subnetId,[controllers]::addresses) } } $_params.Add("Identifiers",$_identifiers) diff --git a/functions/public/Get-Tag.ps1 b/functions/public/Get-Tag.ps1 index 02cec76..393bc4e 100644 --- a/functions/public/Get-Tag.ps1 +++ b/functions/public/Get-Tag.ps1 @@ -1,6 +1,6 @@ function Get-Tag { [CmdletBinding(DefaultParameterSetName="NoParams")] - [OutputType('tag')] + [OutputType({[types]::tag})] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] [ValidateScript({ $_ -match "^\d+$" })] @@ -9,14 +9,18 @@ function Get-Tag { $Id, [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")] [ValidateNotNullOrEmpty()] - [ps.ipam.address] - $InputObject + [PS.IPAM.Address] + $AddressObject, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Subnetwork] + $SubnetObject ) process { $_params = @{ - Controller = "addresses" - Method = "GET" - Type = "tag" + Controller = [controllers]::addresses + Method = [methods]::GET + Type = [types]::tag } $_identifiers = @("tags") switch ($PSCmdlet.ParameterSetName) {