Software Development Kit (SDK) and API Discussions

SLM - PowerShell

mbeattie
2,504 Views

Hi,

 

I recently installed SLM and found all the example code was in Perl\Python\Ruby. A good example can be found here:

 

http://netapp.io/2017/07/19/provision-storage-like-service-provider-netapp-service-level-manager/

 

Having that example code is great but it probably doesnt help all our windows customers out there...

So i thought it might be helpful to post a "very" basic example of using SLM to provision a LUN.

 

Whilst this example is targeting the creation of a LUN on cluster and vserver with ISCSI enabled (instead of targeting a cluster\storage service level) you get the idea (it's just an example to help you get started). One point worth noting is that i had to explicitly set TLS to version 1.1 to be able to connect to SLM.

 

Param(
   [Parameter(Mandatory=$True, HelpMessage="The cluster name")]
   [String]$ClusterName,
   [Parameter(Mandatory=$True, HelpMessage="The lun name to create")]
   [String]$LunName,
   [Parameter(Mandatory=$True, HelpMessage="The IP Address of the SLM Server")]
   [String]$SlmIPAddress,
   [Parameter(Mandatory=$False, HelpMessage="The IP Address of the SLM Server")]
   [String]$PortNumber=8443
)
#'------------------------------------------------------------------------------
#'Ignore self signed SSL certificate on SLM server and set TLS version to 1.1
#'------------------------------------------------------------------------------
Add-Type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls11
#'------------------------------------------------------------------------------
#'Prompt for Credentials to connect to SLM and enumerate the cluster.
#'------------------------------------------------------------------------------
$credentials = Get-Credential -Credential admin
$uri         = "https://$SlmIPAddress`:$PortNumber/api/2.0/ontap/clusters?name=$clusterName"
$result      = Invoke-RestMethod -Method Get -Uri $uri -Credential $credentials
$clusterKey  = $result.result.records.key
#'------------------------------------------------------------------------------
#'Enumerate the vserver.
#'------------------------------------------------------------------------------
$uri        = "https://$SlmIPAddress`:$PortNumber/api/2.0/ontap/storage-vms?cluster_key=$clusterKey&allowed_protocols=iscsi"
$result     = Invoke-RestMethod -Method Get -Uri $uri -Credential $credentials
$vserverKey = $result.result.records.key
#'------------------------------------------------------------------------------
#'Enumerate the vserver data volume.
#'------------------------------------------------------------------------------
$uri        = "https://$SlmIPAddress`:$PortNumber/api/2.0/ontap/volumes?storage_vm_key=$vserverKey&is_storage_vm_root=false"
$result     = Invoke-RestMethod -Method Get -Uri $uri -Credential $credentials
$volumeKey  = $result.result.records.key
$lunType    = "windows"
$guid       = [guid]::newguid();
$sizeBytes  = $(10 * (1024 * 1024 * 1024))
#'------------------------------------------------------------------------------
#'Set the LUN attributes to create and convert it JSON.
#'------------------------------------------------------------------------------
$lun = @{}
$lun.Add("storage_vm_key", $vserverKey)
$lun.Add("is_online", $True)
$lun.Add("is_space_alloc_enabled", $False)
$lun.Add("is_space_reservation_enabled", $False)
$lun.Add("volume_key", $volumeKey)
$lun.Add("multi_protocol_type", $lunType)
$lun.Add("comment", $guid)
$lun.Add("size", $sizeBytes)
$body = $lun | ConvertTo-Json
#'------------------------------------------------------------------------------
#'Create the LUN.
#'------------------------------------------------------------------------------
Write-Host "Creating LUN"
Write-Host ""
Write-Host "Cluster`: $clusterKey"
Write-Host "Vserver`: $vserverKey"
Write-Host "Volume`: $volumeKey"
Write-Host
$body
Write-Host ""
$uri    = "https://$SlmIPAddress`:$PortNumber/api/2.0/ontap/luns?name=$lunName"
$result = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers @{"content-type"="application/json"} -Credential $credentials
#'------------------------------------------------------------------------------

Example output:

 

PS C:\Scripts\PowerShell\Projects\SLM\CreateLun> .\CreateLun.ps1 -ClusterName cluster1 -LunName "lun_008" -SlmIPAddress XX.XX.XX.XX
Creating LUN

Cluster: b3b81b41-62a5-11e7-95d3-005056ac3a25:type=cluster,uuid=b3b81b41-62a5-11e7-95d3-005056ac3a25
Vserver: b3b81b41-62a5-11e7-95d3-005056ac3a25:type=vserver,uuid=6cc0d8c9-62a6-11e7-95d3-005056ac3a25
Volume: b3b81b41-62a5-11e7-95d3-005056ac3a25:type=volume,uuid=8318434b-5485-4a8d-839d-8a76552c86bb

{
    "comment":  "be803746-1224-4c91-a868-319edd75ae79",
    "volume_key":  "b3b81b41-62a5-11e7-95d3-005056ac3a25:type=volume,uuid=8318434b-5485-4a8d-839d-8a76552c86bb",
    "storage_vm_key":  "b3b81b41-62a5-11e7-95d3-005056ac3a25:type=vserver,uuid=6cc0d8c9-62a6-11e7-95d3-005056ac3a25",
    "size":  10737418240,
    "is_online":  true,
    "is_space_reservation_enabled":  false,
    "is_space_alloc_enabled":  false,
    "multi_protocol_type":  "windows"
}

Example CLI output:

 

cluster1::> lun show
Vserver   Path                            State   Mapped   Type        Size
--------- ------------------------------- ------- -------- -------- --------
vserver3  /vol/iscsi_data_001/lun_001     online  unmapped windows   10.00GB
vserver3  /vol/iscsi_data_001/lun_002     online  unmapped windows   10.00GB
vserver3  /vol/iscsi_data_001/lun_003     online  unmapped windows   10.00GB
vserver3  /vol/iscsi_data_001/lun_004     online  unmapped windows   10.00GB
vserver3  /vol/iscsi_data_001/lun_005     online  unmapped windows   10.00GB
vserver3  /vol/iscsi_data_001/lun_006     online  unmapped windows   10.00GB
vserver3  /vol/iscsi_data_001/lun_007     online  unmapped windows   10.00GB
vserver3  /vol/iscsi_data_001/lun_008     online  unmapped windows   10.00GB
8 entries were displayed.

Hope that helps!

 

Happy coding

 

/Matt

 

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.
0 REPLIES 0
Public