Skip to content

Commit

Permalink
Merge pull request #52 from greenlighttec/master
Browse files Browse the repository at this point in the history
Added validation for Get/Set Assets modules
  • Loading branch information
greenlighttec authored Jun 5, 2024
2 parents e7ac6c7 + 1082b6a commit 19dcab4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
3 changes: 3 additions & 0 deletions HuduAPI/Public/Get-HuduAssets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ function Get-HuduAssets {
#>
[CmdletBinding()]
Param (
[ValidateRange(1, [int]::MaxValue)]
[Int]$Id = '',
[Alias('asset_layout_id')]
[ValidateRange(1, [int]::MaxValue)]
[Int]$AssetLayoutId = '',
[string]$AssetLayout,
[Alias('company_id')]
[ValidateRange(1, [int]::MaxValue)]
[Int]$CompanyId = '',
[String]$Name = '',
[switch]$Archived,
Expand Down
84 changes: 45 additions & 39 deletions HuduAPI/Public/Set-HuduAsset.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ function Set-HuduAsset {

[Alias('asset_id','assetid')]
[Parameter(Mandatory = $true)]
[ValidateRange(1, [int]::MaxValue)]
[Int]$Id,

[Alias('primary_serial')]
[string]$PrimarySerial,

Expand All @@ -72,45 +73,50 @@ function Set-HuduAsset {

[string]$Slug
)

$Object = Get-HuduAssets -id $Id | Select-Object name,asset_layout_id,company_id,slug,primary_serial,primary_model,primary_mail,id,primary_manufacturer,@{n='custom_fields';e={$_.fields | ForEach-Object {[pscustomobject]@{$_.label.replace(' ','_').tolower()= $_.value}}}}
$Asset = [ordered]@{asset = $Object }
$CompanyId = $Object.company_id

if ($Name) {
$Asset.asset.name = $Name
}

if ($AssetLayoutId) {
$Asset.asset.asset_layout_id = $AssetLayoutId
}
if ($Object) {
$Asset = [ordered]@{asset = $Object }
$CompanyId = $Object.company_id

if ($PrimarySerial) {
$Asset.asset.primary_serial = $PrimarySerial
}

if ($PrimaryMail) {
$Asset.asset.primary_mail = $PrimaryMail
}

if ($PrimaryModel) {
$Asset.asset.primary_model = $PrimaryModel
}

if ($PrimaryManufacturer) {
$Asset.asset.primary_manufacturer = $PrimaryManufacturer
}

if ($Fields) {
$Asset.asset.custom_fields = $Fields
}

if ($Slug) {
$Asset.asset.slug = $Slug
}

$JSON = $Asset | ConvertTo-Json -Depth 10

if ($PSCmdlet.ShouldProcess($ArticleId)) {
Invoke-HuduRequest -Method put -Resource "/api/v1/companies/$CompanyId/assets/$Id" -Body $JSON
if ($Name) {
$Asset.asset.name = $Name
}

if ($AssetLayoutId) {
$Asset.asset.asset_layout_id = $AssetLayoutId
}

if ($PrimarySerial) {
$Asset.asset.primary_serial = $PrimarySerial
}

if ($PrimaryMail) {
$Asset.asset.primary_mail = $PrimaryMail
}

if ($PrimaryModel) {
$Asset.asset.primary_model = $PrimaryModel
}

if ($PrimaryManufacturer) {
$Asset.asset.primary_manufacturer = $PrimaryManufacturer
}

if ($Fields) {
$Asset.asset.custom_fields = $Fields
}

if ($Slug) {
$Asset.asset.slug = $Slug
}

$JSON = $Asset | ConvertTo-Json -Depth 10

if ($PSCmdlet.ShouldProcess("ID: $($Asset.id) Name: $($Asset.Name)", "Set Hudu Asset")) {
Invoke-HuduRequest -Method put -Resource "/api/v1/companies/$CompanyId/assets/$Id" -Body $JSON
}
} else {
throw "A valid asset could not be found to update, please double check the ID and try again"
}
}

0 comments on commit 19dcab4

Please sign in to comment.