on 09-Mar-2015 11:59
Problem this snippet solves:
The components that make up the BIG-IP Global Traffic Manager can be divided into two categories: logical and physical components. Logical Components are abstractions of network resources, such as virtual servers. Physical components, on the other hand, have a direct correlation with one or more physical entities on the network. In this article, I will discuss Data Centers (one of the physical network components in the Global Traffic Manager) and illustrate how to programmatically manage them with the iControl API.
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. #---------------------------------------------------------------------------- param ( $g_bigip = $null, $g_uid = $null, $g_pwd = $null, $g_cmd = $null, $g_name = $null, $g_arg1 = $null, $g_arg2 = $null ); Set-PSDebug -strict; #------------------------------------------------------------------------- # function Write-Usage #------------------------------------------------------------------------- function Write-Usage() { Write-Host @" Usage: GtmDataCenter.ps1 host uid pwd [options] options ------- list - Get a list of rate classess create name location contact - Create a data center. setcontact name contact - Set the contact info for the given data center. setlocation name location - Set the location info for the given data center. setstate name [enabled|disabled] - Set the enabled state for the given data center. getstatistics name - Get the statisics for the given data center. "@; exit; } #------------------------------------------------------------------------- # Get-DataCenterListList #------------------------------------------------------------------------- function Get-DataCenterList() { param([string]$dc = $null ); if ( $dc ) { $dc_list = ,$dc; } else { $dc_list = (Get-F5.iControl).GlobalLBDataCenter.get_list(); } Write-Host "Data Centers:"; if ( $dc_list ) { $contacts = (Get-F5.iControl).GlobalLBDataCenter.get_contact_information($dc_list); $locations = (Get-F5.iControl).GlobalLBDataCenter.get_location_information($dc_list); $enabled_states = (Get-F5.iControl).GlobalLBDataCenter.get_enabled_state($dc_list); $statuses = (Get-F5.iControl).GlobalLBDataCenter.get_object_status($dc_list); $links = (Get-F5.iControl).GlobalLBDataCenter.get_link($dc_list); $servers = (Get-F5.iControl).GlobalLBDataCenter.get_server($dc_list); for($i=0; $i -lt $dc_list.Length; $i++) { Write-Host @" -------------------- Data Center $($dc_list[$i]) Contact : $($contacts[$i]) Location : $($locations[$i]) Enabled State : $($enabled_states[$i]) Availability : $($statuses[$i].availability_status) Enabled Status: $($statuses[$i].enabled_status) Status Desc :$($statuses[$i].status_description) "@ Write-Host " Links :"; for($j=0; $j -lt $links[$i].links.Length; $j++) { Write-Host " [$j] $($links[$i].links[$j])" } Write-Host " Servers :"; for($j=0; $j -lt $servers[$i].servers.Length; $j++) { Write-Host " [$j] $($servers[$i].servers[$j])" } } } } #------------------------------------------------------------------------- # Create-DataCenter #------------------------------------------------------------------------- function Create-DataCenter() { param([string]$name = $null, [string]$location = $null, [string]$contact = $null); if ( $name -and $location -and $contact ) { $dca = New-Object -TypeName iControl.GlobalLBDataCenterDataCenterAttribute; $dca.name = $name; $dca.location = $location; $dca.contact = $contact; (Get-F5.iControl).GlobalLBDataCenter.create( (,$dca) ); Get-DataCenterList $name; } } #------------------------------------------------------------------------- # Delete-DataCenter #------------------------------------------------------------------------- function Delete-DataCenter() { param([string]$name = $null); if ( $name ) { $dca = New-Object -TypeName iControl.GlobalLBDataCenterDataCenterAttribute; (Get-F5.iControl).GlobalLBDataCenter.delete_data_center( (,$name) ); Get-DataCenterList; } } #------------------------------------------------------------------------- # Set-DataCenterContact #------------------------------------------------------------------------- function Set-DataCenterContact() { param([string]$name = $null, [string]$contact = $null); if ( $name -and $contact ) { (Get-F5.iControl).GlobalLBDataCenter.set_contact_information( (, $name), (, $contact) ); Get-DataCenterList $name; } } #------------------------------------------------------------------------- # Set-DataCenterLocation #------------------------------------------------------------------------- function Set-DataCenterLocation() { param([string]$name = $null, [string]$location = $null); if ( $name -and $location ) { (Get-F5.iControl).GlobalLBDataCenter.set_location_information( (, $name), (, $location) ); Get-DataCenterList $name; } } #------------------------------------------------------------------------- # Set-DataCenterEnabledState #------------------------------------------------------------------------- function Set-DataCenterEnabledState() { param([string]$name = $null, [string]$state = $null); if ( $name -and $state ) { $EnabledState = "STATE_DISABLED"; if ( $state -eq "enabled" ) { $EnabledState = "STATE_ENABLED"; } (Get-F5.iControl).GlobalLBDataCenter.set_enabled_state( (, $name), (, $EnabledState) ); Get-DataCenterList $name; } } #------------------------------------------------------------------------- # Get-DataCenterStatistics #------------------------------------------------------------------------- function Get-DataCenterStatistics { param([string]$name = $null); if ( $name ) { $DataCenterStats = (Get-F5.iControl).GlobalLBDataCenter.get_statistics( (,$name) ); $stats = $DataCenterStats.statistics[0]; Write-Host "Statistics for Data Center '$(${stats}.data_center)'" foreach ($stat in $stats.statistics) { $val = Convert-To64Bit $stat.value.high $stat.value.low; Write-Host " " ("{0,-44}" -f ${stat}.type) "=" $val } } } #------------------------------------------------------------------------- # function Convert-To64Bit #------------------------------------------------------------------------- function Convert-To64Bit() { param($high, $low); $low = [Convert]::ToString($low,2).PadLeft(32,'0') if($low.length -eq "64") { $low = $low.substring(32,32) } return [Convert]::ToUint64([Convert]::ToString($high,2).PadLeft(32,'0')+$low,2); } #------------------------------------------------------------------------- # Do-Initialize #------------------------------------------------------------------------- function Do-Initialize() { if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null ) { Add-PSSnapIn iControlSnapIn } $success = Initialize-F5.iControl -HostName $g_bigip -Username $g_uid -Password $g_pwd; return $success; } #------------------------------------------------------------------------- # Main Application Logic #------------------------------------------------------------------------- if ( ($g_bigip -eq $null) -or ($g_uid -eq $null) -or ($g_pwd -eq $null) ) { Write-Usage; } if ( Do-Initialize ) { switch ($g_cmd) { "" { Get-DataCenterList; } "list" { Get-DataCenterList $g_name; } "create" { Create-DataCenter $g_name $g_arg1 $g_arg2; } "delete" { Delete-DataCenter $g_name; } "setcontact" { Set-DataCenterContact $g_name $g_arg1; } "setlocation" { Set-DataCenterLocation $g_name $g_arg1; } "setstate" { Set-DataCenterEnabledState $g_name $g_arg1; } "getstatistics" { Get-DataCenterStatistics $g_name; } default { Write-Usage; } } } else { Write-Error "ERROR: iControl subsystem not initialized" }