Wednesday, 26 April 2017

Mapping APIs to ClusterShell and PowerShell with ONTAP + more …

Ages ago (June 2014) I did a ClusterShell to PowerShell Command Translator. I wanted to do something slightly different, map APIs to: ClusterShell commands (using show-ontapi output), PowerShell cmdlets (using get-nchelp output), and include the useful category and family fields from Get-NcHelp.

To better explain, below is a screenshot example of the output.

Image: Section of output from API-to-CS-PS.PS1

And how to run API-to-CS-PS.PS1.

Image: Running API-to-CS-PS.PS1

And the PowerShell script to generate the output CSV is below:


## PRE-REQ: A connection to a cluster:
## >Import-Module DataONTAP
## > Connect-NcController {CLUSTER}
## > .\API-to-CS-PS.ps1

## MAP API TO CSHELL ##
## ================= ##

[String]$showOntapi = invoke-ncssh show-ontapi
# Assumption: APIs in show-ontapi are unique #
[System.Array]$Global:Lines = $showOntapi.split("`n")
[System.Object]$Global:APItoCSHELL = @{}
[System.Array]$APIlist = @()
[Boolean]$Recording = $FALSE
[String]$ONTAPI     = ""
[String]$Command    = ""
$Global:Lines | Foreach{
  If($Recording){
    If($_.StartsWith(" ")){
      If($Command){$Command += " "}
      # += " " because command might carry onto next line #
      $Command += $_.Trim(" ")
    }else{
      If($ONTAPI -and $Command){
        $Global:APItoCSHELL.$ONTAPI = $Command
        $APIlist += $ONTAPI
        [String]$ONTAPI = ""
        [String]$Command = ""                       
      }
      If($_.Split(" ").Count -eq 1){ $ONTAPI = $_ }
      else{
        $ONTAPI = $_.Split(" ")[0]
        $Command = $_.SubString($ONTAPI.length,($_.length - $ONTAPI.length)).Trim(" ")
      }
    }
  }
  If($_.StartsWith("-")){$Recording = $TRUE}
  # The header finishes with a line of "-" #
  If($_ -like '[0-9]*'){$Recording = $FALSE}
  # The show-ontapi output ends with a count #
}

## MAP API TO PSHELL ##
## ================= ##

$GetNcHelp = Get-NcHelp
# A cmdlet might map to two or more APIs, we need unique APIs #
[System.Object]$Global:APItoPSHELL = @{}
$GetNcHelp | Foreach{
  If($_.API){
    Foreach($API in $_.API.Split(",")){
      If($Global:APItoPSHELL.$API){}
      else{
        [System.Object]$Global:APItoPSHELL.$API = @{}
        [String]$Global:APItoPSHELL.$API.Category   = ""
        [String]$Global:APItoPSHELL.$API.Family     = ""
        [String]$Global:APItoPSHELL.$API.PowerShell = ""
      }
      If($Global:APItoPSHELL.$API.PowerShell){$Global:APItoPSHELL.$API.PowerShell += " "}
      # Above adds " " if a cmdlet has already been logged for the API #
      If($_.Category){ $Global:APItoPSHELL.$API.Category    = $_.Category}
      If($_.Family){   $Global:APItoPSHELL.$API.Family      = [String]($_.Family)}
      If($_.Name){     $Global:APItoPSHELL.$API.PowerShell += $_.Name}
    }
  }
}

## GENERATE CSV AND EXPORT ##
## ======================= ##

[System.Array]$Global:CSV = @()
$APIlist | Foreach{
  $Category = $Family = $PSHELL = ""
  If($Global:APItoPSHELL.$_){
    $Category = $Global:APItoPSHELL.$_.Category
    $Family = $Global:APItoPSHELL.$_.Family
    $PSHELL = $Global:APItoPSHELL.$_.PowerShell
  }
  $Global:CSV += [PSCustomObject]@{
    "API"      = $_
    "Category" = $Category
    "Family"   = $Family
    "CSHELL"   = $Global:APItoCSHELL.$_
    "PSHELL"   = $PSHELL
  }
}
$Global:CSV | Export-CSV "API-to-CS-PS.CSV" -NoTypeInformation


No comments:

Post a Comment