replaced hardcoded text by enums in some functions
This commit is contained in:
12
classlib/enum/types.cs
Normal file
12
classlib/enum/types.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace PS.IPAM;
|
||||
using System;
|
||||
|
||||
public enum types {
|
||||
address,
|
||||
domain,
|
||||
section,
|
||||
subnetwork,
|
||||
tag,
|
||||
vlan,
|
||||
vrf
|
||||
}
|
||||
@@ -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)")
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user