Advertisement
EricWoodford

vra-abx-set_deployment_lease

Sep 30th, 2022 (edited)
1,560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #set a deployment lease on a specific deployment ID.
  2. # this action is triggered when a deployment is completed by a specific blueprint id.
  3.  
  4. function Request-VRABearerToken {
  5.     [CmdLetBinding()]
  6.     param(
  7.         [parameter(Mandatory=$true)]
  8.         [string]$refresh_Token
  9.     )
  10.     # Code will generate a new bearer token for VRA account and set global variable to be used later
  11.     $body = @{"refreshToken"=$refresh_Token} | ConvertTo-Json
  12.     $GetMyToken = Invoke-RestMethod -uri $('https://api.mgmt.cloud.vmware.com/iaas/api/login?apiVersion=2021-07-15') -body $body  -Method POST -ContentType "application/json"
  13.     if ($null -ne $GetMyToken) {
  14.         $GetMyToken | Add-Member -MemberType NoteProperty -name "expires" -Value $(get-date).AddSeconds($GetMyToken.expires_in)
  15.         Set-Variable -Value @{"authorization"="Bearer $([string]$GetMyToken.token)"} -Name headers -Scope Global
  16.         Set-Variable -Value $GetMyToken -Name "MyBearerToken" -Scope Global
  17.     }                  
  18. }
  19.  
  20. function expire-deployment {
  21.     [CmdLetBinding()]
  22.     param(
  23.         [parameter(Mandatory=$true)]
  24.         [string]$deploymentId,
  25.         [parameter(Mandatory=$true)]
  26.         [string]$expirationDate
  27.     )
  28.     #see if deployment id is valid by trying to find it in current VRA.
  29.     $testDeploymentId = Invoke-RestMethod -uri $("https://api.mgmt.cloud.vmware.com/deployment/api/deployments/"+$deploymentId) -ContentType "application/json" -UseBasicParsing -headers $headers -Method GET
  30.     # drop out of this deployment ID is invalid
  31.     if ($null -eq $testDeploymentId ) {return "no deployment found with this ID"}
  32.     # drop out of more than 1 deployment found.
  33.     if ($testDeploymentId -is [array]) {return "too many deployments found during query."}
  34.     do {
  35.         # loop until not in middle of another action.
  36.         $checkingStatus = Invoke-RestMethod -uri $("https://api.mgmt.cloud.vmware.com/deployment/api/deployments/"+$deploymentId+"/requests") -ContentType "application/json" -UseBasicParsing -headers $headers -Method GET
  37.     } while (($checkingStatus.content.status -ne "SUCCESSFUL").count -ne 0)
  38.     if (($checkingStatus.content.status -eq "SUCCESSFUL") -and ($checkingStatus.content.actionId -eq "Deployment.changelease")) {
  39.         #lease already set, do not run again. Returning previous lease date. Need to do via day-2 action.
  40.         return $("Lease Date:"+$checkingStatus.content.inputs);
  41.     }
  42.     $body = @{"actionId" = "Deployment.ChangeLease"; "inputs" = @{"Lease Expiration Date" = "$expirationDate" } } | ConvertTo-Json
  43.     $url = "https://api.mgmt.cloud.vmware.com/deployment/api/deployments/"+$deploymentId+"/requests?apiVersion=2021-07-15"
  44.     $leaseResults = Invoke-RestMethod -uri $url -Method POST -body $body -ContentType "application/json" -UseBasicParsing -headers $headers
  45.     return $leaseResults
  46. }
  47.  
  48.  
  49. function handler($context, $inputs) {
  50.     #decode encrypted refresh token from action constants
  51.     $refresh_Token = $context.getSecret($inputs.refreshToken);  
  52.     #get new bearer token
  53.     Request-VRABearerToken -refresh_token $refresh_Token
  54.     # configure new lease expiration date based off today+[leasePeriod] days.
  55.     $expirationDate = $(get-date).addDays($inputs.LeasePeriod).tostring("yyyy-MM-ddTHH:mm:00.000Z")
  56.     #run function to set lease
  57.     $SetLeaseResults = expire-deployment -deploymentId $inputs.deploymentId -expirationDate $expirationDate
  58.     return $SetLeaseResults
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement