rmbolger/Posh-IBWAPI

Updating attributes

jddemcher opened this issue · 2 comments

When updating extensible attributes with the -Template parameter, I had to make sure that I included any existing attributes on the record when adding new fields or else they would get removed from the attribute table.

Example:

$obj = Get-IBObject -ObjectType network -ReturnFields 'extattrs' -Filters 'network=10.0.0.0*'
$json = @{
extattrs = @{
'FieldA' = @{value = 'Abc'}
'FieldB' = @{value = '123'}
'FieldC' = @{value = 'Def'}
}
}
$obj | Set-IBObject -TemplateObject $json

Say I only wanted to update FieldC, I'd still need to include the existing values for Field's A and B or else they'd get removed.

Unfortunately, that's a (rather annoying) limitation in the WAPI specifically for extensible attributes. Infoblox seems to treat the extattrs property collection as a single unit when updating an existing object. As far as I know, there's no way to update a single extensible attribute value without effectively copying the other ones to the template object first or just modifying a copy of the existing object.

So in your case, I'd probably do something like this instead:

$obj = Get-IBObject -ObjectType network -ReturnFields 'extattrs' -Filters 'network=10.0.0.0*'
$obj | ForEach-Object {
    $_.extattrs.FieldA.value = 'Abc'
    $_.extattrs.FieldB.value = '123'
    $_.extattrs.FieldC.value = 'Def'
    Set-IBObject -IBObject $_
}

If it's possible that some of your network objects don't already have one of those attributes defined, you'd want to check for that and explicitly add them to the object first. It might look something like this:

$obj = Get-IBObject -ObjectType network -ReturnFields 'extattrs' -Filters 'network=10.0.0.0*'
$obj | ForEach-Object {
    if (-not $_.extattrs.FieldA) { $_.extattrs | Add-Member 'FieldA' @{value=$null} }
    if (-not $_.extattrs.FieldB) { $_.extattrs | Add-Member 'FieldB' @{value=$null} }
    if (-not $_.extattrs.FieldC) { $_.extattrs | Add-Member 'FieldC' @{value=$null} }
    $_.extattrs.FieldA.value = 'Abc'
    $_.extattrs.FieldB.value = '123'
    $_.extattrs.FieldC.value = 'Def'
    Set-IBObject -IBObject $_
}

thanks! this helps a lot!