Create vCAC ManagementModelEntites objects from ODATA REST call with PowerShell

As quickly described in a previous post, the ‘AddQueryOption()’ in the vCAC WCF DataService is flawed and does not allow you to narrow queries and thus leads to massive performance penalties when working with larger installations.

However, there is a workaround for this: you can use native ODATA REST calls and convert the result into a native .NET vCAC ManagementModelEntities object. And this is how it works:

1a. You invoke a query against an id/guid of an entity set
— or —
1b. You invoke a query with a filter expression against a property (or combination of properties)
2. the result is parsed and every property is then copied into a native vCAC object
3. at the end you attach the object to the entity tracker and you are ready to go

(You could also use this approach to query with the ‘$expand’ option to circumvent the shortcoming of PowerShell when working with lambda expressions. With that you can query an object and return its associated (linked) properties in one single query without the need to call ‘LoadProperty()’ after is several times.)

See Gist at https://gist.github.com/dfch/301d330d961b93ffc37e

function Get-VcacEntity {
[CmdletBinding(
  SupportsShouldProcess = $false
  ,
    ConfirmImpact = 'Low'
  ,
  DefaultParameterSetName = 'guid'
  ,
  HelpURI='http://dfch.biz/PS/Vcac/Utilities/Get-VcacEntity/'
)]
PARAM (
  [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'guid')]
  [guid] $Guid
  ,
  [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'id')]
  [int] $id
  ,
  [Parameter(Mandatory = $false, ParameterSetName = 'filter')]
  [string] $FilterProperty
  ,
  [Parameter(Mandatory = $true, ParameterSetName = 'filter')]
  [Alias('Name')]
  [string] $FilterValue
  ,
  [ValidateSet('eq', 'ne', 'gt', 'ge', 'lt', 'le')]
  [Parameter(Mandatory = $false, ParameterSetName = 'filter')]
  [string] $FilterOperand = 'eq'
  ,
  [Parameter(Mandatory = $false)]
  [String] $Type = 'DynamicOps.ManagementModel.VirtualMachine'
  ,
  [Parameter(Mandatory = $false)]
  [Switch] $ReturnExistingEntity = $true
  ,
  [Parameter(Mandatory = $false, ParameterSetName = 'list')]
  [Switch] $ListAvailable = $true
  ,
  [Parameter(Mandatory = $false, ParameterSetName = 'ListEntityType')]
  [String] $ListEntityType = 'DynamicOps.ManagementModel.VirtualMachine'
  ,
  [DynamicOps.ManagementModel.ManagementModelEntities] $m = $biz_dfch_PS_vCAC_Utilities.mgmtContext
)

BEGIN {

  Set-Variable gotoSuccess -Option 'Constant' -Value 'biz.dfch.System.Exception.gotoSuccess' -Confirm:$false -WhatIf:$false;
  Set-Variable gotoError -Option 'Constant' -Value 'biz.dfch.System.Exception.gotoError' -Confirm:$false -WhatIf:$false;
  Set-Variable gotoFailure -Option 'Constant' -Value 'biz.dfch.System.Exception.gotoFailure' -Confirm:$false -WhatIf:$false;
  Set-Variable gotoNotFound -Option 'Constant' -Value 'biz.dfch.System.Exception.gotoNotFound' -Confirm:$false -WhatIf:$false;

  $datBegin = [datetime]::Now;
  [string] $fn = $MyInvocation.MyCommand.Name;
  Write-Host ("CALL. MgmtContext '{0}'. Guid '{1}'. id '{2}'. FilterProperty '{3}'. FilterValue '{4}'. Type '{5}'. ParameterSetName: '{6}'." -f ($m -is [Object]), $Guid, $id, $FilterProperty, $FilterValue, $Type, $PsCmdlet.ParameterSetName) -fac 1;

} # BEGIN
PROCESS {

# Default test variable for checking function response codes.
[Boolean] $fReturn = $false;
# Return values are always and only returned via OutputParameter.
$OutputParameter = $null;

try {

  if($PSCmdlet.ParameterSetName -eq 'list') {
    $aDefinition = ($m | gm -Type Properties).Definition;
    foreach($Definition in $aDefinition) {
      $fReturn = $Definition -match 'DynamicOps\.Repository\.RepositoryServiceQuery\[(?<Type>[^\]]+)\]';
      if(!$fReturn) { continue; }
      $OutputParameter += ("{0}`n" -f $Matches.Type);
      $OutputParameter = $OutputParameter | Sort;
    } # foreach
    throw($gotoSuccess);
  } elseif($PSCmdlet.ParameterSetName -eq 'ListEntityType') {
    $entity = New-Object $ListEntityType;
    $fReturn = $true;
    $OutputParameter = $entity;
    throw($gotoSuccess);
  } # if

  # Create new entity
  $NewVcacEntity = New-Object $Type;
  if(!$NewVcacEntity) { Write-Error ("Invalid object type: '{0}'." -f $Type) }
  # Define default property name if not specified
  if(!$FilterProperty) { $FilterProperty = '{0}Name' -f $NewVcacEntity.GetType().Name; }
  # Construct Uri
  $ServerUri = $biz_dfch_PS_vCAC_Utilities.ServerUri;
  switch($PSCmdlet.ParameterSetName) {
    'id' { [Uri] $Uri = "{0}/{1}({2})" -f $ServerUri, $m.GetEntitySetFromType($Type), $id; }
    'guid' { [Uri] $Uri = "{0}/{1}(guid'{2}')" -f $ServerUri, $m.GetEntitySetFromType($Type), $Guid; }
    'filter' { [Uri] $Uri = "{0}/{1}?`$filter={2} {3} '{4}'" -f $ServerUri, $m.GetEntitySetFromType($Type), $FilterProperty, $FilterOperand, $FilterValue; }
    default { Write-Error 'Invalid ParameterSetName.' }
  } # switch

  Write-Host ("Invoking Uri '{0}' ..." -f $Uri);
  if($m.Credentials.UserName) {
    $r = Invoke-RestMethod $Uri -Credential $m.Credentials;
  } else {
    $r = Invoke-RestMethod $Uri -UseDefaultCredentials;
  } # if
  # Convert from XmlDocument or XmlLinkedNode
  if($r.entry) { $x = $r.entry; } else { $x = $r; }

  foreach($Entity in $m.Entities) {
    if($x.id -ne $Entity.Identity) { continue; }
    if($ReturnExistingEntity) {
      Write-Host ("Entity '{0}' [{1}] found in entity tracker. Returning existing entity ..." -f $Entity.Identity, $NewVcacEntity.GetType().Name);
      $NewVcacEntity = $Entity.Entity;
      $m.AttachIfNeeded($m.GetEntitySetFromType($NewVcacEntity.GetType()), $NewVcacEntity);
      $OutputParameter = $NewVcacEntity;
      throw($gotoSuccess);
    } else {
      Write-Host ("Entity '{0}' [{1}] found in entity tracker. Removing existing entity ..." -f $Entity.Identity, $NewVcacEntity.GetType().Name);
      $fReturn = $m.Detach($Entity.Entity);
      break;
    } # if
  } # foreach

  # Convert returned EF xml entities to .NET data types
  foreach($p in $x.content.properties.GetEnumerator()) { 
    if($p.localname -eq '#whitespace') { continue; }
    switch($p.type) {
    'Edm.Int32' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
    'Edm.Int64' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
    'Edm.Decimal' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
    'Edm.Boolean' { if($p.null -ne 'true') { if($p.'#text' -eq 'true') { $NewVcacEntity.($p.localname) = $true; } else { $NewVcacEntity.($p.localname) = $false; } } }
    'Edm.Guid' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
    'Edm.DateTime' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
    'Edm.Byte' { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
    default { if($p.null -ne 'true') { $NewVcacEntity.($p.localname) = $p.'#text'; } }
    } # switch
  } # foreach

  # Attach object to entity tracker
  $m.AttachIfNeeded($m.GetEntitySetFromType($NewVcacEntity.GetType()), $NewVcacEntity);
  # Optional: if you had used an expand expression you could also load the returned links to that entity
  # foreach($l in $r.entry.link.GetEnumerator()) { 
    # if(!$l.inline) { continue; }
    # $null = $m.LoadProperty($NewVcacEntity, $l.title);
  # } # foreach

  $fReturn = $true;
  $OutputParameter = $NewVcacEntity
} # try
catch {
  # ... do sth useful
} # catch
finally {
  # Clean up
} # finally

} # PROCESS

END {

$datEnd = [datetime]::Now;
Write-Host ("RET. fReturn: [{0}]. Execution time: [{1}]ms. Started: [{2}]." -f $fReturn, ($datEnd - $datBegin).TotalMilliseconds, $datBegin.ToString('yyyy-MM-dd HH:mm:ss.fffzzz')) -fac 2;

return $OutputParameter;
} # END

} # function
Export-ModuleMember -Function Get-VcacEntity;

As usual: most error checking has been removed and here only one property can be filtered.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: