-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Set-vRAProject to reflect better working with PATCH API request
- Loading branch information
1 parent
b5c453c
commit 90155f1
Showing
1 changed file
with
39 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ | |
Members = '[email protected]','[email protected]' | ||
Administrators = '[email protected]','[email protected]' | ||
OperationTimeout = 3600 | ||
SharedResources = $true | ||
SharedResources = 'true' | ||
CustomProperties = $CustomProperties | ||
} | ||
|
@@ -173,7 +173,8 @@ | |
|
||
[Parameter(Mandatory=$false,ParameterSetName="ById")] | ||
[Parameter(Mandatory=$false,ParameterSetName="ByName")] | ||
[Switch]$SharedResources, | ||
[ValidateSet("true", "false", IgnoreCase = $false)] | ||
[String]$SharedResources, | ||
|
||
[Parameter(Mandatory=$false,ParameterSetName="ById")] | ||
[Parameter(Mandatory=$false,ParameterSetName="ByName")] | ||
|
@@ -191,15 +192,6 @@ | |
|
||
begin { | ||
|
||
if ($PSBoundParameters.ContainsKey("SharedResources")) { | ||
|
||
$SharedResourcesStatus = 'true' | ||
} | ||
else { | ||
|
||
$SharedResourcesStatus = 'false' | ||
} | ||
|
||
function CalculateOutput([PSCustomObject]$Project) { | ||
[PSCustomObject] @{ | ||
Name = $Project.name | ||
|
@@ -226,7 +218,7 @@ | |
# --- Update Project by id | ||
'ById' { | ||
if ($null -eq $Name) { | ||
# if the user does not provide a name keep the existing | ||
# If the user does not provide a name keep the existing | ||
$ExistingProject = Get-vRAProject -Id $Id | ||
if (!$ExistingProject){ | ||
|
||
|
@@ -238,7 +230,7 @@ | |
|
||
# --- Update Project by name | ||
'ByName' { | ||
# we need the id to do the patch | ||
# We need the id to do the patch | ||
$ExistingProject = Get-vRAProject -Name $Name | ||
if (!$ExistingProject){ | ||
|
||
|
@@ -270,126 +262,109 @@ | |
|
||
if (-not $PSBoundParameters.ContainsKey("JSON")) { | ||
|
||
$Project = [PSCustomObject]@{} | ||
|
||
if ($PSBoundParameters.ContainsKey("NewName")){ | ||
|
||
Write-Verbose -Message "Updating Name: $($ExistingProject.name) >> $($NewName)" | ||
$ExistingProject.name = $NewName | ||
$Project | Add-Member -MemberType NoteProperty -Name 'name' -Value $NewName | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey("Description")){ | ||
|
||
Write-Verbose -Message "Updating Description: $($ExistingProject.description) >> $($Description)" | ||
$ExistingProject.description = $Description | ||
$Project | Add-Member -MemberType NoteProperty -Name 'description' -Value $Description | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey("Zones")){ | ||
|
||
Write-Verbose -Message "Updating Zones: $($ExistingProject.Zones) >> $($Zones)" | ||
if (!$Zones){ | ||
$ExistingProject.Zones = @() | ||
|
||
$EmptyZones = @() | ||
$Project | Add-Member -MemberType NoteProperty -Name 'zoneAssignmentConfigurations' -Value $EmptyZones | ||
} | ||
else { | ||
$ExistingProject.Zones = $Zones | ||
|
||
$Project | Add-Member -MemberType NoteProperty -Name 'zoneAssignmentConfigurations' -Value $Zones | ||
} | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey("Members")){ | ||
Write-Verbose -Message "Updating Members: $($ExistingProject.Members) >> $($Members)" | ||
|
||
Write-Verbose -Message "Updating Members: $($ExistingProject.Members.email) >> $($Members)" | ||
if (!$Members){ | ||
$ExistingProject.Members = @() | ||
|
||
$Members = @() | ||
$Project | Add-Member -MemberType NoteProperty -Name 'members' -Value $Members | ||
} | ||
else { | ||
$MembersArray = @() | ||
foreach ($Member in $Members){ | ||
$MembersArray += [PSCustomObject]@{email=$Member} | ||
} | ||
$ExistingProject.Members = $MembersArray | ||
$Project | Add-Member -MemberType NoteProperty -Name 'members' -Value $MembersArray | ||
} | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey("Administrators")){ | ||
Write-Verbose -Message "Updating Administrators: $($ExistingProject.Administrators) >> $($Administrators)" | ||
|
||
Write-Verbose -Message "Updating Administrators: $($ExistingProject.Administrators.email) >> $($Administrators)" | ||
if (!$Administrators){ | ||
$ExistingProject.Administrators = @() | ||
|
||
$Administrators = @() | ||
$Project | Add-Member -MemberType NoteProperty -Name 'administrators' -Value $Administrators | ||
} | ||
else { | ||
$AdministratorsArray = @() | ||
foreach ($Administrator in $Administrators){ | ||
$AdministratorsArray += [PSCustomObject]@{email=$Administrator} | ||
} | ||
$ExistingProject.Administrators = $AdministratorsArray | ||
$Project | Add-Member -MemberType NoteProperty -Name 'administrators' -Value $AdministratorsArray | ||
} | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey("Viewers")){ | ||
Write-Verbose -Message "Updating Viewers: $($ExistingProject.Viewers) >> $($Viewers)" | ||
|
||
Write-Verbose -Message "Updating Viewers: $($ExistingProject.Viewers.email) >> $($Viewers)" | ||
if (!$Viewers){ | ||
$ExistingProject.Viewers = @() | ||
|
||
$Viewers = @() | ||
$Project | Add-Member -MemberType NoteProperty -Name 'viewers' -Value $Viewers | ||
} | ||
else { | ||
$ViewersArray = @() | ||
foreach ($Viewer in $Viewers){ | ||
$ViewersArray += [PSCustomObject]@{email=$Viewer} | ||
} | ||
$ExistingProject.Viewers = $ViewersArray | ||
$Project | Add-Member -MemberType NoteProperty -Name 'viewers' -Value $ViewersArray | ||
} | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey("OperationTimeout")){ | ||
|
||
Write-Verbose -Message "Updating OperationTimeout: $($ExistingProject.OperationTimeout) >> $($OperationTimeout)" | ||
$ExistingProject.OperationTimeout = $OperationTimeout | ||
$Project | Add-Member -MemberType NoteProperty -Name 'operationTimeout' -Value $OperationTimeout | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey("SharedResources")){ | ||
|
||
Write-Verbose -Message "Updating SharedResources: $($ExistingProject.SharedResources) >> $($SharedResourcesStatus)" | ||
$ExistingProject.SharedResources = $SharedResourcesStatus | ||
Write-Verbose -Message "Updating SharedResources: $($ExistingProject.SharedResources) >> $($SharedResources)" | ||
$Project | Add-Member -MemberType NoteProperty -Name 'sharedResources' -Value $SharedResources | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey("PlacementPolicy")){ | ||
|
||
Write-Verbose -Message "Updating PlacementPolicy: $($ExistingProject.PlacementPolicy) >> $($PlacementPolicy)" | ||
$ExistingProject.PlacementPolicy = $PlacementPolicy | ||
$Project | Add-Member -MemberType NoteProperty -Name 'placementPolicy' -Value $PlacementPolicy | ||
} | ||
|
||
# Take a copy of custom properties | ||
$ExistingCustomProperties = $ExistingProject.CustomProperties | ||
|
||
# Remove OrganizationId and Links properties on object since not needed (CustomProperties temporarily) | ||
$ExistingProject.psobject.properties.remove('OrganizationId') | ||
$ExistingProject.psobject.properties.remove('Links') | ||
$ExistingProject.psobject.properties.remove('CustomProperties') | ||
|
||
$json = $ExistingProject | ConvertTo-Json -Depth 5 | ||
|
||
# Convert property names from UpperCamelCase to LowerCamelCase | ||
$Body = [regex]::Replace( | ||
$json, | ||
'(?<=")(\w+)(?=":)', | ||
{ | ||
([Char]::ToLower($args[0].Groups[1].Value[0]) + $args[0].Groups[1].Value.Substring(1)) | ||
|
||
} | ||
) | ||
|
||
# Adjust Zones property to match API requirements | ||
$Body = $Body -replace "Zones`":","zoneAssignmentConfigurations`":" | ||
|
||
# Re-add either updated or existing custom properties | ||
$ReformedBody = $Body | ConvertFrom-Json | ||
|
||
if ($PSBoundParameters.ContainsKey("CustomProperties")){ | ||
|
||
$ReformedBody | Add-Member -MemberType NoteProperty -Name 'customProperties' -Value $CustomProperties | ||
$Project | Add-Member -MemberType NoteProperty -Name 'customProperties' -Value $CustomProperties | ||
} | ||
else { | ||
|
||
$ReformedBody | Add-Member -MemberType NoteProperty -Name 'customProperties' -Value $ExistingCustomProperties | ||
} | ||
|
||
# Create JSON body to send in the API request | ||
$Body = $ReformedBody | ConvertTo-Json -Depth 5 | ||
|
||
$Body = $Project | ConvertTo-Json -Depth 5 | ||
Write-Verbose "JSON Body is : $($Body)" | ||
} | ||
|
||
|
@@ -398,7 +373,6 @@ | |
if ($PSCmdlet.ShouldProcess($Name)){ | ||
|
||
$URI = "/iaas/api/projects" | ||
Write-Verbose "$URI`/$Id" | ||
$Response = Invoke-vRARestMethod -Method PATCH -URI "$URI`/$Id" -Body $Body -Verbose:$VerbosePreference | ||
|
||
CalculateOutput $Response | ||
|