Ps Object Meta Data

Problem this snippet solves:

This PowerShell library will allow you to build a metadata database of information associated with objects on the system. This example uses iControl to allow you to assign metadata like Owner and Location to an object such as a VIP and then be able to query it with iControl or have access to it from within an iRule with the class command.

Code :

#----------------------------------------------------------------------------
# The contents of this file are subject to the "END USER LICENSE AGREEMENT FOR F5
# Software Development Kit for iControl"; you may not use this file except in
# compliance with the License. The License is included in the iControl
# Software Development Kit.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is iControl Code and related documentation
# distributed by F5.
#
# The Initial Developer of the Original Code is F5 Networks,
# Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2009 F5 Networks,
# Inc. All Rights Reserved.  iControl (TM) is a registered trademark of F5 Networks, Inc.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above.  If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#----------------------------------------------------------------------------
$script:DataGroup = "ObjectMetaData";
$script:Separator = ':';

#-------------------------------------------------------------------------
function New-F5.DataObject()
#-------------------------------------------------------------------------
{
  param(
    $Product = $null,
    $Type = $null,
    $Name = $null,
    $Key = $null,
    $Value = $null
  );
  if ( ($Product -eq $null) -or ($Type -eq $null) -or ($Name -eq $null) -or ($Key -eq $null) -or ($Value -eq $null) )
  {
    Write-Host "Usage: New-F5.DataObject -Product prod -Type type -Name objname -Key datakey -Value datavalue";
    return;
  }
  
  $o = 1 | select Product, Type, Name, Key, Value;
  $o.Product = $Product;
  $o.Type = $Type;
  $o.Name = $Name;
  $o.Key = $Key;
  $o.Value = $Value;
  $o;
}

#-------------------------------------------------------------------------
function Set-F5.MetaDataClass()
#-------------------------------------------------------------------------
{
  param($Class = $null);
  if ( $null -eq $Class )
  {
    Write-Host "Usage: Set-F5.MetaDataClass -Class classname";
    return;
  }
  
  $StringClass = New-Object -TypeName iControl.LocalLBClassStringClass;
  $StringClass.name = $Class
  $StringClass.members = (,$null);
  
  trap {
    if ( $_.Exception.Message.Contains("already exists") ) {
      # Class member already exists, so eat the error.
      $script:DataGroup = $Class;
      continue;
    }
  }
  (Get-F5.iControl).LocalLBClass.create_string_class( (,$StringClass) );
  
  $script:DataGroup = $Class;
}

#-------------------------------------------------------------------------
function Get-F5.MetaDataClass()
#-------------------------------------------------------------------------
{
  $script:DataGroup;
}

#-------------------------------------------------------------------------
function Get-F5.MetaData()
#-------------------------------------------------------------------------
{
  param(
    $Product = $null,
    $Type = $null,
    $Name = $null,
    $Key = $null,
    $Value = $null
  );
  
  $Objs = @();
  # Build list
  
  $StringClassA = (Get-F5.iControl).LocalLBClass.get_string_class( (,$script:Datagroup) );
  $StringClass = $StringClassA[0];
  
  $classname = $StringClass.name;
  $members = $StringClass.members;
  
  for($i=0; $i -lt $members.length; $i++)
  {
    $tokens = $members[$i].Split($Separator);
    if ( $tokens.Length -eq 5 )
    {
      $oProd = $tokens[0];
      $oType = $tokens[1];
      $oName = $tokens[2];
      $oKey = $tokens[3];
      $oValue = $tokens[4];
      
      $o = New-F5.DataObject -Product $oProd -Type $oType -Name $oName -Key $oKey -Value $oValue;
      
      $match = $true;

      # Process filter parameters
      if ( ($Product -ne $null) -and ($oProd  -notlike $Product) ) { $match = $false; }
      if ( ($Type    -ne $null) -and ($oType  -notlike $Type   ) ) { $match = $false; }
      if ( ($Name    -ne $null) -and ($oName  -notlike $Name   ) ) { $match = $false; }
      if ( ($Key     -ne $null) -and ($oKey   -notlike $Key    ) ) { $match = $false; }
      if ( ($Value   -ne $null) -and ($oValue -notlike $Value  ) ) { $match = $false; }
      
      if ( $match ) { $Objs += (,$o); }
    }
  }
  
  $Objs;
}

#-------------------------------------------------------------------------
function Set-F5.MetaData()
#-------------------------------------------------------------------------
{
  param(
    $Product = $null,
    $Type = $null,
    $Name = $null,
    $Key = $null,
    $Value = $null
  );
  if ( ($Product -eq $null)
    -or ($Type -eq $null)
    -or ($Name -eq $null)
    -or ($Key -eq $null)
    -or ($Value -eq $null) )
  {
    Write-Host "Usage: Set-F5.MetaData -Product prod -Type type -Name name -Key key -Value value";
    return;
  }
  
  $StringClass = New-Object -TypeName iControl.LocalLBClassStringClass;
  $StringClass.name = $script:Datagroup;
  $StringClass.members = ( ,"${Product}:${Type}:${Name}:${Key}:${Value}");
  
  trap {
    if ( $_.Exception.Message.Contains("already exists") ) {
      # Class member already exists, so eat the error.
      continue;
    }
  }
  (Get-F5.iControl).LocalLBClass.add_string_class_member( (,$StringClass) );
}

#-------------------------------------------------------------------------
function Remove-F5.MetaData()
#-------------------------------------------------------------------------
{
  param($Product = $null, $Type = $null, $Name = $null, $Key = $null, $Value = $null);
  if ( ($Product -eq $null)
    -or ($Type -eq $null)
    -or ($Name -eq $null)
    -or ($Key -eq $null)
    -or ($Value -eq $null) )
  {
    Write-Host "Usage: Remove-F5.MetaData -Product prod -Type type -Name name -Key key -Value value";
    return;
  }
  
  $StringClass = New-Object -TypeName iControl.LocalLBClassStringClass;
  $StringClass.name = $script:Datagroup;
  $StringClass.members = ( ,"${Product}:${Type}:${Name}:${Key}:${Value}");

  trap {
    if ( $_.Exception.Message.Contains("was not found") ) {
      # Class member doesn't exists, so eat the error.
      continue;
    }
  }
  (Get-F5.iControl).LocalLBClass.delete_string_class_member( (,$StringClass) );
  
}
Published Mar 09, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment