diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4abcf34 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.dll +bin +obj +.code-workspace diff --git a/README.md b/README.md index ded5ff1..a8bce5c 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ [![License](https://img.shields.io/badge/license-GPL2+-blue.svg)](https://git.arnike.ru/Arnike/ps.ipam/src/branch/main/LICENSE) ![Powershell](https://img.shields.io/badge/powershell-v5.1+-blue.svg) +![PowerShell Gallery](https://img.shields.io/powershellgallery/dt/ps.ipam)

PS.IPAM

diff --git a/classlib/class/address.cs b/classlib/class/address.cs new file mode 100644 index 0000000..1d15998 --- /dev/null +++ b/classlib/class/address.cs @@ -0,0 +1,77 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public class Address { + public int Id { get; } + public int SubnetId { get; } + public string Ip { get; } + public bool IsGateway { get; } + public string Description { get; } + public string Hostname { get; } + public string MAC { get; } + public string Owner { get; } + public int TagId { get; } + public int DeviceId { get; } + public string Location { get; } + public string Port { get; } + public string Note { get; } + public DateTime? LastSeen { get; } + public bool ExcludePing { get; } + public bool PTRignore { get; } + public int PTR { get; } + public string FirewallAddressObject { get; } + public DateTime? EditDate { get; } + public int CustomerId { get; } + public Object? ExtendedData { get; } + + public Address( + int id, + int subnetId, + string ip, + bool is_gateway, + string description, + string hostname, + string mac, + string owner, + int tag, + int deviceId, + string location, + string port, + string note, + DateTime? lastSeen, + bool excludePing, + bool PTRignore, + int PTR, + string firewallAddressObject, + DateTime? editDate, + int customer_id, + Object? extendedData + ) { + this.Id = id; + this.SubnetId = subnetId; + this.Ip = ip; + this.IsGateway = is_gateway; + this.Description = description; + this.Hostname = hostname; + this.MAC = mac; + this.Owner = owner; + this.TagId = tag; + this.DeviceId = deviceId; + this.Location = location; + this.Port = port; + this.Note = note; + this.EditDate = lastSeen; + this.ExcludePing = excludePing; + this.PTRignore = PTRignore; + this.PTR = PTR; + this.FirewallAddressObject = firewallAddressObject; + this.EditDate = editDate; + this.CustomerId = customer_id; + this.ExtendedData = extendedData; + } + + public override string ToString() { + return this.Ip; + } +} \ No newline at end of file diff --git a/classlib/class/domain.cs b/classlib/class/domain.cs new file mode 100644 index 0000000..36b8530 --- /dev/null +++ b/classlib/class/domain.cs @@ -0,0 +1,27 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public class Domain { + public int Id { get; } + public string Name { get; } + public string Description { get; } + public string Sections { get; } + + public Domain ( + int id, + string name, + string description, + string sections + ) { + this.Id = id; + this.Name = name; + this.Description = description; + this.Sections = sections; + } + + public override string ToString() + { + return this.Name; + } +} \ No newline at end of file diff --git a/classlib/class/section.cs b/classlib/class/section.cs new file mode 100644 index 0000000..c8589a7 --- /dev/null +++ b/classlib/class/section.cs @@ -0,0 +1,57 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public class Section { + public int Id { get; } + public string Name { get; } + public string Description { get; } + public int MasterSectionId { get; } + public string Permissions { get; } + public bool StrictMode { get; } + public string SubnetOrdering { get; } + public int Order { get; } + public DateTime? EditDate { get; } + public bool ShowSubnet { get; } + public bool ShowVlan { get; } + public bool ShowVRF { get; } + public bool ShowSupernetOnly { get; } + public int DNSId { get; } + + public Section ( + int id, + string name, + string description, + int masterSectionId, + string permissions, + bool strictMode, + string subnetOrdering, + int order, + DateTime? editDate, + bool showSubnet, + bool showVlan, + bool showVRF, + bool showSupernetOnly, + int dnsId + ) { + this.Id = id; + this.Name = name; + this.Description = description; + this.MasterSectionId = masterSectionId; + this.Permissions = permissions; + this.StrictMode = strictMode; + this.SubnetOrdering = subnetOrdering; + this.Order = order; + this.EditDate = editDate; + this.ShowSubnet = showSubnet; + this.ShowVlan = showVlan; + this.ShowVRF = showVRF; + this.ShowSupernetOnly = showSupernetOnly; + this.DNSId = dnsId; + } + + public override string ToString() + { + return this.Name; + } +} \ No newline at end of file diff --git a/classlib/class/session.cs b/classlib/class/session.cs new file mode 100644 index 0000000..774d011 --- /dev/null +++ b/classlib/class/session.cs @@ -0,0 +1,32 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public class Session { + public AuthType AuthType { get; } + public string Token { get; } + public string AppID { get; } + public string URL { get; } + public DateTime? Expires { get; } + public Object? Credentials { get; } + public Session( + AuthType authType, + string token, + string appId, + string url, + DateTime? expires, + Object? credentials + ) { + AuthType = authType; + Token = token; + AppID = appId; + URL = url; + Expires = expires; + Credentials = credentials; + } + + public override string ToString() + { + return base.ToString(); + } +} \ No newline at end of file diff --git a/classlib/class/subnet.cs b/classlib/class/subnet.cs new file mode 100644 index 0000000..45cdf2a --- /dev/null +++ b/classlib/class/subnet.cs @@ -0,0 +1,114 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public class Subnetwork { + public int Id { get; } + public string Subnet { get; } + public int Mask { get; } + public int SectionId { get; } + public string Description { get; } + public string LinkedSubnet { get; } + public string FirewallAddressObject { get; } + public int VrfId { get; } + public int MasterSubnetId { get; } + public bool AllowRequests { get; } + public int VlanId { get; } + public bool ShowName { get; } + public int DeviceId { get; } + public string Permissions { get; } + public bool PingSubnet { get; } + public bool DiscoverSubnet { get; } + public bool ResolveDNS { get; } + public bool DNSRecursive { get; } + public bool DNSRecords { get; } + public int NameserverId { get; } + public bool ScanAgent { get; } + public bool IsFolder { get; } + public bool IsFull { get; } + public bool IsPool { get; } + public int TagId { get; } + public int Threshold { get; } + public int LocationId { get; } + public DateTime? EditDate { get; } + public DateTime? LastScan { get; } + public DateTime? LastDiscovery { get; } + public Object Calculation { get; } + public Object? ExtendedData { get; } + public Subnetwork( + int id, + string subnet, + int mask, + int sectionId, + string description, + string linkedSubnet, + string firewallAddressObject, + int vrfId, + int masterSubnetId, + bool allowRequests, + int vlanId, + bool showName, + int deviceId, + string permissions, + bool pingSubnet, + bool discoverSubnet, + bool resolveDNS, + bool dnsRecursive, + bool dnsRecords, + int nameserverId, + bool scanAgent, + bool isFolder, + bool isFull, + bool isPool, + int tagId, + int threshold, + int locationId, + DateTime? editDate, + DateTime? lastScan, + DateTime? lastDiscovery, + Object calculation, + Object? custom_fields + ) { + this.Id = id; + this.Subnet = subnet; + this.Mask = mask; + this.SectionId = sectionId; + this.Description = description; + this.LinkedSubnet = linkedSubnet; + this.FirewallAddressObject = firewallAddressObject; + this.VrfId = vrfId; + this.MasterSubnetId = masterSubnetId; + this.AllowRequests = allowRequests; + this.VlanId = vlanId; + this.ShowName = showName; + this.DeviceId = deviceId; + this.Permissions = permissions; + this.PingSubnet = pingSubnet; + this.DiscoverSubnet = discoverSubnet; + this.ResolveDNS = resolveDNS; + this.DNSRecursive = dnsRecursive; + this.DNSRecords = dnsRecords; + this.NameserverId = nameserverId; + this.ScanAgent = scanAgent; + this.IsFolder = isFolder; + this.IsFull = isFull; + this.IsPool = isPool; + this.TagId = tagId; + this.Threshold = threshold; + this.LocationId = locationId; + this.EditDate = editDate; + this.LastScan = lastScan; + this.LastDiscovery = lastDiscovery; + this.Calculation = calculation; + this.ExtendedData = custom_fields; + } + + public string GetCIDR() { + return $"{this.Subnet}/{this.Mask}"; + } + + public override string ToString() + { + return this.GetCIDR(); + } +} \ No newline at end of file diff --git a/classlib/class/tag.cs b/classlib/class/tag.cs new file mode 100644 index 0000000..ddc94e5 --- /dev/null +++ b/classlib/class/tag.cs @@ -0,0 +1,45 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public class Tag { + public int Id { get; } + public string Type { get; } + public bool ShowTag { get; } + public string BGColor { get; } + public string FGColor { get; } + public bool Compress { get; } + public bool Locked { get; } + public bool UpdateTag { get; } + + public Tag( + int id, + string type, + bool showTag, + string BGColor, + string FGColor, + string compress, + string locked, + bool updateTag + ) { + this.Id = id; + this.Type = type; + this.ShowTag = showTag; + this.BGColor = BGColor; + this.FGColor = FGColor; + this.Compress = this.StringToBool(compress); + this.Locked = this.StringToBool(locked); + this.UpdateTag = updateTag; + } + + public override string ToString() { + return this.Type; + } + + private bool StringToBool(string str) { + if (str == "Yes") { + return true; + } + return false; + } +} \ No newline at end of file diff --git a/classlib/class/vlan.cs b/classlib/class/vlan.cs new file mode 100644 index 0000000..77ddb50 --- /dev/null +++ b/classlib/class/vlan.cs @@ -0,0 +1,39 @@ +namespace PS.IPAM; +using System; +using System.Dynamic; + +[Serializable] +public class Vlan : DynamicObject { + public int Id { get; } + public int DomainId { get; } + public string Name { get; } + public int Number { get; } + public string Description { get; } + public DateTime? EditDate { get; } + public int CustomerId { get; } + public Object? ExtendedData { get; } + public Vlan ( + int vlanId, + int domainId, + string name, + int number, + string description, + DateTime? editDate, + int customer_id, + Object? custom_fields + ) { + this.Id = vlanId; + this.DomainId = domainId; + this.Name = name; + this.Number = number; + this.Description = description; + this.EditDate = editDate; + this.CustomerId = customer_id; + this.ExtendedData = custom_fields; + } + + public override string ToString() + { + return $"{this.Number}"; + } +} \ No newline at end of file diff --git a/classlib/class/vrf.cs b/classlib/class/vrf.cs new file mode 100644 index 0000000..92c2af9 --- /dev/null +++ b/classlib/class/vrf.cs @@ -0,0 +1,35 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public class Vrf { + public int Id { get; } + public string Name { get; } + public string RouteDistinguisher { get; } + public string Description { get; } + public string Sections { get; } + public DateTime? EditDate { get; } + public Object? ExtendedData { get; } + + public Vrf( + int id, + string name, + string rd, + string description, + string sections, + DateTime? editDate, + Object? custom_fields + ) { + this.Id = id; + this.Name = name; + this.RouteDistinguisher = rd; + this.Description = description; + this.Sections = sections; + this.EditDate = editDate; + this.ExtendedData = custom_fields; + } + public override string ToString() + { + return this.Name; + } +} \ No newline at end of file diff --git a/classlib/classlib.csproj b/classlib/classlib.csproj new file mode 100644 index 0000000..b3c1ee9 --- /dev/null +++ b/classlib/classlib.csproj @@ -0,0 +1,10 @@ + + + + netstandard2.1 + enable + enable + latest + + + diff --git a/classlib/enum/authType.cs b/classlib/enum/authType.cs new file mode 100644 index 0000000..bb513c6 --- /dev/null +++ b/classlib/enum/authType.cs @@ -0,0 +1,8 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public enum AuthType { + credentials, + token +} \ No newline at end of file diff --git a/classlib/enum/controllers.cs b/classlib/enum/controllers.cs new file mode 100644 index 0000000..7d6c6a6 --- /dev/null +++ b/classlib/enum/controllers.cs @@ -0,0 +1,14 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public enum controllers { + user, + vlan, + subnets, + addresses, + sections, + vrf, + l2domains, + tools +} \ No newline at end of file diff --git a/classlib/enum/subcontrollers.cs b/classlib/enum/subcontrollers.cs new file mode 100644 index 0000000..a674d67 --- /dev/null +++ b/classlib/enum/subcontrollers.cs @@ -0,0 +1,16 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public enum subcontrollers { + nameservers, + tags, + devices, + device_types, + vlans, + vrfs, + scanagents, + locations, + nat, + racks +} \ No newline at end of file diff --git a/classlib/enum/types.cs b/classlib/enum/types.cs new file mode 100644 index 0000000..1dfaba1 --- /dev/null +++ b/classlib/enum/types.cs @@ -0,0 +1,13 @@ +namespace PS.IPAM; +using System; + +[Serializable] +public enum types { + Address, + Domain, + Section, + Subnetwork, + Tag, + Vlan, + Vrf +} \ No newline at end of file diff --git a/functions/private/Invoke-PSIPAMRequest.ps1 b/functions/private/Invoke-PSIPAMRequest.ps1 deleted file mode 100644 index 00040bf..0000000 --- a/functions/private/Invoke-PSIPAMRequest.ps1 +++ /dev/null @@ -1,64 +0,0 @@ -function Invoke-PSIPAMRequest { - [CmdletBinding()] - param ( - [parameter(Mandatory=$true)] - [ValidateSet("POST","GET","PATCH","DELETE")] - [string] - $Method, - [parameter(Mandatory=$true)] - [ValidateSet("user","vlan","subnets","addresses","sections","vrf","l2domains","tools")] - [string] - $Controller, - [parameter(Mandatory=$false)] - [ValidateSet("nameservers")] - [string] - $SubController, - [Parameter(Mandatory=$false)] - [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] - $Params, - [Parameter(Mandatory=$false)] - [array] - $Identifiers - ) - $_tokenStatus = Test-PSIPAMSession - - if ($_tokenStatus -eq "NoToken") { throw "No session available!" } - if ($_tokenStatus -eq "Expired") { Update-PSIPAMSession } - - $Controller = $Controller.ToLower() - - $_uri = "$($script:ipamURL)/api/$($script:ipamAppID)/$Controller" - if ($SubController) { $_uri += "/$SubController" } - if ($Identifiers) { $_uri += "/$($Identifiers -join '/')/" } - - $_headers = @{ - "Accept" = "application/json" - "Content-Type" = "application/json" - "token" = $script:ipamToken - } - - $_arguments = @{ - Method = $Method - Uri = $_uri - Headers = $_headers - } - - if ($Method -match "POST|PATCH") { - if ($Params -is [PSCustomObject]) { - $_params = @{}; - $Params | Get-Member -MemberType *Property | Where-Object { - $_params.($_.name) = $Params.($_.name) - } - } else { $_params = $Params } - - $_arguments.Add("Body",($_params | ConvertTo-Json)) - } - - $_response = Invoke-RestMethod @_arguments - - if ($_response.code -match "20\d") { - return $_response.data - } else { - throw ("Error - $($_response.code)") - } -} \ No newline at end of file diff --git a/functions/private/Invoke-Request.ps1 b/functions/private/Invoke-Request.ps1 new file mode 100644 index 0000000..2a81e66 --- /dev/null +++ b/functions/private/Invoke-Request.ps1 @@ -0,0 +1,101 @@ +function Invoke-Request { + [CmdletBinding()] + param ( + [parameter(Mandatory=$true)] + [ValidateSet("GET","POST","PATCH","DELETE")] + [string] + $Method, + [parameter(Mandatory=$true)] + [PS.IPAM.controllers] + $Controller, + [parameter(Mandatory=$false)] + [PS.IPAM.types] + $Type, + [parameter(Mandatory=$false)] + [PS.IPAM.subcontrollers] + $SubController, + [Parameter(Mandatory=$false)] + [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] + $Params, + [Parameter(Mandatory=$false)] + [array] + $Identifiers + ) + $_tokenStatus = Test-Session + + if ($_tokenStatus -eq "NoToken") { throw "No session available!" } + if ($_tokenStatus -eq "Expired") { Update-Session } + + $Controller = $Controller + + $_uri = "$($script:psipamSession.URL)/api/$($script:psipamSession.AppID)/$Controller" + if ($SubController) { $_uri += "/$SubController" } + if ($Identifiers) { $_uri += "/$($Identifiers -join '/')/" } + + $_headers = @{ + "Accept" = "application/json" + "Content-Type" = "application/json" + } + switch ($script:psipamSession.AuthType) { + "Credentials" { $_headers.Add("token", $script:psipamSession.Token) } + "Token" { $_headers.Add("phpipam-token", $script:psipamSession.Token) } + } + + $_arguments = @{ + Method = $Method + Uri = $_uri + Headers = $_headers + } + + if ($Method -eq "POST" -or $Method -eq "PATCH") { + if ($Params -is [PSCustomObject]) { + $_params = @{}; + $Params | Get-Member -MemberType *Property | Where-Object { + $_params.($_.name) = $Params.($_.name) + } + } else { $_params = $Params } + + $_arguments.Add("Body",($_params | ConvertTo-Json)) + } + + Write-Verbose -Message "Invoking web request to $($_uri), with method $($_arguments.Method), headers: $($_arguments.Headers)" + + $_response = Invoke-RestMethod @_arguments + + if ($_response.code -match "20\d") { + if ($Type -is [PS.IPAM.types]) { + switch ($Type) { + "address" { + $_paramList = @("id","subnetId","ip","is_gateway","description","hostname","mac","owner","tag","deviceId","location","port","note","lastSeen","excludePing","PTRignore","PTR","firewallAddressObject","editDate","customer_id") + + $_response.data | ForEach-Object { + New-Object -TypeName ([PS.IPAM.Address]) -ArgumentList (@(($_ | Select-Object $_paramList).psobject.properties.value) + ($_ | Select-Object -Property custom_* -ExcludeProperty 'custom_\*')) + }; break + } + "vlan" { + $_paramList = @("vlanId","domainId","name","number","description","editDate","customer_id","custom_fields") + + $_response.data | ForEach-Object { + New-Object -TypeName ([PS.IPAM.Vlan]) -ArgumentList (@(($_ | Select-Object $_paramList).psobject.properties.value) + ($_ | 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 + } + "vrf" { + $_response.data | ForEach-Object { + New-Object -TypeName ([PS.IPAM.Vrf]) -ArgumentList (@($_.psobject.properties.value[0..5]) + ($_ | Select-Object -Property custom_* -ExcludeProperty 'custom_\*')) + }; break + } + Default { $_response.data | ForEach-Object { New-Object -TypeName ("PS.IPAM.$Type") -ArgumentList $_.psobject.properties.value } } + } + + } else { + return $_response.data + } + } else { + throw ("Error - $($_response.code)") + } +} \ No newline at end of file diff --git a/functions/private/Test-PSIPAMSession.ps1 b/functions/private/Test-PSIPAMSession.ps1 deleted file mode 100644 index 4c4cf77..0000000 --- a/functions/private/Test-PSIPAMSession.ps1 +++ /dev/null @@ -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" - } -} \ No newline at end of file diff --git a/functions/private/Test-Session.ps1 b/functions/private/Test-Session.ps1 new file mode 100644 index 0000000..1558a55 --- /dev/null +++ b/functions/private/Test-Session.ps1 @@ -0,0 +1,19 @@ +function Test-Session { + [CmdletBinding()] + param ( + + ) + if ($script:psipamSession) { + if ($null -eq $script:psipamSession.Expires) { + return "Valid" + } else { + if ($script:psipamSession.Expires -lt (Get-Date)) { + return "Expired" + } else { + return "Valid" + } + } + } else { + return "NoToken" + } +} \ No newline at end of file diff --git a/functions/private/Update-PSIPAMSession.ps1 b/functions/private/Update-PSIPAMSession.ps1 deleted file mode 100644 index d8ade10..0000000 --- a/functions/private/Update-PSIPAMSession.ps1 +++ /dev/null @@ -1,18 +0,0 @@ -function Update-PSIPAMSession { - [CmdletBinding()] - param ( - [switch] - $Force - ) - $_tokenStatus = Test-PSIPAMSession - if ($_tokenStatus -eq "NoToken") { - throw "No session available!" - } - if ($_tokenStatus -eq "Valid") { - return (Invoke-PSIPAMRequest -Method PATCH -Controller user).expires - } - if ($_tokenStatus -eq "Expired" -or $Force) { - New-PSIPAMSession -URL $script:ipamURL -AppID $script:ipamAppID -Credentials $script:ipamCredentials - return $script:ipamExpires - } -} \ No newline at end of file diff --git a/functions/private/Update-Session.ps1 b/functions/private/Update-Session.ps1 new file mode 100644 index 0000000..245e35b --- /dev/null +++ b/functions/private/Update-Session.ps1 @@ -0,0 +1,18 @@ +function Update-Session { + [CmdletBinding()] + param ( + [switch] + $Force + ) + $_tokenStatus = Test-Session + if ($_tokenStatus -eq "NoToken") { + throw "No session available!" + } + if ($_tokenStatus -eq "Valid") { + return (Invoke-Request -Method [ps.ipam.methods]::PATCH -Controller [ps.ipam.controllers]::user).expires + } + if ($_tokenStatus -eq "Expired" -or $Force) { + New-Session -URL $script:psipamSession.URL -AppID $script:psipamSession.AppID -Credentials $script:psipamSession.Credentials + return + } +} \ No newline at end of file diff --git a/functions/public/Assign-Tag.ps1 b/functions/public/Assign-Tag.ps1 new file mode 100644 index 0000000..7b609ab --- /dev/null +++ b/functions/public/Assign-Tag.ps1 @@ -0,0 +1,33 @@ +function Assign-Tag { + [CmdletBinding()] + param ( + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Address] + $AddressObject, + [parameter(Mandatory=$true,Position=1)] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Tag] + $Tag + ) + process { + $_params = @{ + Controller = [PS.IPAM.controllers]::addresses + Method = "PATCH" + } + $_id = $AddressObject.id + $_tagid = $Tag.id + + $_identifiers = @($_id) + + $_params.Add("Identifiers",$_identifiers) + + $_body = @{ } + $_body.Add("tag", $_tagid) + + $_params.Add("Params",$_body) + + Invoke-Request @_params + } +} +Export-ModuleMember -Function Assign-Tag \ No newline at end of file diff --git a/functions/public/Get-Address.ps1 b/functions/public/Get-Address.ps1 new file mode 100644 index 0000000..ecaf22e --- /dev/null +++ b/functions/public/Get-Address.ps1 @@ -0,0 +1,66 @@ +function Get-Address { + [CmdletBinding(DefaultParameterSetName="ByID")] + [OutputType([PS.IPAM.address])] + param ( + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] + [ValidateNotNullOrEmpty()] + [int] + $Id, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByIP")] + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1,ParameterSetName="BySubnetId")] + [ValidateNotNullOrEmpty()] + [ipaddress] + $IP, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByHostName")] + [ValidateNotNullOrEmpty()] + [string] + $HostName, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByTag")] + [ValidateNotNullOrEmpty()] + [int] + $TagId, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetId")] + [ValidateNotNullOrEmpty()] + [int] + $SubnetId, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetCIDR")] + [ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})] + [ValidateNotNullOrEmpty()] + [string] + $SubnetCIDR + ) + process { + $_params = @{ + Controller = [PS.IPAM.controllers]::addresses + Method = "GET" + Type = [PS.IPAM.types]::address + } + switch ($PSCmdlet.ParameterSetName) { + "ByID" { $_identifiers = @($id); break } + "ByIP" { $_identifiers = ("search",$IP); break } + "ByHostName" { $_identifiers = ("search_hostname",$HostName); break } + "ByTag" { $_identifiers = ("tags",$TagId,[PS.IPAM.controllers]::addresses); break } + "BySubnetId" { + if ($IP) { + $_identifiers = ($IP,$SubnetId) + } else { + $_params.Item("Controller") = [PS.IPAM.controllers]::subnets + $_identifiers = ($SubnetId,[PS.IPAM.controllers]::addresses) + } + break + } + "BySubnetCIDR" { + $_params.Item("Controller") = [PS.IPAM.controllers]::subnets + $_subnetId = (Get-Subnet -CIDR $SubnetCIDR).id + if (!$_subnetId) { throw "Cannot find subnet!" } + + $_identifiers = ($_subnetId,[PS.IPAM.controllers]::addresses) + break + } + } + $_params.Add("Identifiers",$_identifiers) + + Invoke-Request @_params + } +} +Export-ModuleMember -Function Get-Address \ No newline at end of file diff --git a/functions/public/Get-PSIPAMFirstFreeIP.ps1 b/functions/public/Get-FirstFreeIP.ps1 similarity index 57% rename from functions/public/Get-PSIPAMFirstFreeIP.ps1 rename to functions/public/Get-FirstFreeIP.ps1 index 47215d2..daba78f 100644 --- a/functions/public/Get-PSIPAMFirstFreeIP.ps1 +++ b/functions/public/Get-FirstFreeIP.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAMFirstFreeIP { +function Get-FirstFreeIP { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByCIDR")] @@ -7,27 +7,32 @@ function Get-PSIPAMFirstFreeIP { [string] $CIDR, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] - $Id + [int] + $Id, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Subnetwork] + $SubnetObject ) process { $_params = @{ - Controller = "subnets" + Controller = [PS.IPAM.controllers]::subnets Method = "GET" } switch ($PSCmdlet.ParameterSetName) { - "ByID" { $_subnetId = $Id } + "ByID" { $_subnetId = $Id; break } "ByCIDR" { - $_subnetId = (Get-PSIPAMSubnet -CIDR $CIDR).id + $_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) - return Invoke-PSIPAMRequest @_params | Select-Object @{n="Ip";e={$_}} + return Invoke-Request @_params | Select-Object @{n="Ip";e={$_}} } } -Export-ModuleMember -Function Get-PSIPAMFirstFreeIP \ No newline at end of file +Export-ModuleMember -Function Get-FirstFreeIP \ No newline at end of file diff --git a/functions/public/Get-L2Domain.ps1 b/functions/public/Get-L2Domain.ps1 new file mode 100644 index 0000000..d97694b --- /dev/null +++ b/functions/public/Get-L2Domain.ps1 @@ -0,0 +1,23 @@ +function Get-L2Domain { + [CmdletBinding(DefaultParameterSetName="ByID")] + [OutputType([PS.IPAM.Domain])] + param ( + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] + [ValidateNotNullOrEmpty()] + [int] + $Id + ) + process { + $_params = @{ + Controller = [PS.IPAM.controllers]::l2domains + Method = "GET" + Type = [PS.IPAM.types]::Domain + } + + $_identifiers = @($Id) + $_params.Add("Identifiers",$_identifiers) + + Invoke-Request @_params + } +} +Export-ModuleMember -Function Get-L2Domain \ No newline at end of file diff --git a/functions/public/Get-PSIPAMNameserver.ps1 b/functions/public/Get-Nameserver.ps1 similarity index 66% rename from functions/public/Get-PSIPAMNameserver.ps1 rename to functions/public/Get-Nameserver.ps1 index 8c6fa3b..06fe325 100644 --- a/functions/public/Get-PSIPAMNameserver.ps1 +++ b/functions/public/Get-Nameserver.ps1 @@ -1,10 +1,9 @@ -function Get-PSIPAMNameserver { +function Get-Nameserver { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id ) process { @@ -12,18 +11,18 @@ function Get-PSIPAMNameserver { $visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList) $_params = @{ - Controller = "tools" - SubController = "nameservers" + Controller = [PS.IPAM.controllers]::tools + SubController = [PS.IPAM.subcontrollers]::nameservers Method = "GET" } switch ($PSCmdlet.ParameterSetName) { - "ByID" { $_nameserverId = $Id } + "ByID" { $_nameserverId = $Id; break } } $_identifiers = @($_nameserverId) $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | Select-Object @{n="address";e={$_.namesrv1}},* | Select-Object -ExcludeProperty namesrv1 | ` + Invoke-Request @_params | Select-Object @{n="address";e={$_.namesrv1}},* | Select-Object -ExcludeProperty namesrv1 | ` Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru } } -Export-ModuleMember -Function Get-PSIPAMNameserver \ No newline at end of file +Export-ModuleMember -Function Get-Nameserver \ No newline at end of file diff --git a/functions/public/Get-PSIPAML2Domain.ps1 b/functions/public/Get-PSIPAML2Domain.ps1 deleted file mode 100644 index 2a20537..0000000 --- a/functions/public/Get-PSIPAML2Domain.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -function Get-PSIPAML2Domain { - [CmdletBinding(DefaultParameterSetName="ByID")] - param ( - [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $Id - ) - process { - [string[]]$visiblePropertiesList = @('Id','Name','Description') - $visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList) - - $_params = @{ - Controller = "l2domains" - Method = "GET" - } - $_identifiers = @($Id) - - $_params.Add("Identifiers",$_identifiers) - - Invoke-PSIPAMRequest @_params | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru - } -} -Export-ModuleMember -Function Get-PSIPAML2Domain \ No newline at end of file diff --git a/functions/public/Get-PSIPAMSection.ps1 b/functions/public/Get-PSIPAMSection.ps1 deleted file mode 100644 index 0894b1d..0000000 --- a/functions/public/Get-PSIPAMSection.ps1 +++ /dev/null @@ -1,32 +0,0 @@ -function Get-PSIPAMSection { - [CmdletBinding(DefaultParameterSetName="ByID")] - param ( - [parameter(Mandatory=$false,ValueFromPipeline=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $Id, - [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,ParameterSetName="ByName")] - [ValidateNotNullOrEmpty()] - [string] - $Name - ) - process { - [string[]]$visiblePropertiesList = @('Id','Name','Description') - $visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList) - - $_params = @{ - Controller = "sections" - Method = "GET" - } - switch ($PSCmdlet.ParameterSetName) { - "ByID" { $_identifiers = @($Id) } - "ByName" { $_identifiers = @($Name) } - } - $_params.Add("Identifiers",$_identifiers) - - Invoke-PSIPAMRequest @_params | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru - } -} -Export-ModuleMember Get-PSIPAMSection \ No newline at end of file diff --git a/functions/public/Get-PSIPAMTags.ps1 b/functions/public/Get-PSIPAMTags.ps1 deleted file mode 100644 index cd8f0ec..0000000 --- a/functions/public/Get-PSIPAMTags.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -function Get-PSIPAMTags { - [CmdletBinding()] - param ( - [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $Id - ) - process { - [string[]]$visiblePropertiesList = @('Id','Name') - $visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList) - - $_params = @{ - Controller = "addresses" - Method = "GET" - } - $_identifiers = @("tags") - if ($Id) { $_identifiers += $Id } - $_params.Add("Identifiers",$_identifiers) - - Invoke-PSIPAMRequest @_params | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru - } -} -Export-ModuleMember -Function Get-PSIPAMTags \ No newline at end of file diff --git a/functions/public/Get-PSIPAMVlan.ps1 b/functions/public/Get-PSIPAMVlan.ps1 deleted file mode 100644 index 60634b2..0000000 --- a/functions/public/Get-PSIPAMVlan.ps1 +++ /dev/null @@ -1,45 +0,0 @@ -function Get-PSIPAMVlan { - [CmdletBinding(DefaultParameterSetName="ByID")] - param ( - [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $Id, - [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByNumber")] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $Number, - [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByL2Domain")] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $L2DomainId - ) - process { - [string[]]$visiblePropertiesList = @('Id','Name','Description') - $visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList) - - $_params = @{ - Controller = "vlan" - Method = "GET" - } - switch ($PSCmdlet.ParameterSetName) { - "ByID" { $_identifiers = @($Id) } - "ByNumber" { $_identifiers = @("search",$Number) } - "ByL2Domain"{ - $_params.Item("Controller") = "l2domains" - - $_l2domainId = $L2DomainId - - $_identifiers = @($_l2domainId,"vlans") - } - } - $_params.Add("Identifiers",$_identifiers) - - Invoke-PSIPAMRequest @_params | Select-Object @{n="id";e={$_.vlanId}},* | Select-Object -ExcludeProperty vlanId | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru - } -} -Export-ModuleMember -Function Get-PSIPAMVlan \ No newline at end of file diff --git a/functions/public/Get-PSIPAMVrf.ps1 b/functions/public/Get-PSIPAMVrf.ps1 deleted file mode 100644 index 6846a4c..0000000 --- a/functions/public/Get-PSIPAMVrf.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -function Get-PSIPAMVrf { - [CmdletBinding(DefaultParameterSetName="ByID")] - param ( - [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $Id - ) - process { - [string[]]$visiblePropertiesList = @('Id','Name','Description') - $visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList) - - $_params = @{ - Controller = "vrf" - Method = "GET" - } - if ($Id) { $_identifiers = @($Id) } - - $_params.Add("Identifiers",$_identifiers) - - Invoke-PSIPAMRequest @_params | Select-Object @{n="id";e={$_.vrfId}},* | Select-Object -ExcludeProperty vrfId | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru - } -} -Export-ModuleMember -Function Get-PSIPAMVrf \ No newline at end of file diff --git a/functions/public/Get-PSIPAMAddress.ps1 b/functions/public/Get-Permissions.ps1 similarity index 87% rename from functions/public/Get-PSIPAMAddress.ps1 rename to functions/public/Get-Permissions.ps1 index 252536c..2cdc0a4 100644 --- a/functions/public/Get-PSIPAMAddress.ps1 +++ b/functions/public/Get-Permissions.ps1 @@ -1,5 +1,5 @@ -function Get-PSIPAMAddress { - [CmdletBinding(DefaultParameterSetName="ByID")] +function Get-Permissions { + [CmdletBinding()] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] [ValidateScript({ $_ -match "^\d+$" })] @@ -41,10 +41,10 @@ function Get-PSIPAMAddress { Method = "GET" } switch ($PSCmdlet.ParameterSetName) { - "ByID" { $_identifiers = @($id) } - "ByIP" { $_identifiers = ("search",$IP) } - "ByHostName" { $_identifiers = ("search_hostname",$HostName) } - "ByTag" { $_identifiers = ("tags",$TagId,"addresses") } + "ByID" { $_identifiers = @($id); break } + "ByIP" { $_identifiers = ("search",$IP); break } + "ByHostName" { $_identifiers = ("search_hostname",$HostName); break } + "ByTag" { $_identifiers = ("tags",$TagId,"addresses"); break } "BySubnetId" { if ($IP) { $_identifiers = ($IP,$SubnetId) @@ -52,19 +52,21 @@ function Get-PSIPAMAddress { $_params.Item("Controller") = "subnets" $_identifiers = ($SubnetId,"addresses") } + break } "BySubnetCIDR" { $_params.Item("Controller") = "subnets" - $_subnetId = (Get-PSIPAMSubnet -CIDR $SubnetCIDR).id + $_subnetId = (Get-Subnet -CIDR $SubnetCIDR).id if (!$_subnetId) { throw "Cannot find subnet!" } $_identifiers = ($_subnetId,"addresses") + break } } $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | ` + Invoke-Request @_params | ` Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru } } -Export-ModuleMember -Function Get-PSIPAMAddress \ No newline at end of file +Export-ModuleMember -Function Get-Permissions \ No newline at end of file diff --git a/functions/public/Get-Section.ps1 b/functions/public/Get-Section.ps1 new file mode 100644 index 0000000..16ae03c --- /dev/null +++ b/functions/public/Get-Section.ps1 @@ -0,0 +1,29 @@ +function Get-Section { + [CmdletBinding(DefaultParameterSetName="NoParams")] + [OutputType([PS.IPAM.Section])] + param ( + [parameter(Mandatory=$false,ValueFromPipeline=$true,Position=0,ParameterSetName="ByID")] + [ValidateNotNullOrEmpty()] + [int] + $Id, + [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,ParameterSetName="ByName")] + [ValidateNotNullOrEmpty()] + [string] + $Name + ) + process { + $_params = @{ + Controller = [PS.IPAM.controllers]::sections + Method = "GET" + Type = [PS.IPAM.types]::Section + } + switch ($PSCmdlet.ParameterSetName) { + "ByID" { $_identifiers = @($Id); break } + "ByName" { $_identifiers = @($Name); break } + } + $_params.Add("Identifiers",$_identifiers) + + Invoke-Request @_params + } +} +Export-ModuleMember Get-Section \ No newline at end of file diff --git a/functions/public/Get-PSIPAMSubnet.ps1 b/functions/public/Get-Subnet.ps1 similarity index 75% rename from functions/public/Get-PSIPAMSubnet.ps1 rename to functions/public/Get-Subnet.ps1 index 0d22635..814f3af 100644 --- a/functions/public/Get-PSIPAMSubnet.ps1 +++ b/functions/public/Get-Subnet.ps1 @@ -1,5 +1,6 @@ -function Get-PSIPAMSubnet { - [CmdletBinding(DefaultParameterSetName="ByID")] +function Get-Subnet { + [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}"})] @@ -7,16 +8,14 @@ function Get-PSIPAMSubnet { [string] $CIDR, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $Id, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySectionId")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="ByVlanNumber")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1,ParameterSetName="ByVlanId")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $SectionId, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySectionName")] [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=3,ParameterSetName="ByVlanNumber")] @@ -25,25 +24,21 @@ function Get-PSIPAMSubnet { [string] $SectionName, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByVrfId")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $VrfId, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByVlanId")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $VlanId, [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByVlanNumber")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $VlanNumber, [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1,ParameterSetName="ByVlanNumber")] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] - $VlanDomain, + [int] + $VlanDomainId, [parameter(Mandatory=$false,ParameterSetName="ByID")] [switch] $Slaves, @@ -52,16 +47,14 @@ function Get-PSIPAMSubnet { $Recurse ) process { - [string[]]$visiblePropertiesList = @('Id','Subnet','Mask','Description') - $visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList) - $_params = @{ - Controller = "subnets" + Controller = [PS.IPAM.controllers]::subnets Method = "GET" + Type = [PS.IPAM.types]::Subnetwork } switch ($PSCmdlet.ParameterSetName) { "ByCIDR" { - $_identifiers = @("cidr",$CIDR) + $_identifiers = @("cidr",$CIDR); break } "ByID" { $_identifiers = @($Id) @@ -73,42 +66,47 @@ function Get-PSIPAMSubnet { $_identifiers += "slaves" } } + break } "BySectionId" { $_params.Item("Controller") = "sections" $_sectionId = $SectionId - $_identifiers = @($_sectionId,"subnets") + $_identifiers = @($_sectionId,"subnets") + break } "BySectionName" { $_params.Item("Controller") = "sections" - $_sectionId = (Get-PSIPAMSection -Name $SectionName).id + $_sectionId = (Get-Section -Name $SectionName).id if (!$_sectionId) { throw "Cannot find section!" } - $_identifiers = @($_sectionId,"subnets") + $_identifiers = @($_sectionId,"subnets") + break } "ByVrfId" { $_params.Item("Controller") = "vrf" $_vrfId = $VrfId $_identifiers = @($_vrfId,"subnets") + break } "ByVlanId" { $_params.Item("Controller") = "vlan" $_vlanId = $VlanId if ($SectionId) { $_sectionId = $SectionId } - if ($SectionName){ $_sectionId = (Get-PSIPAMSection -Name $SectionName).id } + if ($SectionName){ $_sectionId = (Get-Section -Name $SectionName).id } $_identifiers = @($_vlanId,"subnets") if ($_sectionId) { $_identifiers += $_sectionId } + break } "ByVlanNumber" { $_params.Item("Controller") = "vlan" - $_vlans = Get-PSIPAMVlan -Number $VlanNumber + $_vlans = Get-Vlan -Number $VlanNumber if ($VlanDomain) { $_vlans = $_vlans | Where-Object {$_.domainId -eq $VlanDomain} } if ($SectionId) { $_sectionId = $SectionId } - if ($SectionName){ $_sectionId = (Get-PSIPAMSection -Name $SectionName).id } + if ($SectionName){ $_sectionId = (Get-Section -Name $SectionName).id } $_vlanId = $_vlans.vlanId @@ -118,12 +116,12 @@ function Get-PSIPAMSubnet { $_identifiers = @($_vlanId,"subnets") if ($_sectionId) { $_identifiers += $_sectionId } + break } } $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru + Invoke-Request @_params } } -Export-ModuleMember Get-PSIPAMSubnet \ No newline at end of file +Export-ModuleMember Get-Subnet \ No newline at end of file diff --git a/functions/public/Get-PSIPAMSubnetUsage.ps1 b/functions/public/Get-SubnetUsage.ps1 similarity index 80% rename from functions/public/Get-PSIPAMSubnetUsage.ps1 rename to functions/public/Get-SubnetUsage.ps1 index 7b8b550..731de87 100644 --- a/functions/public/Get-PSIPAMSubnetUsage.ps1 +++ b/functions/public/Get-SubnetUsage.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAMSubnetUsage { +function Get-SubnetUsage { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByCIDR")] @@ -19,15 +19,16 @@ function Get-PSIPAMSubnetUsage { } switch ($PSCmdlet.ParameterSetName) { "ByCIDR" { - $_subnetId = (Get-PSIPAMSubnet -CIDR $CIDR).id + $_subnetId = (Get-Subnet -CIDR $CIDR).id if (!$_subnetId) { throw "Cannot find subnet!" } + break } - "ByID" { $_subnetId = $Id } + "ByID" { $_subnetId = $Id; break } } $_identifiers = @($_subnetId,"usage") $_params.Add("Identifiers",$_identifiers) - return Invoke-PSIPAMRequest @_params + return Invoke-Request @_params } } -Export-ModuleMember -Function Get-PSIPAMSubnetUsage \ No newline at end of file +Export-ModuleMember -Function Get-SubnetUsage \ No newline at end of file diff --git a/functions/public/Get-Tag.ps1 b/functions/public/Get-Tag.ps1 new file mode 100644 index 0000000..31fa28f --- /dev/null +++ b/functions/public/Get-Tag.ps1 @@ -0,0 +1,49 @@ +function Get-Tag { + [CmdletBinding(DefaultParameterSetName="NoParams")] + [OutputType([PS.IPAM.Tag])] + param ( + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] + [ValidateNotNullOrEmpty()] + [int] + $Id, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Address] + $AddressObject, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Subnetwork] + $SubnetObject + ) + process { + $_params = @{ + Controller = [PS.IPAM.controllers]::addresses + Method = "GET" + Type = [PS.IPAM.types]::tag + } + $_identifiers = @("tags") + switch ($PSCmdlet.ParameterSetName) { + "ByID" { $_identifiers += $Id; break } + "ByAddressObject" { + if ($AddressObject.TagId) { + $_identifiers += $AddressObject.TagId + } else { + return $null + } + break + } + "BySubnetObject" { + if ($SubnetObject.TagId) { + $_identifiers += $SubnetObject.TagId + } else { + return $null + } + break + } + } + $_params.Add("Identifiers",$_identifiers) + + Invoke-Request @_params + } +} +Export-ModuleMember -Function Get-Tag \ No newline at end of file diff --git a/functions/public/Get-Vlan.ps1 b/functions/public/Get-Vlan.ps1 new file mode 100644 index 0000000..a6aa22f --- /dev/null +++ b/functions/public/Get-Vlan.ps1 @@ -0,0 +1,59 @@ +function Get-Vlan { + [CmdletBinding(DefaultParameterSetName="ByID")] + [OutputType([PS.IPAM.Vlan])] + param ( + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] + [ValidateNotNullOrEmpty()] + [int] + $Id, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByNumber")] + [ValidateNotNullOrEmpty()] + [int] + $Number, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByL2Domain")] + [ValidateNotNullOrEmpty()] + [int] + $L2DomainId, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Subnetwork] + $SubnetObject, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByDomainObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Domain] + $DomainObject + ) + process { + $_params = @{ + Controller = [PS.IPAM.controllers]::vlan + Method = "GET" + Type = [PS.IPAM.types]::Vlan + } + switch ($PSCmdlet.ParameterSetName) { + "ByID" { $_identifiers = @($Id); break } + "ByNumber" { $_identifiers = @("search",$Number); break } + "ByL2Domain"{ + $_params.Item("Controller") = [PS.IPAM.controllers]::l2domains + + $_l2domainId = $L2DomainId + + $_identifiers = @($_l2domainId,[PS.IPAM.subcontrollers]::vlans) + break + } + "BySubnetObject" { + if ($SubnetObject.VlanId) { + $_identifiers = @($SubnetObject.VlanId); break + } else { + return $null + } + } + "DomainObject" { + Get-Vlan -L2DomainId $DomainObject.Id + } + } + $_params.Add("Identifiers",$_identifiers) + + Invoke-Request @_params + } +} +Export-ModuleMember -Function Get-Vlan \ No newline at end of file diff --git a/functions/public/Get-Vrf.ps1 b/functions/public/Get-Vrf.ps1 new file mode 100644 index 0000000..9ff2c78 --- /dev/null +++ b/functions/public/Get-Vrf.ps1 @@ -0,0 +1,23 @@ +function Get-Vrf { + [CmdletBinding(DefaultParameterSetName="ByID")] + [OutputType([PS.IPAM.Vrf])] + param ( + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] + [ValidateNotNullOrEmpty()] + [int] + $Id + ) + process { + $_params = @{ + Controller = [PS.IPAM.controllers]::vrf + Method = "GET" + Type = [PS.IPAM.types]::vrf + } + if ($Id) { $_identifiers = @($Id) } + + $_params.Add("Identifiers",$_identifiers) + + Invoke-Request @_params + } +} +Export-ModuleMember -Function Get-Vrf \ No newline at end of file diff --git a/functions/public/New-PSIPAMAddress.ps1 b/functions/public/New-Address.ps1 similarity index 92% rename from functions/public/New-PSIPAMAddress.ps1 rename to functions/public/New-Address.ps1 index 0a70bfa..36069af 100644 --- a/functions/public/New-PSIPAMAddress.ps1 +++ b/functions/public/New-Address.ps1 @@ -1,4 +1,4 @@ -function New-PSIPAMAddress { +function New-Address { [CmdletBinding()] param ( [parameter( @@ -7,9 +7,8 @@ function New-PSIPAMAddress { ValueFromPipelineByPropertyName=$true, HelpMessage="Id of subnet address belongs to", Position=0)] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $SubnetId, [parameter( Mandatory=$true, @@ -72,9 +71,8 @@ function New-PSIPAMAddress { ValueFromPipelineByPropertyName=$true, HelpMessage="Id of subnet address belongs to", Position=7)] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $TagId, [parameter( Mandatory=$false, @@ -90,9 +88,8 @@ function New-PSIPAMAddress { ValueFromPipelineByPropertyName=$true, HelpMessage="Id of PowerDNS PTR record", Position=7)] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $PTRId, [parameter( Mandatory=$false, @@ -117,9 +114,8 @@ function New-PSIPAMAddress { ValueFromPipelineByPropertyName=$true, HelpMessage="Id of device address belongs to", Position=12)] - [ValidateScript({ $_ -match "^\d+$" })] [ValidateNotNullOrEmpty()] - [string] + [int] $DeviceId, [parameter( Mandatory=$false, @@ -137,7 +133,7 @@ function New-PSIPAMAddress { ) process { $_params = @{ - Controller = "addresses" + Controller = [PS.IPAM.controllers]::addresses Method = "POST" } @@ -172,11 +168,11 @@ function New-PSIPAMAddress { $_params.Add("Params",$_body) try { - Invoke-PSIPAMRequest @_params + Invoke-Request @_params } finally { - Get-PSIPAMAddress -SubnetId $SubnetId -Ip $Ip + Get-Address -SubnetId $SubnetId -Ip $Ip } } } -Export-ModuleMember -Function New-PSIPAMAddress \ No newline at end of file +Export-ModuleMember -Function New-Address \ No newline at end of file diff --git a/functions/public/New-PSIPAMFirstFreeIP.ps1 b/functions/public/New-FirstFreeIP.ps1 similarity index 96% rename from functions/public/New-PSIPAMFirstFreeIP.ps1 rename to functions/public/New-FirstFreeIP.ps1 index fe377dc..39c2b8b 100644 --- a/functions/public/New-PSIPAMFirstFreeIP.ps1 +++ b/functions/public/New-FirstFreeIP.ps1 @@ -1,4 +1,4 @@ -function New-PSIPAMFirstFreeIP { +function New-FirstFreeIP { [CmdletBinding()] param ( [parameter( @@ -163,10 +163,10 @@ function New-PSIPAMFirstFreeIP { $_params.Add("Params",$_body) - $_result = Invoke-PSIPAMRequest @_params + $_result = Invoke-Request @_params if ($_result) { - Get-PSIPAMAddress -SubnetId $SubnetId -IP $_result + Get-Address -SubnetId $SubnetId -IP $_result } } } -Export-ModuleMember -Function New-PSIPAMFirstFreeIP \ No newline at end of file +Export-ModuleMember -Function New-FirstFreeIP \ No newline at end of file diff --git a/functions/public/New-PSIPAMSession.ps1 b/functions/public/New-PSIPAMSession.ps1 deleted file mode 100644 index 119a59e..0000000 --- a/functions/public/New-PSIPAMSession.ps1 +++ /dev/null @@ -1,37 +0,0 @@ -function New-PSIPAMSession { - [CmdletBinding()] - param ( - [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] - [ValidateNotNullOrEmpty()] - [string]$URL, - [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1)] - [ValidateNotNullOrEmpty()] - [string]$AppID, - [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2)] - [ValidateNotNullOrEmpty()] - [pscredential]$Credentials - ) - $_bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credentials.Password) - $_password = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($_bstr) - $_uri = "$URL/api/$AppID/user" - $_auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($Credentials.UserName):$_password")) - $_headers = @{ - "Accept" = "application/json" - "Content-Type" = "application/json" - "Authorization" = "Basic $_auth" - } - - $_response = Invoke-RestMethod -Method Post -Uri $_uri -Headers $_headers -ErrorAction SilentlyContinue - - if ($_response.success -eq $true) { - $script:ipamAuth = $true - $script:ipamToken = $_response.data.token - $script:ipamAppID = $AppID - $script:ipamURL = $URL - $script:ipamCredentials = $Credentials - $script:ipamExpires = Get-Date $_response.data.expires - } else { - $_response.error - } -} -Export-ModuleMember -Function New-PSIPAMSession \ No newline at end of file diff --git a/functions/public/New-PSIPAMSubnet.ps1 b/functions/public/New-PSIPAMSubnet.ps1 deleted file mode 100644 index d655d34..0000000 --- a/functions/public/New-PSIPAMSubnet.ps1 +++ /dev/null @@ -1,202 +0,0 @@ -function New-PSIPAMSubnet { - [CmdletBinding()] - param ( - [parameter( - Mandatory=$true, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="CIDR of subnet in dotted format (e.g 10.10.10.0/24)", - Position=0)] - [ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})] - [ValidateNotNullOrEmpty()] - [string] - $CIDR, - [parameter( - Mandatory=$true, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Section identifier", - Position=1)] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $SectionId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Subnet description", - Position=2)] - [string] - $Description, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Assigns subnet to VLAN", - Position=3)] - [ValidateScript({ $_ -match "^\d+$" })] - [string] - $VlanId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Assigns subnet to VRF", - Position=4)] - [ValidateScript({ $_ -match "^\d+$" })] - [string] - $VrfId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Master subnet id for nested subnet", - Position=5)] - [ValidateScript({ $_ -match "^\d+$" })] - [string] - $MasterSubnetId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Id of nameserver to attach to subnet", - Position=6)] - [ValidateScript({ $_ -match "^\d+$" })] - [string] - $NameserverId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Controls weather subnet is displayed as IP address or Name in subnets menu", - Position=7)] - [switch] - $ShowName, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Controls if PTR records should be created for subnet", - Position=8)] - [switch] - $DNSRecursive, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Controls weather hostname DNS records are displayed", - Position=9)] - [switch] - $DNSRecords, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Controls if IP requests are allowed for subnet", - Position=10)] - [switch] - $AllowRequests, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Controls which scanagent to use for subnet (default id 1)", - Position=11)] - [ValidateScript({ $_ -match "^\d+$" })] - [string] - $ScanAgentId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Controls if new hosts should be discovered for new host scans", - Position=12)] - [switch] - $DiscoverSubnet, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Marks subnet as used", - Position=12)] - [switch] - $IsFull, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Assignes state (tag) to subnet (default: 1 Used)", - Position=12)] - [ValidateScript({ $_ -match "^\d+$" })] - [string] - $TagId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Subnet threshold", - Position=13)] - [ValidateScript({ $_ -match "^\d+$" -and $_ -le 100 -and $_ -ge 1 })] - $Threshold, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Location index", - Position=14)] - [ValidateScript({ $_ -match "^\d+$" })] - [string] - $LocationId, - - [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] - [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] - $CustomFields - ) - process { - $_params = @{ - Controller = "subnets" - Method = "POST" - } - - $_body = @{ - subnet = $CIDR.Split('/')[0] - mask = $CIDR.Split('/')[1] - sectionId = $SectionId - } - if ($Description) { $_body.Add("description", $Description) } - if ($VlanId) { $_body.Add("vlanId", $VlanId) } - if ($VrfId) { $_body.Add("vrfId", $VrfId) } - if ($MasterSubnetId) { $_body.Add("masterSubnetId", $MasterSubnetId) } - if ($NameserverId) { $_body.Add("nameserverId", $NameserverId) } - if ($ShowName) { $_body.Add("showName", "1") } - if ($DNSRecursive) { $_body.Add("DNSrecursive", "1") } - if ($DNSRecords) { $_body.Add("DNSrecords", "1") } - if ($AllowRequests) { $_body.Add("allowRequests", "1") } - if ($ScanAgentId) { $_body.Add("scanAgent", $ScanAgentId) } - if ($DiscoverSubnet) { $_body.Add("discoverSubnet", "1") } - if ($IsFull) { $_body.Add("isFull", "1") } - if ($TagId) { $_body.Add("state", $TagId) } - if ($Threshold) { $_body.Add("threshold", $Threshold) } - if ($Location) { $_body.Add("location", $Location) } - - if ($CustomFields) { - if ($CustomFields -is [PSCustomObject]) { - $_customFields = @{}; - $CustomFields | Get-Member -MemberType *Property | Where-Object { - $_customFields.($_.name) = $CustomFields.($_.name) - } - } else { $_customFields = $CustomFields } - - $_body = $_body + $_customFields - } - - $_params.Add("Params",$_body) - - $_result = Invoke-PSIPAMRequest @_params - if ($_result) { - return Get-PSIPAMSubnet -CIDR $_result - } - } -} -Export-ModuleMember -Function New-PSIPAMSubnet \ No newline at end of file diff --git a/functions/public/New-Session.ps1 b/functions/public/New-Session.ps1 new file mode 100644 index 0000000..73fb82b --- /dev/null +++ b/functions/public/New-Session.ps1 @@ -0,0 +1,59 @@ +function New-Session { + [CmdletBinding(DefaultParameterSetName="Credentials")] + param ( + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] + [ValidateNotNullOrEmpty()] + [validatescript({$_.startswith("http")})] + [string]$URL, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1)] + [ValidateNotNullOrEmpty()] + [string]$AppID, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="Credentials")] + [ValidateNotNullOrEmpty()] + [pscredential]$Credentials, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="Token")] + [ValidateNotNullOrEmpty()] + [string]$Token, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=3)] + [switch]$IgnoreSSL = $false + ) + switch ($PSCmdlet.ParameterSetName) { + "Credentials" { + $_bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credentials.Password) + $_password = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($_bstr) + $_uri = "$URL/api/$AppID/user" + $_auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($Credentials.UserName):$_password")) + $_headers = @{ + "Accept" = "application/json" + "Content-Type" = "application/json" + "Authorization" = "Basic $_auth" + } + + $_response = Invoke-RestMethod -Method POST -Uri $_uri -Headers $_headers + + if ($_response.success -ne $true) { return $_response.error } + + $script:psipamSession = [PS.IPAM.Session]::new( + [ps.ipam.authType]::credentials, + $_response.data.token, + $AppID, + $URL, + (Get-Date $_response.data.expires), + $Credentials + ) + break + } + "Token" { + $script:psipamSession = [PS.IPAM.Session]::new( + [ps.ipam.authType]::token, + $Token, + $AppID, + $URL, + $null, + $null + ) + break + } + } +} +Export-ModuleMember -Function New-Session \ No newline at end of file diff --git a/functions/public/New-Subnet.ps1 b/functions/public/New-Subnet.ps1 new file mode 100644 index 0000000..3987f74 --- /dev/null +++ b/functions/public/New-Subnet.ps1 @@ -0,0 +1,124 @@ +function New-Subnet { + [CmdletBinding()] + param ( + [parameter( + Mandatory=$true, + ValueFromPipeline=$true, + ValueFromPipelineByPropertyName=$true, + HelpMessage="CIDR of subnet in dotted format (e.g 10.10.10.0/24)", + Position=0)] + [ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})] + [ValidateNotNullOrEmpty()] + [string] + $CIDR, + [parameter(Mandatory=$true,HelpMessage="Section identifier",Position=1)] + [ValidateNotNullOrEmpty()] + [int] + $SectionId, + [parameter(Mandatory=$false,HelpMessage="Subnet description",Position=2)] + [ValidateNotNullOrEmpty()] + [string] + $Description, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,HelpMessage="Assigns subnet to VLAN",Position=3)] + [ValidateNotNullOrEmpty()] + [int] + $VlanId, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,HelpMessage="Assigns subnet to VRF",Position=4)] + [ValidateNotNullOrEmpty()] + [int] + $VrfId, + [parameter(Mandatory=$false,HelpMessage="Master subnet id for nested subnet",Position=5)] + [ValidateNotNullOrEmpty()] + [int] + $MasterSubnetId, + [parameter(Mandatory=$false,HelpMessage="Id of nameserver to attach to subnet",Position=6)] + [ValidateNotNullOrEmpty()] + [int] + $NameserverId, + [parameter(Mandatory=$false,HelpMessage="Controls weather subnet is displayed as IP address or Name in subnets menu",Position=7)] + [switch] + $ShowName, + [parameter(Mandatory=$false,HelpMessage="Controls if PTR records should be created for subnet",Position=8)] + [switch] + $DNSRecursive, + [parameter(Mandatory=$false,HelpMessage="Controls weather hostname DNS records are displayed",Position=9)] + [switch] + $DNSRecords, + [parameter(Mandatory=$false,HelpMessage="Controls if IP requests are allowed for subnet",Position=10)] + [switch] + $AllowRequests, + [parameter(Mandatory=$false,HelpMessage="Controls which scanagent to use for subnet (default id 1)",Position=11)] + [ValidateNotNullOrEmpty()] + [int] + $ScanAgentId, + [parameter(Mandatory=$false,HelpMessage="Controls if new hosts should be discovered for new host scans",Position=12)] + [switch] + $DiscoverSubnet, + [parameter(Mandatory=$false,HelpMessage="Marks subnet as used",Position=12)] + [switch] + $IsFull, + [parameter(Mandatory=$false,HelpMessage="Assignes state (tag) to subnet (default: 1 Used)",Position=12)] + [ValidateNotNullOrEmpty()] + [int] + $TagId, + [parameter(Mandatory=$false,HelpMessage="Subnet threshold",Position=13)] + [ValidateScript({ $_ -le 100 -and $_ -ge 1 })] + [ValidateNotNullOrEmpty()] + [int] + $Threshold, + [parameter(Mandatory=$false,HelpMessage="Location index",Position=14)] + [ValidateNotNullOrEmpty()] + [int] + $LocationId, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] + [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] + [ValidateNotNullOrEmpty()] + $CustomFields + ) + process { + $_params = @{ + Controller = "subnets" + Method = "POST" + } + + $_body = @{ + subnet = $CIDR.Split('/')[0] + mask = $CIDR.Split('/')[1] + sectionId = $SectionId + } + if ($Description) { $_body.Add("description", $Description) } + if ($VlanId) { $_body.Add("vlanId", $VlanId) } + if ($VrfId) { $_body.Add("vrfId", $VrfId) } + if ($MasterSubnetId) { $_body.Add("masterSubnetId", $MasterSubnetId) } + if ($NameserverId) { $_body.Add("nameserverId", $NameserverId) } + if ($ShowName) { $_body.Add("showName", "1") } + if ($DNSRecursive) { $_body.Add("DNSrecursive", "1") } + if ($DNSRecords) { $_body.Add("DNSrecords", "1") } + if ($AllowRequests) { $_body.Add("allowRequests", "1") } + if ($ScanAgentId) { $_body.Add("scanAgent", $ScanAgentId) } + if ($DiscoverSubnet) { $_body.Add("discoverSubnet", "1") } + if ($IsFull) { $_body.Add("isFull", "1") } + if ($TagId) { $_body.Add("state", $TagId) } + if ($Threshold) { $_body.Add("threshold", $Threshold) } + if ($Location) { $_body.Add("location", $Location) } + + if ($CustomFields) { + if ($CustomFields -is [PSCustomObject]) { + $_customFields = @{}; + $CustomFields | Get-Member -MemberType *Property | Where-Object { + $_customFields.($_.name) = $CustomFields.($_.name) + } + } else { $_customFields = $CustomFields } + + $_body = $_body + $_customFields + } + + $_params.Add("Params",$_body) + + $_result = Invoke-Request @_params + if ($_result) { + return Get-Subnet -CIDR $_result + } + } +} +Export-ModuleMember -Function New-Subnet \ No newline at end of file diff --git a/functions/public/Remove-Address.ps1 b/functions/public/Remove-Address.ps1 new file mode 100644 index 0000000..07a5a07 --- /dev/null +++ b/functions/public/Remove-Address.ps1 @@ -0,0 +1,28 @@ +function Remove-Address { + [CmdletBinding(DefaultParameterSetName="ByID")] + param ( + [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,ParameterSetName="ByID")] + [ValidateNotNullOrEmpty()] + [int] + $Id, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Address] + $AddressObject + ) + process { + $_params = @{ + Controller = [PS.IPAM.controllers]::addresses + Method = "DELETE" + } + + switch ($PSCmdlet.ParameterSetName) { + "ByID" { $_identifiers = @($Id); break } + "ByAddressObject" { $_identifiers = @($AddressObject.Id); break } + } + $_params.Add("Identifiers",$_identifiers) + + Invoke-Request @_params + } +} +Export-ModuleMember Remove-Address \ No newline at end of file diff --git a/functions/public/Remove-PSIPAMAddress.ps1 b/functions/public/Remove-PSIPAMAddress.ps1 deleted file mode 100644 index 49d31b0..0000000 --- a/functions/public/Remove-PSIPAMAddress.ps1 +++ /dev/null @@ -1,35 +0,0 @@ -function Remove-PSIPAMAddress { - [CmdletBinding(DefaultParameterSetName="ByID")] - param ( - [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,ParameterSetName="ByID")] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $Id, - [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,ParameterSetName="ByIP")] - [ValidateScript({[ipaddress] $_ })] - [ValidateNotNullOrEmpty()] - [string] - $IP, - [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=1,ParameterSetName="ByIP")] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $SubnetId - ) - process { - $_params = @{ - Controller = "addresses" - Method = "DELETE" - } - - switch ($PSCmdlet.ParameterSetName) { - "ByID" { $_identifiers = @($Id) } - "ByIP" { $_identifiers = @($IP,$SubnetId) } - } - $_params.Add("Identifiers",$_identifiers) - - Invoke-PSIPAMRequest @_params - } -} -Export-ModuleMember Remove-PSIPAMAddress \ No newline at end of file diff --git a/functions/public/Set-Address.ps1 b/functions/public/Set-Address.ps1 new file mode 100644 index 0000000..f47ef10 --- /dev/null +++ b/functions/public/Set-Address.ps1 @@ -0,0 +1,110 @@ +function Set-Address { + [CmdletBinding(DefaultParameterSetName="ById")] + param ( + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,HelpMessage="Id of subnet address belongs to",Position=0,ParameterSetName="ById")] + [ValidateNotNullOrEmpty()] + [int] + $Id, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")] + [ValidateNotNullOrEmpty()] + [PS.IPAM.Address] + $AddressObject, + [parameter(Mandatory=$false,HelpMessage="Defines if address is presented as gateway",Position=1)] + [bool] + $Gateway, + [parameter(Mandatory=$false,HelpMessage="Address description",Position=2)] + [ValidateNotNullOrEmpty()] + [string] + $Description, + [parameter(Mandatory=$false,HelpMessage="Address hostname",Position=3)] + [ValidateNotNullOrEmpty()] + [string] + $Hostname, + [parameter(Mandatory=$false,HelpMessage="Mac address",Position=4)] + [ValidateScript({ $_.Replace(":","") -match "^$('([A-F0-9]{2})' * 6)$" })] + [ValidateNotNullOrEmpty()] + [string] + $MAC, + [parameter(Mandatory=$false,HelpMessage="Address owner",Position=5)] + [ValidateNotNullOrEmpty()] + [string] + $Owner, + [parameter(Mandatory=$false,HelpMessage="Id of subnet address belongs to",Position=6)] + [ValidateNotNullOrEmpty()] + [int] + $TagId, + [parameter(Mandatory=$false,HelpMessage="Controls if PTR should not be created",Position=7)] + [bool] + $PTRIgnore, + [parameter(Mandatory=$false,HelpMessage="Id of PowerDNS PTR record",Position=8)] + [ValidateNotNullOrEmpty()] + [int] + $PTRId, + [parameter(Mandatory=$false,HelpMessage="Note",Position=9)] + [ValidateNotNullOrEmpty()] + [string] + $Note, + [parameter(Mandatory=$false,HelpMessage="Exclude this address from status update scans (ping)",Position=10)] + [bool] + $ExcludePing, + [parameter(Mandatory=$false,HelpMessage="Id of device address belongs to",Position=11)] + [ValidateNotNullOrEmpty()] + [int] + $DeviceId, + [parameter(Mandatory=$false,HelpMessage="Port",Position=12)] + [ValidateNotNullOrEmpty()] + [string] + $Port, + [parameter(Mandatory=$false)] + [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] + $CustomFields + ) + process { + $_params = @{ + Controller = [PS.IPAM.controllers]::addresses + Method = "PATCH" + } + switch ($PSCmdlet.ParameterSetName) { + "ByID" { $_id = $Id; break } + "ByAddressObject" { $_id = $AddressObject.id; break } + } + $_identifiers = @($_id) + + $_params.Add("Identifiers",$_identifiers) + + $_body = @{ } + if ($Gateway) { $_body.Add("is_gateway", $Gateway) } + if ($Description) { $_body.Add("description", $Description) } + if ($Hostname) { $_body.Add("hostname", $Hostname) } + if ($MAC) { $_body.Add("mac", $MAC) } + if ($Owner) { $_body.Add("owner", $Owner) } + if ($TagId) { $_body.Add("tag", $TagId) } + if ($PTRIgnore) { $_body.Add("PTRignore", $PTRIgnore) } + if ($PTRId) { $_body.add("PTR", $PTRId)} + if ($Note) { $_body.Add("note", $Note) } + if ($ExcludePing) { $_body.Add("excludePing", $ExcludePing) } + if ($DeviceId) { $_body.Add("deviceId", $DeviceId) } + if ($Port) { $_body.Add("port", $Port) } + + if ($CustomFields) { + if ($CustomFields -is [PSCustomObject]) { + $_customFields = @{}; + $CustomFields | Get-Member -MemberType *Property | Where-Object { + $_customFields.($_.name) = $CustomFields.($_.name) + } + } else { $_customFields = $CustomFields } + + $_body = $_body + $_customFields + } + + $_params.Add("Params",$_body) + + try { + Invoke-Request @_params + } + finally { + Get-Address -Id $_id + } + } +} +Export-ModuleMember -Function Set-Address \ No newline at end of file diff --git a/functions/public/Set-PSIPAMAddress.ps1 b/functions/public/Set-PSIPAMAddress.ps1 deleted file mode 100644 index df04f00..0000000 --- a/functions/public/Set-PSIPAMAddress.ps1 +++ /dev/null @@ -1,172 +0,0 @@ -function Set-PSIPAMAddress { - [CmdletBinding()] - param ( - [parameter( - Mandatory=$true, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Id of subnet address belongs to", - Position=0)] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $Id, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Defines if address is presented as gateway", - Position=1)] - [bool] - $Gateway, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Address description", - Position=2)] - [ValidateNotNullOrEmpty()] - [string] - $Description, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Address hostname", - Position=3)] - [ValidateNotNullOrEmpty()] - [string] - $Hostname, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Mac address", - Position=4)] - [ValidateScript({ $_.Replace(":","") -match "^$('([A-F0-9]{2})' * 6)$" })] - [ValidateNotNullOrEmpty()] - [string] - $MAC, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Address owner", - Position=5)] - [ValidateNotNullOrEmpty()] - [string] - $Owner, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Id of subnet address belongs to", - Position=6)] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $TagId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Controls if PTR should not be created", - Position=7)] - [bool] - $PTRIgnore, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Id of PowerDNS PTR record", - Position=8)] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $PTRId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Note", - Position=9)] - [ValidateNotNullOrEmpty()] - [string] - $Note, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Exclude this address from status update scans (ping)", - Position=10)] - [bool] - $ExcludePing, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Id of device address belongs to", - Position=11)] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $DeviceId, - [parameter( - Mandatory=$false, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Port", - Position=12)] - [ValidateNotNullOrEmpty()] - [string] - $Port, - - [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] - [ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })] - $CustomFields - ) - process { - $_params = @{ - Controller = "addresses" - Method = "PATCH" - } - $_identifiers = @($Id) - - $_params.Add("Identifiers",$_identifiers) - - $_body = @{ } - if ($Gateway) { $_body.Add("is_gateway", [int]$Gateway) } - if ($Description) { $_body.Add("description", $Description) } - if ($Hostname) { $_body.Add("hostname", $Hostname) } - if ($MAC) { $_body.Add("mac", $MAC) } - if ($Owner) { $_body.Add("owner", $Owner) } - if ($TagId) { $_body.Add("tag", $TagId) } - if ($PTRIgnore) { $_body.Add("PTRignore", [int]$PTRIgnore) } - if ($PTRId) { $_body.add("PTR", $PTRId)} - if ($Note) { $_body.Add("note", $Note) } - if ($ExcludePing) { $_body.Add("excludePing", [int]$ExcludePing) } - if ($DeviceId) { $_body.Add("deviceId", $DeviceId) } - if ($Port) { $_body.Add("port", $Port) } - - if ($CustomFields) { - if ($CustomFields -is [PSCustomObject]) { - $_customFields = @{}; - $CustomFields | Get-Member -MemberType *Property | Where-Object { - $_customFields.($_.name) = $CustomFields.($_.name) - } - } else { $_customFields = $CustomFields } - - $_body = $_body + $_customFields - } - - $_params.Add("Params",$_body) - - try { - Invoke-PSIPAMRequest @_params - } - finally { - Get-PSIPAMAddress -Id $Id - } - } -} -Export-ModuleMember -Function Set-PSIPAMAddress \ No newline at end of file diff --git a/functions/public/Set-PSIPAMSubnet.ps1 b/functions/public/Set-Subnet.ps1 similarity index 100% rename from functions/public/Set-PSIPAMSubnet.ps1 rename to functions/public/Set-Subnet.ps1 diff --git a/ps.ipam.psd1 b/ps.ipam.psd1 index 24b4119..d81187c 100644 Binary files a/ps.ipam.psd1 and b/ps.ipam.psd1 differ diff --git a/types/types.ps1xml b/types/types.ps1xml new file mode 100644 index 0000000..a56619e --- /dev/null +++ b/types/types.ps1xml @@ -0,0 +1,172 @@ + + + + ps.ipam.address + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Id + Ip + Hostname + Description + + + + DefaultKeyPropertySet + + Id + + + + + + + + ps.ipam.tag + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Id + Type + + + + DefaultKeyPropertySet + + Id + + + + + + + + ps.ipam.domain + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Id + Name + + + + DefaultKeyPropertySet + + Id + + + + + + + + ps.ipam.section + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Id + Name + MasterSectionId + StrictMode + + + + DefaultKeyPropertySet + + Id + + + + + + + + ps.ipam.vlan + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Id + Name + DomainId + Number + + + + DefaultKeyPropertySet + + Id + + + + + + + + ps.ipam.vrf + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Id + Name + + + + DefaultKeyPropertySet + + Id + + + + + + + + ps.ipam.subnet + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Id + Name + DomainId + Number + + + + DefaultKeyPropertySet + + Id + + + + + + + \ No newline at end of file