Skip to content

Commit

Permalink
1.12.0-Beta1 - Recover, Deleted and 429 fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
homotechsual committed Oct 2, 2022
1 parent 062463d commit 46b4009
Show file tree
Hide file tree
Showing 36 changed files with 483 additions and 152 deletions.
8 changes: 5 additions & 3 deletions HaloAPI.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = '.\HaloAPI.psm1'

# Version number of this module.
ModuleVersion = '1.11.0'
ModuleVersion = '1.12.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -183,6 +183,8 @@
'Remove-HaloCRMNote',
'Remove-HaloTicket',
'Remove-HaloTicketBatch',
'Remove-HaloUser',
'Restore-HaloTicket',
'Set-HaloAction',
'Set-HaloAgent',
'Set-HaloAppointment',
Expand Down Expand Up @@ -255,10 +257,10 @@
IconUri = 'https://3c3br937rz386088k2z3qqdi-wpengine.netdna-ssl.com/wp-content/uploads/2020/04/HaloIcon-300x300.png'

# ReleaseNotes of this module
ReleaseNotes = 'Fix for PostedOnly and NonPostedOnly parameters on Get-HaloInvoice.'
ReleaseNotes = 'Adds Recover-HaloTicket commandlet and ability to retrieve deleted tickets with Get-HaloTicket -Deleted. Fix for 429 rate limit responses.'

# Prerelease string of this module
Prerelease = 'Beta2'
Prerelease = 'Beta1'

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenceAcceptance = $false
Expand Down
11 changes: 9 additions & 2 deletions PSScriptAnalyzerSettings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
'Information'
)
IncludeRules = @(
'PSAlignAssignmentStatement',
'PSAvoidAssignmentToAutomaticVariable',
'PSAvoidDefaultValueForMandatoryParameter',
'PSAvoidDefaultValueSwitchParameter',
'PSAvoidGlobalAliases',
'PSAvoidGlobalVars',
'PSAvoidUsingCmdletAliases',
'PSAvoidUsingComputerNameHardcoded',
Expand All @@ -24,6 +28,7 @@
'PSAvoidUsingUserNameAndPasswordParams',
'PSAvoidUsingWMICmdlet',
'PSAvoidUsingWriteHost',
'PSAvoidUsingWMICmdlet',
'PSDSC*',
'PSMissingModuleManifestField',
'PSPlaceCloseBrace',
Expand All @@ -39,8 +44,7 @@
'PSUseCorrectCasing',
'PSUseDeclaredVarsMoreThanAssignments',
'PSUsePSCredentialType',
'PSUseShouldProcessForStateChangingFunctions',
'PSUseSingularNouns'
'PSUseShouldProcessForStateChangingFunctions'
)
Rules = @{
PSPlaceOpenBrace = @{
Expand Down Expand Up @@ -85,5 +89,8 @@
Enable = $true
}

PSAlignAssignmentStatement = @{
Enable = $false
}
}
}
5 changes: 4 additions & 1 deletion Public/Get/Get-HaloTicket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ function Get-HaloTicket {
[Switch]$IncludeDetails,
# Include the last action in the result.
[Parameter( ParameterSetName = 'Single' )]
[Switch]$IncludeLastAction
[Switch]$IncludeLastAction,
# Return deleted tickets only.
[Parameter( ParameterSetName = 'Multi' )]
[Switch]$Deleted
)
Invoke-HaloPreFlightCheck
$CommandName = $MyInvocation.MyCommand.Name
Expand Down
51 changes: 33 additions & 18 deletions Public/Invoke-HaloRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,40 @@ function Invoke-HaloRequest {
} else {
$RequestHeaders = $null
}
try {
Write-Verbose "Making a $($WebRequestParams.Method) request to $($WebRequestParams.Uri)"
$Response = Invoke-WebRequest @WebRequestParams -Headers $RequestHeaders -ContentType 'application/json; charset=utf-8'
Write-Debug "Response headers: $($Response.Headers | Out-String)"
Write-Debug "Raw Response: $Response"
if ($Response.StatusCode.__value -eq 429) {
do {
Write-Warning 'The request was throttled, waiting for 1 second.'
Start-Sleep -Seconds 1
$Response = Invoke-WebRequest @WebRequestParams -Headers $RequestHeaders -ContentType 'application/json; charset=utf-8'
} while ($Response.StatusCode.__value -eq 429)
$Retries = 0
do {
$Retries++
$Results = try {
Write-Verbose "Making a $($WebRequestParams.Method) request to $($WebRequestParams.Uri)"
$Response = Invoke-WebRequest @WebRequestParams -Headers $RequestHeaders -ContentType 'application/json; charset=utf-8'
Write-Debug "Response headers: $($Response.Headers | Out-String)"
Write-Debug "Raw Response: $Response"
$Success = $True
if ($RawResult) {
$Results = $Response
} else {
$Results = $Response.Content | ConvertFrom-Json
}
Return $Results
} catch [Microsoft.PowerShell.Commands.HttpResponseException] {
$Success = $False
if ($_.Exception.Response.StatusCode.__value -eq 429) {
Write-Warning 'The request was throttled, waiting for 5 seconds.'
Start-Sleep -Seconds 5
continue
} else {
$_
break
}
} catch {
throw $_
}
if ($RawResult) {
$Results = $Response
} else {
$Results = $Response.Content | ConvertFrom-Json
} while ((-not $Results) -and ($Retries -lt 10) -and (-not $Success))
if ($Results) {
Return $Results
} else {
if ($Retries -gt 1) {
throw ('Retried request {0} times, request unsuccessful.') -f $Retries
}
return $Results
} catch {
throw $_
}
}
33 changes: 33 additions & 0 deletions Public/Remove/Remove-HaloUser.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function Remove-HaloUser {
<#
.SYNOPSIS
Removes a user from the Halo API.
.DESCRIPTION
Deletes a specific user from Halo.
.OUTPUTS
A powershell object containing the response.
#>
[cmdletbinding( SupportsShouldProcess = $True, ConfirmImpact = 'High' )]
[OutputType([Object])]
Param(
# The Ticket ID
[Parameter( Mandatory = $True )]
[Alias('User')]
[int64]$UserId
)
Invoke-HaloPreFlightCheck
try {
$ObjectToDelete = Get-HaloUser -UserId $UserId
if ($ObjectToDelete) {
if ($PSCmdlet.ShouldProcess("User '$($ObjectToDelete.name)')'", 'Delete')) {
$Resource = "api/users/$($UserId)"
$UserResults = New-HaloDELETERequest -Resource $Resource
Return $UserResults
}
} else {
Throw 'User was not found in Halo to delete.'
}
} catch {
New-HaloError -ErrorRecord $_
}
}
36 changes: 36 additions & 0 deletions Public/Restore/Restore-HaloTicket.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function Restore-HaloTicket {
<#
.SYNOPSIS
Restores a ticket using the Halo API.
.DESCRIPTION
Restores a specific ticket or array of tickets from Halo.
.OUTPUTS
A powershell object containing the response.
#>
[cmdletbinding( SupportsShouldProcess = $True, ConfirmImpact = 'High' )]
[OutputType([Object])]
Param(
# The Ticket id or array of ticket ids.
[Parameter( Mandatory = $True )]
[Alias('Ticket')]
[int64[]]$TicketId
)
Invoke-HaloPreFlightCheck
try {
$Tickets = [System.Collections.Generic.List[Object]]
$TicketId | ForEach-Object {
$Tickets.Add(
@{
id = $_.id
_recover = $True
_validate_updates = $True
}
)
}
if ($PSCmdlet.ShouldProcess($Tickets -is [Array] ? 'Tickets' : 'Ticket', 'Update')) {
Set-HaloTicket -Object $Tickets
}
} catch {
New-HaloError -ErrorRecord $_
}
}
18 changes: 13 additions & 5 deletions Public/Set/Set-HaloAction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ Function Set-HaloAction {
Param (
# Object or array of objects containing properties and values used to update one or more existing actions.
[Parameter( Mandatory = $True, ValueFromPipeline = $True )]
[Object[]]$Action
[Object[]]$Action,
# Skip validation checks.
[Parameter()]
[Switch]$SkipValidation
)
Invoke-HaloPreFlightCheck
try {
Expand All @@ -27,11 +30,16 @@ Function Set-HaloAction {
ActionID = ($_.id)
TicketID = ($_.ticket_id)
}
$ActionExists = Get-HaloAction @HaloActionParams
if ($ActionExists) {
Return $True
if (-not $SkipValidation) {
$ActionExists = Get-HaloAction @HaloActionParams
if ($ActionExists) {
Return $True
} else {
Return $False
}
} else {
Return $False
Write-Verbose 'Skipping validation checks.'
Return $True
}
}
if ($False -notin $ObjectsToUpdate) {
Expand Down
18 changes: 13 additions & 5 deletions Public/Set/Set-HaloAgent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ Function Set-HaloAgent {
Param (
# Object or array of objects containing properties and values used to update one or more existing agents.
[Parameter( Mandatory = $True, ValueFromPipeline )]
[Object[]]$Agent
[Object[]]$Agent,
# Skip validation checks.
[Parameter()]
[Switch]$SkipValidation
)
Invoke-HaloPreFlightCheck
try {
Expand All @@ -23,11 +26,16 @@ Function Set-HaloAgent {
$HaloAgentParams = @{
AgentId = $_.id
}
$AgentExists = Get-HaloAgent @HaloAgentParams
if ($AgentExists) {
Return $True
if (-not $SkipValidation) {
$AgentExists = Get-HaloAgent @HaloAgentParams
if ($AgentExists) {
Return $True
} else {
Return $False
}
} else {
Return $False
Write-Verbose 'Skipping validation checks.'
Return $True
}
}
if ($False -notin $ObjectToUpdate) {
Expand Down
18 changes: 13 additions & 5 deletions Public/Set/Set-HaloAppointment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ Function Set-HaloAppointment {
Param (
# Object or array of objects containing properties and values used to update one or more existing appointments.
[Parameter( Mandatory = $True, ValueFromPipeline )]
[Object[]]$Appointment
[Object[]]$Appointment,
# Skip validation checks.
[Parameter()]
[Switch]$SkipValidation
)
Invoke-HaloPreFlightCheck
try {
Expand All @@ -23,11 +26,16 @@ Function Set-HaloAppointment {
$HaloAppointmentParams = @{
AppointmentId = ($_.id)
}
$AppointmentExists = Get-HaloAppointment @HaloAppointmentParams
if ($AppointmentExists) {
Return $True
if (-not $SkipValidation) {
$AppointmentExists = Get-HaloAppointment @HaloAppointmentParams
if ($AppointmentExists) {
Return $True
} else {
Return $False
}
} else {
Return $False
Write-Verbose 'Skipping validation checks.'
Return $True
}
}
if ($False -notin $ObjectToUpdate) {
Expand Down
18 changes: 13 additions & 5 deletions Public/Set/Set-HaloAsset.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ Function Set-HaloAsset {
Param (
# Object or array of objects containing properties and values used to update one or more existing assets.
[Parameter( Mandatory = $True, ValueFromPipeline )]
[Object[]]$Asset
[Object[]]$Asset,
# Skip validation checks.
[Parameter()]
[Switch]$SkipValidation
)
Invoke-HaloPreFlightCheck
try {
Expand All @@ -23,11 +26,16 @@ Function Set-HaloAsset {
$HaloAssetParams = @{
AssetId = ($_.id)
}
$AssetExists = Get-HaloAsset @HaloAssetParams
if ($AssetExists) {
Return $True
if (-not $SkipValidation) {
$AssetExists = Get-HaloAsset @HaloAssetParams
if ($AssetExists) {
Return $True
} else {
Return $False
}
} else {
Return $False
Write-Verbose 'Skipping validation checks.'
Return $True
}
}
if ($False -notin $ObjectToUpdate) {
Expand Down
14 changes: 11 additions & 3 deletions Public/Set/Set-HaloAssetType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ Function Set-HaloAssetType {
Param (
# Object containing properties and values used to update an existing asset type.
[Parameter( Mandatory = $True, ValueFromPipeline )]
[Object]$AssetType
[Object]$AssetType,
# Skip validation checks.
[Parameter()]
[Switch]$SkipValidation
)
Invoke-HaloPreFlightCheck
try {
if ($null -eq $AssetType.id) {
throw 'Asset type ID is required.'
}
$ObjectToUpdate = Get-HaloAssetType -AssetTypeID $AssetType.id
if (-not $SkipValidation) {
$ObjectToUpdate = Get-HaloAssetType -AssetTypeID $AssetType.id
} else {
Write-Verbose 'Skipping validation checks.'
$ObjectToUpdate = $True
}
if ($ObjectToUpdate) {
if ($PSCmdlet.ShouldProcess("Asset Type '$($ObjectToUpdate.name)'", 'Update')) {
if ($PSCmdlet.ShouldProcess('Asset Type', 'Update')) {
New-HaloPOSTRequest -Object $AssetType -Endpoint 'assettype' -Update
}
} else {
Expand Down
Loading

0 comments on commit 46b4009

Please sign in to comment.