Modules/networks.psm1

#GET/rest/v2.0/dd-systems/{SYSTEM-ID}/networks
function Get-DDnetworks {
   [CmdletBinding()]
   param ( 
      [CmdletBinding()]
      [Parameter(Mandatory = $true, ParameterSetName = 'host-names' )]
      [switch]$hostnames,
      [Parameter(Mandatory = $true, ParameterSetName = 'domain-names' )]
      [switch]$domainnames,
      [Parameter(Mandatory = $true, ParameterSetName = 'search-domains' )]
      [switch]$searchdomains,
      [Parameter(Mandatory = $true, ParameterSetName = 'dns-servers' )]
      [switch]$dnsservers,  
      [Parameter(Mandatory = $true, ParameterSetName = 'gateways' )]
      [switch]$Gateways,
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces' )]
      [switch]$Interfaces,
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/physicals' )]
      [switch]$PhysicalInterfaces,
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [string]$ID,               
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/vlans' )]
      [switch]$VlanInterfaces,
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/aliases' )]
      [switch]$AliasInterfaces,
      [Parameter(Mandatory = $true, ParameterSetName = 'nics' )]
      [switch]$nics,                                                          
      [Parameter(Mandatory = $false, ParameterSetName = 'default')]  
      [Parameter(Mandatory = $false, ParameterSetName = 'host-names' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'domain-names' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'search-domains' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'dns-servers' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'gateways' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'nics' )]
      [string]
      $SYSTEM_ID = $Global:DD_UUID,
      $body = @{}
   )
   begin {
      $Response = @()
      $METHOD = "GET"
      $apiver = "api/v3"
   }
   process {
      switch ($PsCmdlet.ParameterSetName) {
         $PsCmdlet.ParameterSetName {
            $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)"
            if ($ID) {
               $uri = Join-Path $uri $id
            }
         }  
         'default' {
            $uri = "dd-systems/$($SYSTEM_ID)/networks"
         }
         'nics' {
            $apiver = "rest/v2.0"
            $uri = "dd-systems/$($SYSTEM_ID)/networks/nics"
         }                               
      }
      $parameters = @{
         uri           = $uri          
         apiver        = $apiver
         Method        = $METHOD
         body          = $body
         RequestMethod = "REST"
         Verbose       = $PSBoundParameters['Verbose'] -eq $true
         Debug         = $PSBoundParameters['Debug'] -eq $true
      }      
      try {
         $Response += Invoke-DDapirequest @Parameters
      }
      catch {
         Get-DDWebException  -ExceptionMessage $_
         break
      }
      write-verbose ($response | Out-String)
   }
   end {    
      switch ($PsCmdlet.ParameterSetName) {
         default {
            $response | Select-Object -ExcludeProperty links, link,  paging_info
         }
         'nics' {
            $response.nics | Select-Object -ExcludeProperty links, link,  paging_info
         } 
         'interfaces' {            
            $response.interfaces | Select-Object -ExcludeProperty links, link,  paging_info
         }
         'gateways' {
            $response.defaultGateways  | Select-Object -ExcludeProperty links, link,  paging_info
         }
         'search-domains' {
            $response | Select-Object   -ExcludeProperty links, link,  paging_info
         }
         'interfaces/physicals' {
            if ($ID) {
               $response | Select-Object -ExcludeProperty links, link,  paging_info
            }
            else {
               $response.physicalInterfaces | Select-Object -ExcludeProperty links, link,  paging_info
            }
         }
         'interfaces/aliases' {
            if ($ID) {
               $response | Select-Object -ExcludeProperty links, link,  paging_info
            }
            else {
               $response.aliases | Select-Object -ExcludeProperty links, link,  paging_info
            }
         } 
         'interfaces/vlans' {
            if ($ID) {
               $response | Select-Object -ExcludeProperty links, link,  paging_info
            }
            else {
               $response.vlans | Select-Object -ExcludeProperty links, link,  paging_info
            }
         }                           
      } 
       
   }
}


function Reset-DDnetworks {
   [CmdletBinding()]
   param ( 
      [CmdletBinding()]
      [Parameter(Mandatory = $true, ParameterSetName = 'host-names' )]
      [switch]$hostnames,
      [Parameter(Mandatory = $true, ParameterSetName = 'domain-names' )]
      [switch]$domainnames,
      [Parameter(Mandatory = $true, ParameterSetName = 'search-domains' )]
      [switch]$searchdomains,
      [Parameter(Mandatory = $true, ParameterSetName = 'dns-servers' )]
      [switch]$dnsservers,  
      [Parameter(Mandatory = $true, ParameterSetName = 'gateways' )]
      [switch]$Gateways,                                                      
      [Parameter(Mandatory = $false, ParameterSetName = 'host-names' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'domain-names' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'search-domains' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'dns-servers' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'gateways' )]
      [string]
      $SYSTEM_ID = $Global:DD_UUID,
      $body = @{}
   )
   begin {
      $Response = @()
      $METHOD = "PUT"
      $apiver = "api/v3"
   }
   process {
      switch ($PsCmdlet.ParameterSetName) {
         $PsCmdlet.ParameterSetName {
            $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)/reset"
         }                             
      }
      $parameters = @{
         uri           = $uri          
         apiver        = $apiver
         Method        = $METHOD
         body          = $body
         RequestMethod = "REST"
         Verbose       = $PSBoundParameters['Verbose'] -eq $true
         Debug         = $PSBoundParameters['Debug'] -eq $true
      }      
      try {
         $Response += Invoke-DDapirequest @Parameters
      }
      catch {
         Get-DDWebException  -ExceptionMessage $_
         break
      }
      write-verbose ($response | Out-String)
   }
   end {    
      switch ($PsCmdlet.ParameterSetName) {
         default {
            $response | Select-Object -ExcludeProperty links, link,  paging_info
         }
      } 
       
   }
}


#PUT/rest/v2.0/dd-systems/{SYSTEM-ID}/networks
function Set-DDnetworks {
   [CmdletBinding()]
   param ( 
      [CmdletBinding()]
      [Parameter(Mandatory = $true, ParameterSetName = 'host-names' )]
      [string]$hostName,
      [Parameter(Mandatory = $true, ParameterSetName = 'domain-names' )]
      [string]$domainName,
      [Parameter(Mandatory = $true, ParameterSetName = 'search-domains' )]
      [string[]]$searchDomain,
      [Parameter(Mandatory = $true, ParameterSetName = 'dns-servers' )]
      [ValidateCount(1, 3)]
      [ipaddress[]]$dnsServerIpAddresses,  
      [Parameter(Mandatory = $true, ParameterSetName = 'gateways' )]
      [ipaddress]$Gateway,
      [Parameter(Mandatory = $false, ParameterSetName = 'gateways' )]
      [ValidateSet('ipv4', 'ipv6')]
      [string]$AddressFamily = 'ipv4',

      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [ipaddress]$Address,

      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [ipaddress]$Netmask,

      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/physicals' )]
      [string][alias('ID')]$InterfaceID,

      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [string][ValidateSet('NO', 'IPV4', 'IPV6')]$DHCP,   

      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [string][ValidateSet('UP', 'DOWN')]$enable,

      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [int]$mtu,

      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [string][ValidateSet('10', '100', '1000', '10000')]$linkspeed,

      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [string][ValidateSet('FULL', 'HALF')]$linkDuplex,

      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [switch]$linkAutoNego,
      
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [int32]$txQueueLen,


      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/vlans' )]
      [string]$VlanInterfaceID,
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/aliases' )]
      [string]$AliasInterfaceID,
      [Parameter(Mandatory = $true, ParameterSetName = 'nics' )]
      [switch]$nics,                                                          
      [Parameter(Mandatory = $false, ParameterSetName = 'default')]  
      [Parameter(Mandatory = $false, ParameterSetName = 'host-names' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'domain-names' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'search-domains' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'dns-servers' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'gateways' )]
      # [Parameter(Mandatory = $false, ParameterSetName = 'interfaces' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/physicals' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'nics' )]

      [string]
      $SYSTEM_ID = $Global:DD_UUID,
      $body = @{}
   )
   begin {
      $Response = @()
      $METHOD = "PUT"
      $apiver = "api/v3"
   }
   process {
      switch ($PsCmdlet.ParameterSetName) {
         'dns-servers' {
            $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)"
            $dnsserver = $dnsServerIpAddresses | Select-Object IPAddressToString 
            $body.Add('dnsServerIpAddresses', $dnsServer.IPAddressToString)
            $body = $body | ConvertTo-Json
         }  
         'domain-names' {
            $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)"
            $body.Add('domainName', $domainName)
            $body = $body | ConvertTo-Json
         }  
         'host-names' {
            $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)"
            $body.Add('hostName', $hostName)
            $body = $body | ConvertTo-Json
         }
         'search-domains' {
            $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)"
            $body.Add('searchDomains', $searchDomain)
            $body = $body | ConvertTo-Json
         } 
         'gateways' {
            $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)/static/$($AddressFamily)"
            $body.Add('ipAddress', $Gateway.IPAddressToString )
            $body = $body | ConvertTo-Json
         }  
         { ($_ -eq 'interfaces/physicals') -or
            ($_ -eq 'interfaces/vlans') -or
            ($_ -eq 'interfaces/aliases') } {
            if ($InterfaceID) {              
               $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)/$($InterfaceID)"
            }
            if ($AliasInterfaceID) {              
               $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)/$($AliasInterfaceID)"
            }
            if ($VlanInterfaceID) {              
               $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)/$($VlanInterfaceID)"
            }                          
            if ($Address) {
               $body.Add('address', $Address.IPAddressToString )
            }
            if ($Netmask) {
               $body.Add('netmask', $Netmask.IPAddressToString )
            }
            if ($DHCP) {
               $body.Add('dhcp', $DHCP )
            } 
            if ($enable) {
               $body.Add('enable', $enable )
            }
            if ($mtu) {
               $body.Add('mtu', $mtu )
            }            
            if ($linkspeed) {
               $body.Add('linkSpeed', $linkspeed )
            }  
            if ($linkDuplex) {
               $body.Add('linkDuplex', $linkDuplex )
            }       
            if ($linkAutoNego) {
               $body.Add('linkAutoNego', $linkAutoNego.IsPresent )
            }  
            if ($txQueueLen) {
               $body.Add('txQueueLen', $txQueueLen )
            } 
            $body = $body | ConvertTo-Json
         }                                             
         default {
            $uri = "dd-systems/$($SYSTEM_ID)/networks"
         }
         'nics' {
            $apiver = "rest/v2.0"
            $uri = "dd-systems/$($SYSTEM_ID)/networks/nics"
         }                               
      }
      Write-Verbose ($body | Out-String)
      $parameters = @{
         uri           = $uri          
         apiver        = $apiver
         Method        = $METHOD
         body          = $body
         RequestMethod = "REST"
         Verbose       = $PSBoundParameters['Verbose'] -eq $true
         Debug         = $PSBoundParameters['Debug'] -eq $true
      }      
      try {
         $Response += Invoke-DDapirequest @Parameters
      }
      catch {
         Get-DDWebException  -ExceptionMessage $_
         break
      }
      write-verbose ($response | Out-String)
   }
   end {    
      switch ($PsCmdlet.ParameterSetName) {
         default {
            $response | Select-Object -ExcludeProperty links, link,  paging_info
         }
         'nics' {
            $responsev | Select-Object -ExcludeProperty links, link,  paging_info
         } 
         'interfaces' {
            $response | Select-Object -ExcludeProperty links, link,  paging_info
         }
         'gateways' {
            $response  | Select-Object -ExcludeProperty links, link,  paging_info
         }
         'search-domains' {
            $response | Select-Object   -ExcludeProperty links, link,  paging_info
         }        
      } 
       
   }
}

function New-DDnetworksInterface {
   [CmdletBinding()]
   param ( 
      [CmdletBinding()]
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/aliases' )]
      [string][alias('AliasID')]$AliasInterfaceID, 
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/vlans' )]
      [string][alias('VlanID')]$VlanInterfaceID,           
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
      [ipaddress]$Address,
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
      [ipaddress]$Netmask,


      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]      
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
      [string][ValidateSet('UP', 'DOWN')]$enable, 


      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [int32][ValidateSet('UP', 'DOWN')]$mtu, 



      <#
 
 
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/vlans' )]
      [switch]$VlanInterfaces,
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/aliases' )]
      [switch]$AliasInterfaces,
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/vlans' )]
      [Parameter(Mandatory = $false, ParameterSetName = 'interfaces/aliases' )]
#>

      [string]
      $SYSTEM_ID = $Global:DD_UUID,
      $body = @{}
   )
   begin {
      $Response = @()
      $METHOD = "POST"
      $apiver = "api/v3"
   }
   process {
      switch ($PsCmdlet.ParameterSetName) {
         default {
            $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)"
            if ($Address) {
               $body.Add('address', $Address.IPAddressToString )
            }
            if ($AliasInterfaceID) {
               $body.Add('id', $AliasInterfaceID)
            }
            if ($VlanInterfaceID) {
               $body.Add('id', $VlanInterfaceID)
            }            
            if ($Netmask) {
               $body.Add('netmask', $Gateway.IPAddressToString )
            }

            if ($enable) {
               $body.Add('enable', $enable )
            }
            if ($mtu) {
               $body.Add('mtu', $mtu )
            }            
            $body = $body | ConvertTo-Json
         }                                                          
      }
      Write-Verbose ($body | Out-String)
      $parameters = @{
         uri           = $uri          
         apiver        = $apiver
         Method        = $METHOD
         body          = $body
         RequestMethod = "REST"
         Verbose       = $PSBoundParameters['Verbose'] -eq $true
         Debug         = $PSBoundParameters['Debug'] -eq $true
      }      
      try {
         $Response += Invoke-DDapirequest @Parameters
      }
      catch {
         Get-DDWebException  -ExceptionMessage $_
         break
      }
      write-verbose ($response | Out-String)
   }
   end {    
      switch ($PsCmdlet.ParameterSetName) {
         default {
            $response | Select-Object -ExcludeProperty links, link,  paging_info
         }
         'nics' {
            $responsev | Select-Object -ExcludeProperty links, link,  paging_info
         } 
         'interfaces' {
            $response | Select-Object -ExcludeProperty links, link,  paging_info
         }
         'gateways' {
            $response  | Select-Object -ExcludeProperty links, link,  paging_info
         }
         'search-domains' {
            $response | Select-Object   -ExcludeProperty links, link,  paging_info
         }        
      } 
       
   }
}
# DELETE /api/v3/dd-systems/{SYSTEM-ID}/networks/interfaces/aliases/{ID}
function Remove-DDnetworksInterface {
   [CmdletBinding()]
   param ( 
      [CmdletBinding()]
      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/aliases' )]
      [string][alias('AliasID')]$AliasInterfaceID,



      [Parameter(Mandatory = $true, ParameterSetName = 'interfaces/vlans' )]
      [string][alias('VLanID')]$VlanInterfaceID,
      [string]$SYSTEM_ID = $Global:DD_UUID,
      $body = @{}
   )
   begin {
      $Response = @()
      $METHOD = "DELETE"
      $apiver = "api/v3"
   }
   process {
      switch ($PsCmdlet.ParameterSetName) {
         default {
            if ($AliasInterfaceID) {
               $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)/$($AliasInterfaceID)"
            }
            if ($VlanInterfaceID) {
               $uri = "dd-systems/$($SYSTEM_ID)/networks/$($PsCmdlet.ParameterSetName)/$($VlanInterfaceID)"
            }            
            $body = $body | ConvertTo-Json
         }                                                          
      }
      Write-Verbose ($body | Out-String)
      $parameters = @{
         uri           = $uri          
         apiver        = $apiver
         Method        = $METHOD
         body          = $body
         RequestMethod = "REST"
         Verbose       = $PSBoundParameters['Verbose'] -eq $true
         Debug         = $PSBoundParameters['Debug'] -eq $true
      }      
      try {
         $Response += Invoke-DDapirequest @Parameters
      }
      catch {
         Get-DDWebException  -ExceptionMessage $_
         break
      }
      write-verbose ($response | Out-String)
   }
   end {    
      switch ($PsCmdlet.ParameterSetName) {
         default {
            $response | Select-Object -ExcludeProperty links, link,  paging_info      
         } 
      }  
   }
}