I recently had the task of creating a PowerShell script to call REST API for adding Networks to an uplink set in HP OneView.

 

A quick overview of the environment.

Our enclosures have 4x FlexFabrics, 2 used to plumb an access port to the LOMs and 2 used as trunks to create the Virtual Switch teams.

The vSwitch uplink sets are labelled Switch_A and Switch_B

Each Vlan we connect is plumbed to each switch and named accordingly. As an example, for a network named LNet-Blog in SCVMM, we would create LNet-Blog_A and LNet-Blog_B and add them to the corresponding uplink

LNet-Blog_A > Switch_A

LNet-Blog_B > Switch_B

 

The assumption is the Networks have already been created in OneView and this script just finds all existing tagged Networks and adds them to appropriate uplink.

I have PoSH and SMA Runbooks for the end to end process but thought that given this one was slightly more complicated I have fast-tracked this post to you all.. my loyal readers..

 

Note: this modifies the uplink sets in your VC domain. Buyer beware!!!

Great, now that’s out of the way…. the script:

# login to the oneview appliance

$person = @{
    userName= "myusername"
    password= "mypassword" 
    authnHost= "192.168.1.10"
    authLoginDomain= "Local"
}
$json = $person | ConvertTo-Json
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-type", 'application/json')
$headers.Add("Accept", 'application/json')
$url = 'https://<oneviewserver>'
$uri = $url + '/rest/login-sessions'
$response = Invoke-RestMethod -Uri $uri -Method POST -Headers $headers -Body $json -ContentType 'application/json' 


# Get-Networks
$uri = $url + '/rest/ethernet-networks'
$Networks = Invoke-RestMethod -Method GET -Headers $auth -Uri $uri
$ANets = $Networks.members | where {$_.ethernetNetworkType -eq "Tagged"}

#list all networks in fabric
$Anets | sort-object Name | FT name,uri -autosize

# Get the uplinkset data
$uri = $url + '/rest/uplink-sets'
$Uplinks = Invoke-RestMethod -Method GET -Headers $auth -Uri $uri

# check the switch uplink sets for the presence of all hte tagged networks and add any missing ones
ForEach ($Uplink in $Uplinks.members | where {$_.name -like "Switch_*"}) 
{
    Write-Host -F Green "Working on" $Uplink.name
    IF($ANets.count -eq $Uplink.networkUris.count){Write-Host -F Green "Yay! Networks are in sync.. we can avoid all the heavy lifting."}
    ELSE
    {   
        Write-Host -F Green "Network uri's mapped" $Uplink.networkUris.count "but there are" $ANets.count "defined"
        ForEach($ANet in $ANets | where {$_.name.split("_")[1] -eq $Uplink.name.split("_")[1]}) 
        {
            $a = $null
            ForEach($Neturi in $Uplink.networkUris)
            {    
                IF($Neturi -eq $ANet.uri)
                {
                    $a = $true
                }
            }
            IF($a -ne $true)
            {
                Write-Host -F yellow "Network"$ANet.name "is missing from" $uplink.name
                $Uplink.networkUris += $Anet.uri
                $body = $uplink | ConvertTo-Json -Depth 5
                $uri = $url + $uplink.uri
                Write-Host -F Cyan "Adding network" $ANet.name "to uplinkset" $uplink.name
                Invoke-RestMethod -Method PUT -Headers $auth -Uri $uri -Body $body -ContentType 'application/json' 
            }
        }
    }
}

Enjoy!
Dan

Leave A Comment