Skip to content

Commit

Permalink
Merge pull request #198 from Stevio54/PropertyGroups
Browse files Browse the repository at this point in the history
Adding Property Group functions to the properties-service
  • Loading branch information
jonathanmedd authored Apr 5, 2019
2 parents 5676396 + 853cf2a commit 55b20d0
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 2 deletions.
154 changes: 154 additions & 0 deletions src/Functions/Public/properties-service/Get-vRAPropertyGroup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
function Get-vRAPropertyGroup {
<#
.SYNOPSIS
Get a property group that the user is allowed to review.
.DESCRIPTION
API for property groups that a system administrator can interact with. It allows the user to interact
with property groups that the user is permitted to review.
.PARAMETER Id
The id of the property group
.PARAMETER Limit
The number of entries returned per page from the API. This has a default value of 100
.PARAMETER Page
The index of the page to display.
.INPUTS
System.String
System.Int
.OUTPUTS
System.Management.Automation.PSObject
.EXAMPLE
Get-vRAPropertyGroup
.EXAMPLE
Get-vRAPropertyGroup -Limit 200
.EXAMPLE
Get-vRAPropertyGroup -Id Hostname
#>
[CmdletBinding(DefaultParameterSetName="Standard")][OutputType('System.Management.Automation.PSObject')]

Param (

[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,ParameterSetName="ById")]
[ValidateNotNullOrEmpty()]
[String[]]$Id,

[Parameter(Mandatory=$false,ParameterSetName="Standard")]
[ValidateNotNullOrEmpty()]
[Int]$Page = 1,

[Parameter(Mandatory=$false,ParameterSetName="Standard")]
[ValidateNotNullOrEmpty()]
[Int]$Limit = 100

)

Begin {
# --- Test for vRA API version
xRequires -Version 7.0
}

Process {

try {

switch ($PsCmdlet.ParameterSetName) {

# --- Get property Group by id
'ById' {

foreach ($PropertyGroupId in $Id) {

$URI = "/properties-service/api/propertygroups/$($PropertyGroupId)"

$PropertyGroup = Invoke-vRARestMethod -Method GET -URI $URI -Verbose:$VerbosePreference

$props = @{}
foreach($vRAProp in $PropertyGroup.properties.PSObject.Properties) {
$facets = @{}
foreach($facetkey in $vRAProp.Value.facets.PSObject.Properties) {
$facets.Add($facetkey.Name, $facetKey.Value.value.value)
}

# add to props grouping now
$props.Add($vRAProp.Name, $facets)
}

[PSCustomObject] @{

Id = $PropertyGroup.id
Label = $PropertyGroup.label
Description = $PropertyGroup.description
TenantId = $PropertyGroup.tenantId
DateCreated = $PropertyGroup.createdDate
LastUpdatedDate = $PropertyGroup.lastUpdated
Properties = $props
}
}

break

}

# --- No parameters passed so return all property Groups
'Standard' {

$URI = "/properties-service/api/propertygroups?limit=$($Limit)&page=$($Page)&`$orderby=id asc"

$EscapedURI = [uri]::EscapeUriString($URI)

$Response = Invoke-vRARestMethod -Method GET -URI $EscapedURI -Verbose:$VerbosePreference

foreach ($PropertyGroup in $Response.content) {
$props = @{}
foreach($vRAProp in $PropertyGroup.properties.PSObject.Properties) {
$facets = @{}
foreach($facetkey in $vRAProp.Value.facets.PSObject.Properties) {
$facets.Add($facetkey.Name, $facetKey.Value.value.value)
}

# add to props grouping now
$props.Add($vRAProp.Name, $facets)
}

[PSCustomObject] @{

Id = $PropertyGroup.id
Label = $PropertyGroup.label
Description = $PropertyGroup.description
TenantId = $PropertyGroup.tenantId
DateCreated = $PropertyGroup.createdDate
LastUpdatedDate = $PropertyGroup.lastUpdated
Properties = $props
}

}

Write-Verbose -Message "Total: $($Response.metadata.totalElements) | Page: $($Response.metadata.number) of $($Response.metadata.totalPages) | Size: $($Response.metadata.size)"

break

}

}

}
catch [Exception]{

throw

}
}

End {

}
}
208 changes: 208 additions & 0 deletions src/Functions/Public/properties-service/New-vRAPropertyGroup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
function New-vRAPropertyGroup {
<#
.SYNOPSIS
Create a custom Property Group
.DESCRIPTION
Create a custom Property Group
.PARAMETER Name
The unique name (ID) of the Property
.PARAMETER Label
The text to display in forms for the Property
.PARAMETER Description
Description of the Property
.PARAMETER Tenant
The tenant in which to create the Property Group (Defaults to the connection tenant )
.PARAMETER Properties
A hashtable representing the properties you would like to build into this new property group
.PARAMETER JSON
Property Group to send in JSON format
.INPUTS
System.String.
.OUTPUTS
System.Management.Automation.PSObject
.EXAMPLE
# Create a simple property group with no properties addded
New-vRAPropertyGroup -Name one
.EXAMPLE
# Create a property group with a description and label
New-vRAPropertyGroup -Name OneWithDescription -Label "On With Description" -Description "This is one with a label and description"
.EXAMPLE
# Create a property group with some properties added in simple form
New-vRAPropertyGroup -Name OneWithPropetiesSimple -Label "One With Properties" -Properties @{"com.org.bool"=$false; "com.org.string"="string1"}
.EXAMPLE
# Create a property group with some properties added in the extended form
New-vRAPropertyGroup -Name OneWithPropertiesExt -Label "One With Properties" -Properties @{"com.org.bool"=@{"mandatory"=$true; "defaultValue"=$false;}; "com.org.encryptedandshowonform"=@{"encrypted"=$true; "visibility"=$true; "defaultValue"="Un-encrypted string";};}
#>
[CmdletBinding(SupportsShouldProcess,ConfirmImpact = "Low",DefaultParameterSetName = 'Default')][OutputType('System.Management.Automation.PSObject')]

Param (
[parameter(Mandatory = $true, ParameterSetName = "Default")]
[ValidateNotNullOrEmpty()]
[String]$Name,

[parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String]$Label = $Name,

[parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String]$Description,

[parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String]$Tenant = $Global:vRAConnection.Tenant,

[parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "Properties")]
[ValidateNotNullOrEmpty()]
[hashtable]$Properties,

[parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "JSON")]
[ValidateNotNullOrEmpty()]
[String]$JSON

)

begin {

# --- Test for vRA API version
xRequires -Version 7.0
}

process {

try {
# --- Set Body for REST request depending on ParameterSet
if ($PSBoundParameters.ContainsKey("JSON")) {
$Body = $JSON
}
else {
$propertiesRaw = ""

# process properties sent in
foreach ($propKey in $Properties.Keys) {
$prop = $Properties[$propKey]
switch ($prop.GetType()) {
"Hashtable" {
$facets = ""
foreach ($fKey in $prop.Keys) {
$f = $prop[$fKey]
switch ($fKey) {
"visibility" {
$facets += @"
"visibility": {
"type": "constant",
"value": {
"type": "boolean",
"value": $($f.toString().toLower())
}
},
"@
}
"encrypted" {
$facets += @"
"encrypted": {
"type": "constant",
"value": {
"type": "boolean",
"value": $($f.toString().toLower())
}
},
"@
}
"mandatory" {
$facets += @"
"mandatory": {
"type": "constant",
"value": {
"type": "boolean",
"value": $($f.toString().toLower())
}
},
"@
}
"defaultValue" {
$facets += @"
"defaultValue": {
"type": "constant",
"value": {
"type": "string",
"value": "$($f)"
}
},
"@
}
}
}
$propertiesRaw += @"
"$($propKey)": {
"facets": { $($facets.Trim(',')) }
},
"@
break
}
default {
$propertiesRaw += @"
"$($propKey)": {
"facets": {
"defaultValue": {
"type": "constant",
"value": {
"type": "string",
"value": "$($prop)"
}
}
}
},
"@
break
}
}
}
# logic to build input
$Body = @"
{
"id" : "$($Name)",
"label" : "$($Label)",
"description" : "$($Description)",
"tenantId" : "$($Tenant)",
"version": 0,
"properties": { $($propertiesRaw.Trim(',')) }
}
"@
}

$URI = "/properties-service/api/propertygroups"

Write-Verbose -Message "Preparing POST to $($URI)"

Write-Verbose -Message "Posting Body: $($Body)"

# --- Run vRA REST Request
if ($PSCmdlet.ShouldProcess($Id)) {
Invoke-vRARestMethod -Method POST -URI $URI -Body $Body | Out-Null
Get-vRAPropertyGroup -Id $Name
}
}
catch [Exception] {

throw
}
}
end {

}
}
Loading

0 comments on commit 55b20d0

Please sign in to comment.