2 Replies Latest reply: May 13, 2016 5:41 AM by Rick van Vliet RSS

    Use REST api to create volume for VMWare.

    Erik Snijder Newbie

      Hello,

       

      Currently we are using a powershell script with a SSH plugin to create a volume for VMWare. We would like to start using the REST api but we are unable to find how to add several parameter on creation of the volume.

       

      The current SSH script:

       

              $nimuser="xxxx"

              $nimpw="xxxx"

              $nimarray="xxxx"

              $plink="C:\plink.exe"

              $plinkarrayoptions="$nimarray -l $nimuser -pw $nimpw"

              $remotecmd1="vol --create $NimVolName --size $volsizeMB --perfpolicy VMware\ ESX\ 5 --initiatorgrp SEN\-VMNIMG\-C01\-R630 --multi_initiator 1"

              $command1=$plink+" "+$plinkarrayoptions+" "+$remotecmd1+" "

              $msg=Invoke-Expression -command $command1 -ErrorAction SilentlyContinue

       

      We are using the following REST api script:

       

           [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

           # Get Token

           $array = "xxx"

           $username = "xxx"

            $password = "xxx"

       

      $data = @{

          username = $username

        password = $password

      }

      $body = convertto-json (@{ data = $data })

       

      $uri = "https://" + $array + ":5392/v1/tokens"

      $token = Invoke-RestMethod -Uri $uri -Method Post -Body $body

      $token = $token.data.session_token

       

      # Create a Volume

       

      $data = @{

          name = $nimvolname

        size = $volsizeMB

      }

       

      $body = convertto-json (@{ data = $data })

      $header = @{ "X-Auth-Token" = $token }

      $uri = "https://" + $array + ":5392/v1/volumes"

      $result = Invoke-RestMethod -Uri $uri -Method Post -Body $body -Header $header

       

      $result.data | select name,size,serial_number | format-table -autosize

       

      We need to add the parameters in italic in the SSH script to REST api script.

      Could someone assist us?

       

      Regards,

      Erik

        • Re: Use REST api to create volume for VMWare.
          Julian Cates Adventurer

          Hi Erik,

           

          First of all, apologies that I didn't see your question earlier.  I can definitely help you out here.

           

          Before answering your question, since you are using PowerShell, you might be interested in checking out the newly released Nimble PowerShell Toolkit. It provides the same type of functionality that you can get through the REST API, but makes it a bit easier to consume.  I blogged about it HERE if you'd like more information.

           

          If you want to continue with invoking REST through PowerShell, you'll just need to specify a few additional parameters to the "data" object you are sending.  Currently, you have name and size specified.  You would want to add additional lines to specify the perfpolicy_id and set multi_initiator to true.

           

          So it would look like:

           

          $data = @{

               name = $nimvolname

               size = $volsizeMB

               perfpolicy_id = $myperfpolicy_id

               multi_initiator = "true"

          }

           

          OK, so how do you find the right perfpolicy id to use?  There is another API for performance_policies. So do a query on that and find the ID of the performance policy you want to use. If you're always going to be using the same performance policy, you can just determine it ahead of time and hard code it here if you'd like.

           

          Once you've obtained a token, the following code should show you all of the perf policies:

           

          # Get Perf Policies

           

          $header = @{ "X-Auth-Token" = $token }

          $uri = "https://" + $array + ":5392/v1/performance_policies"

          $result = Invoke-RestMethod -Uri $uri -Method Get -Header $header

           

          $result.data | format-table -autosize

           

          Now the last bit of information you need is to assign the volume to the correct initiator group.  In the API, this is done by creating an access control record. The ACR takes the initiator group ID and the volume ID as parameters.  To obtain the initiator group id, you can use code like the above but just replace performance_policies with initiator_groups.

           

          You can get the volume id when you create the volume.  In your case, you are selecting name, size, and serial_number.  Just add "id" to that and use the result here.

           

          The following code will create an ACR:

           

          # Add Access Control Record

           

          $data = @{

               apply_to = "both"

               initiator_group_id = $my_igroup_id

               vol_id = $my_vol_id

          }

           

          $body = convertto-json (@{ data = $data })

          $header = @{ "X-Auth-Token" = $token }

          $uri = "https://" + $array + ":5392/v1/access_control_records"

          $result = Invoke-RestMethod -Uri $uri -Method Post -Body $body -Header $header

           

          $result.data

           

           

          Hope this helps!

           

          Cheers,

           

          Julian

          • Re: Use REST api to create volume for VMWare.
            Rick van Vliet Adventurer

            Hi Erik,

             

            We already had email contact but this might help others as well:

             

            If you would like to stay Powershell native without the need to install an extra toolkit you can do the following:

            You first need to get the performance policy ID, after you have this you can add it to the data you are pushing to the api to create the volume

            $data = @{  

            name = $name
            size = $size
            perfpolicy_id = $perf_policy_id
            }

             

            After you create the volume you need to create a ACL relation between the two.

            As you will get the volume ID back in the result you already know this so you only need to get the ID of the initiator group and then you can link the initiator group and the volume together:

             

            $data = @{

              apply_to = "both"

              initiator_group_id = $initiator_group_id

              vol_id = $volume_id

            }

             

            $body = convertto-json (@{ data = $data })

            $uri_acl = "https://" + $array + ":5392/v1/access_control_records"

            $result_acl = Invoke-RestMethod -Uri $uri_acl -Method Post -Body $body -Header $header

             

            $result_acl.data | select vol_id,initiator_group_id | format-table -autosize

             

            Kind regards,

             

            Rick