Skip to content

Commit

Permalink
ShouldProcess New- & Remove- DDI functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TehMuffinMoo committed Jul 18, 2024
1 parent 7023894 commit 7fa8206
Show file tree
Hide file tree
Showing 48 changed files with 1,265 additions and 731 deletions.
6 changes: 6 additions & 0 deletions Modules/ibPS/Functions/BloxOne/BloxOneDDI/Get-B1Address.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
[String]$Address,
[Parameter(ParameterSetName="With Address")]
[String]$State,
[Parameter(ParameterSetName="With Address")]
[String]$Space,
[Switch]$Reserved,
[String]$Compartment,
[Int]$Limit = 1000,
Expand All @@ -83,6 +85,10 @@
if ($State) {
$Filters.Add("state==`"$State`"") | Out-Null
}
if ($Space) {
$SpaceUUID = (Get-B1Space -Name $Space -Strict).id
$Filters.Add("space==`"$SpaceUUID`"") | Out-Null
}
if ($id) {
$Filters.Add("id==`"$id`"") | Out-Null
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/ibPS/Functions/BloxOne/BloxOneDDI/Get-B1Range.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@
}
Write-DebugMsg -Filters $QueryFilters
if ($QueryString) {
Invoke-CSP -Uri "ipam/range$($QueryString)" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
Invoke-CSP -Uri "$(Get-B1CSPUrl)/api/ddi/v1/ipam/range$($QueryString)" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
} else {
Invoke-CSP -Uri "ipam/range?_limit=$Limit&_offset=$Offset" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
Invoke-CSP -Uri "$(Get-B1CSPUrl)/api/ddi/v1/ipam/range?_limit=$Limit&_offset=$Offset" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
}
}
4 changes: 2 additions & 2 deletions Modules/ibPS/Functions/BloxOne/BloxOneDDI/Get-B1Space.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@
}
Write-DebugMsg -Filters $QueryFilters
if ($QueryString) {
$Results = Invoke-CSP -Uri "ipam/ip_space$($QueryString)" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
$Results = Invoke-CSP -Uri "$(Get-B1CSPUrl)/api/ddi/v1/ipam/ip_space$($QueryString)" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
} else {
$Results = Invoke-CSP -Uri "ipam/ip_space" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
$Results = Invoke-CSP -Uri "$(Get-B1CSPUrl)/api/ddi/v1/ipam/ip_space" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
}
if ($Results) {
return $Results
Expand Down
4 changes: 2 additions & 2 deletions Modules/ibPS/Functions/BloxOne/BloxOneDDI/Get-B1Subnet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@
}
Write-DebugMsg -Filters $QueryFilters
if ($QueryString) {
Invoke-CSP -Uri "ipam/subnet$($QueryString)" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
Invoke-CSP -Uri "$(Get-B1CSPUrl)/api/ddi/v1/ipam/subnet$($QueryString)" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
} else {
Invoke-CSP -Uri "ipam/subnet?_limit=$Limit&_offset=$Offset" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
Invoke-CSP -Uri "$(Get-B1CSPUrl)/api/ddi/v1/ipam/subnet?_limit=$Limit&_offset=$Offset" -Method GET | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
}

if ($Parent) {
Invoke-CSP -Method "GET" -Uri "$($Parent.id)/nextavailablesubnet?cidr=$CIDRSize&count=$Count" | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
Invoke-CSP -Method "GET" -Uri "$(Get-B1CSPUrl)/api/ddi/v1/$($Parent.id)/nextavailablesubnet?cidr=$CIDRSize&count=$Count" | Select-Object -ExpandProperty results -ErrorAction SilentlyContinue
} else {
Write-Host "Unable to find Parent Address Block: $ParentAddressBlock" -ForegroundColor Red
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
.PARAMETER Force
Perform the operation without prompting for confirmation. By default, this function will always prompt for confirmation unless -Confirm:$false or -Force is specified, or $ConfirmPreference is set to None.
.PARAMETER Object
The DHCP Host object(s) to grant the DHCP Config Profile to
.EXAMPLE
PS> Grant-B1DHCPConfigProfile -Name "Data Centre" -Hosts "bloxoneddihost1.mydomain.corp","bloxoneddihost2.mydomain.corp"
Expand All @@ -33,47 +36,54 @@
[Parameter(Mandatory=$true)]
[String]$Name,
[Parameter(ParameterSetName="Default",Mandatory=$true)]
[System.Object]$Hosts,
[String[]]$Hosts,
[Parameter(
ValueFromPipelineByPropertyName = $true,
ParameterSetName="With ID",
ValueFromPipeline = $true,
ParameterSetName="Object",
Mandatory=$true
)]
[String]$id,
[System.Object]$Object,
[Switch]$Force
)

process {
$ConfirmPreference = Confirm-ShouldProcess $PSBoundParameters
$LoopHosts = @()

if ($id) {
## Not Implemented
} else {
$Objects = @()
if ($Object) {
$SplitID = $Object.id.split('/')
if (("$($SplitID[0])/$($SplitID[1])") -ne "dhcp/host") {
Write-Error "Error. Unsupported pipeline object. This function only supports 'dhcp/host' objects as input"
return $null
} else {
$Objects += $Object
}
} else {
foreach ($DHCPHost in $Hosts) {
$LoopHosts += Get-B1DHCPHost -Name $DHCPHost -Strict
$Objects += Get-B1DHCPHost -Name $DHCPHost -Strict
}
}

$DHCPConfigProfileId = (Get-B1DHCPConfigProfile -Name $Name -Strict).id
if (!$DHCPConfigProfileId) {
Write-Error "Failed to get DHCP Config Profile."
break
}
}

foreach ($LH in $LoopHosts) {
$DHCPConfigProfileId = (Get-B1DHCPConfigProfile -Name $Name -Strict).id
if (!$DHCPConfigProfileId) {
Write-Error "Failed to get DHCP Config Profile."
break
} else {
foreach ($iObject in $Objects) {
$splat = @{
"server" = $DHCPConfigProfileId
}

$splat = $splat | ConvertTo-Json

$Result = Invoke-CSP -Method "PATCH" -Uri $($LH.id) -Data $splat | Select-Object -ExpandProperty result -ErrorAction SilentlyContinue
if ($Result.server -eq $DHCPConfigProfileId) {
Write-Host "DHCP Config Profile `"$Name`" has been successfully applied to $($LH.name)" -ForegroundColor Green
} else {
Write-Host "Failed to apply DHCP Config Profile `"$Name`" to $($LH.name)" -ForegroundColor Red

if($PSCmdlet.ShouldProcess("Assign DHCP Config Profile: $($Name) to Host: $($iObject.name)","Assign DHCP Config Profile: $($Name) to Host: $($iObject.name)",$MyInvocation.MyCommand)){
$Result = Invoke-CSP -Method "PATCH" -Uri "$(Get-B1CSPUrl)/api/ddi/v1/$($iObject.id)" -Data $splat | Select-Object -ExpandProperty result -ErrorAction SilentlyContinue
if ($Result.server -eq $DHCPConfigProfileId) {
Write-Host "DHCP Config Profile `"$Name`" has been successfully applied to $($iObject.name)" -ForegroundColor Green
} else {
Write-Host "Failed to apply DHCP Config Profile `"$Name`" to $($iObject.name)" -ForegroundColor Red
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
.PARAMETER Force
Perform the operation without prompting for confirmation. By default, this function will always prompt for confirmation unless -Confirm:$false or -Force is specified, or $ConfirmPreference is set to None.
.PARAMETER Object
The DNS Host object(s) to grant the DNS Config Profile to
.EXAMPLE
PS> Grant-B1DNSConfigProfile -Name "Data Centre" -Hosts "bloxoneddihost1.mydomain.corp","bloxoneddihost2.mydomain.corp"
Expand All @@ -32,30 +35,54 @@
param(
[Parameter(Mandatory=$true)]
[String]$Name,
[Parameter(Mandatory=$true)]
[System.Object]$Hosts,
[Switch]$Force
[Parameter(ParameterSetName="Default",Mandatory=$true)]
[String[]]$Hosts,
[Parameter(
ValueFromPipeline = $true,
ParameterSetName="Object",
Mandatory=$true
)]
[System.Object]$Object,
[Switch]$Force
)
$ConfirmPreference = Confirm-ShouldProcess $PSBoundParameters
$DNSConfigProfileId = (Get-B1DNSConfigProfile -Name $Name -Strict).id
if (!$DNSConfigProfileId) {
Write-Error "Failed to get DNS Config Profile."
break
} else {
foreach ($DNSHost in $Hosts) {
$DNSHostId = (Get-B1DNSHost -Name $DNSHost).id

$splat = @{
"server" = $DNSConfigProfileId

process {
$ConfirmPreference = Confirm-ShouldProcess $PSBoundParameters
$Objects = @()
if ($Object) {
$SplitID = $Object.id.split('/')
if (("$($SplitID[0])/$($SplitID[1])") -ne "dns/host") {
Write-Error "Error. Unsupported pipeline object. This function only supports 'dns/host' objects as input"
return $null
} else {
$Objects += $Object
}
} else {
foreach ($DNSHost in $Hosts) {
$Objects += Get-B1DNSHost -Name $DNSHost -Strict
}

$splat = $splat | ConvertTo-Json

$Result = Invoke-CSP -Method "PATCH" -Uri "$DNSHostId" -Data $splat | Select-Object -ExpandProperty result -ErrorAction SilentlyContinue
if ($Result.server -eq $DNSConfigProfileId) {
Write-Host "DNS Config Profile `"$Name`" has been successfully applied to $DNSHost" -ForegroundColor Green
} else {
Write-Host "Failed to apply DNS Config Profile `"$Name`" to $DNSHost" -ForegroundColor Red
}

$DNSConfigProfileId = (Get-B1DNSConfigProfile -Name $Name -Strict).id
if (!$DNSConfigProfileId) {
Write-Error "Failed to get DNS Config Profile."
break
} else {
foreach ($iObject in $Objects) {
$splat = @{
"server" = $DNSConfigProfileId
}

$splat = $splat | ConvertTo-Json

if($PSCmdlet.ShouldProcess("Assign DNS Config Profile: $($Name) to Host: $($iObject.name)","Assign DNS Config Profile: $($Name) to Host: $($iObject.name)",$MyInvocation.MyCommand)){
$Result = Invoke-CSP -Method "PATCH" -Uri "$(Get-B1CSPUrl)/api/ddi/v1/$($iObject.id)" -Data $splat | Select-Object -ExpandProperty result -ErrorAction SilentlyContinue
if ($Result.server -eq $DNSConfigProfileId) {
Write-Host "DNS Config Profile `"$Name`" has been successfully applied to: $($iObject.name)" -ForegroundColor Green
} else {
Write-Host "Failed to apply DNS Config Profile `"$Name`" to: $($iObject.name)" -ForegroundColor Red
}
}
}
}
}
Expand Down
34 changes: 21 additions & 13 deletions Modules/ibPS/Functions/BloxOne/BloxOneDDI/New-B1AddressBlock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
.PARAMETER Compartment
The name of the compartment to assign to this address block
.PARAMETER Force
Perform the operation without prompting for confirmation. By default, this function will not prompt for confirmation unless $ConfirmPreference is set to Medium.
.PARAMETER Tags
Any tags you want to apply to the address block
Expand All @@ -46,6 +49,10 @@
.FUNCTIONALITY
IPAM
#>
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = 'Medium'
)]
param(
[Parameter(Mandatory=$true)]
[String]$Subnet,
Expand All @@ -60,16 +67,15 @@
[System.Object]$DHCPOptions,
[String]$DDNSDomain,
[String]$Compartment,
[System.Object]$Tags
)

[System.Object]$Tags,
[Switch]$Force
)
$ConfirmPreference = Confirm-ShouldProcess $PSBoundParameters
$SpaceUUID = (Get-B1Space -Name $Space -Strict).id

if (Get-B1AddressBlock -Subnet $Subnet -Space $Space -CIDR $CIDR) {
Write-Host "The Address Block $Subnet/$CIDR already exists." -ForegroundColor Yellow
} else {
Write-Host "Creating Address Block $Subnet/$CIDR..." -ForegroundColor Gray

$splat = @{
"space" = $SpaceUUID
"address" = $Subnet
Expand Down Expand Up @@ -105,14 +111,16 @@

$splat = $splat | ConvertTo-Json -Depth 4

$Result = Invoke-CSP -Method POST -Uri "ipam/address_block" -Data $splat | Select-Object -ExpandProperty result -ErrorAction SilentlyContinue

if ($Result.address -eq $Subnet) {
Write-Host "Address Block $Subnet/$CIDR created successfully." -ForegroundColor Green
return $Result
} else {
Write-Host "Failed to create Address Block $Subnet." -ForegroundColor Red
break
if($PSCmdlet.ShouldProcess("Create new Address Block:`n$($splat)","Create new Address Block: $($Name)",$MyInvocation.MyCommand)){
Write-Host "Creating Address Block $Subnet/$CIDR..." -ForegroundColor Gray
$Result = Invoke-CSP -Method POST -Uri "$(Get-B1CSPUrl)/api/ddi/v1/ipam/address_block" -Data $splat | Select-Object -ExpandProperty result -ErrorAction SilentlyContinue
if ($Result.address -eq $Subnet) {
Write-Host "Address Block $Subnet/$CIDR created successfully." -ForegroundColor Green
return $Result
} else {
Write-Host "Failed to create Address Block $Subnet." -ForegroundColor Red
break
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
.PARAMETER Tags
Any tags you want to apply to the address reservation
.PARAMETER Force
Perform the operation without prompting for confirmation. By default, this function will not prompt for confirmation unless $ConfirmPreference is set to Medium.
.PARAMETER Force
Perform the operation without prompting for confirmation. By default, this function will not prompt for confirmation unless $ConfirmPreference is set to Medium.
.EXAMPLE
PS> New-B1AddressReservation -Address "10.0.0.1" -Name "MyReservedHost" -Description "My Reserved Host" -Space "Global"
Expand All @@ -30,6 +36,10 @@
.FUNCTIONALITY
IPAM
#>
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = 'Medium'
)]
param(
[Parameter(Mandatory=$true)]
[String]$Address,
Expand All @@ -39,8 +49,10 @@
[String]$Description,
[Parameter(Mandatory=$true)]
[String]$Space,
[System.Object]$Tags = $null
[System.Object]$Tags,
[Switch]$Force
)
$ConfirmPreference = Confirm-ShouldProcess $PSBoundParameters
if (!(Get-B1Address -Address $Address -Reserved)) {
$splat = @{
"space" = (Get-B1Space -Name $Space -Strict).id
Expand All @@ -53,14 +65,16 @@
"tags" = $Tags
}
$splat = ConvertTo-Json($splat) -Depth 2
$Result = Invoke-CSP -Method "POST" -Uri "ipam/address" -Data $splat

if (($Result | Select-Object -ExpandProperty result).address -eq $Address) {
Write-Host "Address Reservation created successfully." -ForegroundColor Green
return $Result | Select-Object -ExpandProperty result
} else {
Write-Host "Error. Failed to create Address Reservation $Subnet." -ForegroundColor Red
break
if($PSCmdlet.ShouldProcess("Create new Address Reservation:`n$($splat)","Create new Address Reservation: $($Name)",$MyInvocation.MyCommand)){
$Result = Invoke-CSP -Method "POST" -Uri "$(Get-B1CSPUrl)/api/ddi/v1/ipam/address" -Data $splat
if (($Result | Select-Object -ExpandProperty result).address -eq $Address) {
Write-Host "Address Reservation created successfully." -ForegroundColor Green
return $Result | Select-Object -ExpandProperty result
} else {
Write-Host "Error. Failed to create Address Reservation $Subnet." -ForegroundColor Red
break
}
}
} else {
Write-Host "Address already exists." -ForegroundColor Red
Expand Down
Loading

0 comments on commit 7fa8206

Please sign in to comment.