Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #set a deployment lease on a specific deployment ID.
- # this action is triggered when a deployment is completed by a specific blueprint id.
- function Request-VRABearerToken {
- [CmdLetBinding()]
- param(
- [parameter(Mandatory=$true)]
- [string]$refresh_Token
- )
- # Code will generate a new bearer token for VRA account and set global variable to be used later
- $body = @{"refreshToken"=$refresh_Token} | ConvertTo-Json
- $GetMyToken = Invoke-RestMethod -uri $('https://api.mgmt.cloud.vmware.com/iaas/api/login?apiVersion=2021-07-15') -body $body -Method POST -ContentType "application/json"
- if ($null -ne $GetMyToken) {
- $GetMyToken | Add-Member -MemberType NoteProperty -name "expires" -Value $(get-date).AddSeconds($GetMyToken.expires_in)
- Set-Variable -Value @{"authorization"="Bearer $([string]$GetMyToken.token)"} -Name headers -Scope Global
- Set-Variable -Value $GetMyToken -Name "MyBearerToken" -Scope Global
- }
- }
- function expire-deployment {
- [CmdLetBinding()]
- param(
- [parameter(Mandatory=$true)]
- [string]$deploymentId,
- [parameter(Mandatory=$true)]
- [string]$expirationDate
- )
- #see if deployment id is valid by trying to find it in current VRA.
- $testDeploymentId = Invoke-RestMethod -uri $("https://api.mgmt.cloud.vmware.com/deployment/api/deployments/"+$deploymentId) -ContentType "application/json" -UseBasicParsing -headers $headers -Method GET
- # drop out of this deployment ID is invalid
- if ($null -eq $testDeploymentId ) {return "no deployment found with this ID"}
- # drop out of more than 1 deployment found.
- if ($testDeploymentId -is [array]) {return "too many deployments found during query."}
- do {
- # loop until not in middle of another action.
- $checkingStatus = Invoke-RestMethod -uri $("https://api.mgmt.cloud.vmware.com/deployment/api/deployments/"+$deploymentId+"/requests") -ContentType "application/json" -UseBasicParsing -headers $headers -Method GET
- } while (($checkingStatus.content.status -ne "SUCCESSFUL").count -ne 0)
- if (($checkingStatus.content.status -eq "SUCCESSFUL") -and ($checkingStatus.content.actionId -eq "Deployment.changelease")) {
- #lease already set, do not run again. Returning previous lease date. Need to do via day-2 action.
- return $("Lease Date:"+$checkingStatus.content.inputs);
- }
- $body = @{"actionId" = "Deployment.ChangeLease"; "inputs" = @{"Lease Expiration Date" = "$expirationDate" } } | ConvertTo-Json
- $url = "https://api.mgmt.cloud.vmware.com/deployment/api/deployments/"+$deploymentId+"/requests?apiVersion=2021-07-15"
- $leaseResults = Invoke-RestMethod -uri $url -Method POST -body $body -ContentType "application/json" -UseBasicParsing -headers $headers
- return $leaseResults
- }
- function handler($context, $inputs) {
- #decode encrypted refresh token from action constants
- $refresh_Token = $context.getSecret($inputs.refreshToken);
- #get new bearer token
- Request-VRABearerToken -refresh_token $refresh_Token
- # configure new lease expiration date based off today+[leasePeriod] days.
- $expirationDate = $(get-date).addDays($inputs.LeasePeriod).tostring("yyyy-MM-ddTHH:mm:00.000Z")
- #run function to set lease
- $SetLeaseResults = expire-deployment -deploymentId $inputs.deploymentId -expirationDate $expirationDate
- return $SetLeaseResults
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement