Functions/Public/properties-service/New-vRAPropertyDefinition.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
function New-vRAPropertyDefinition { <# .SYNOPSIS Create a custom Property Definition .DESCRIPTION Create a custom Property Definition .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 Definition (Defaults to the connection tenant ) .PARAMETER Index The display index of the Property .PARAMETER Required Switch to flag the Property as required .PARAMETER Encrypted Switch to flag the Property as Encrypted .PARAMETER String Switch to flag the Property type as String .PARAMETER StringDisplay The form display option for the Property .PARAMETER Boolean Switch to flag the Property type as Boolean .PARAMETER BooleanDisplay The form display option for the Property .PARAMETER Integer Switch to flag the Property type as Integer .PARAMETER IntegerDisplay The form display option for Integer .PARAMETER Decimal Switch to flag the Property type as Decimal .PARAMETER DecimalDisplay The form display option for Decimal .PARAMETER Datetime Switch to flag the Property type as Datetime .PARAMETER DatetimeDisplay The form display option for Datetime .PARAMETER JSON Property Definition to send in JSON format .INPUTS System.String. .OUTPUTS System.Management.Automation.PSObject .EXAMPLE # Create a string dropdown with defined values New-vRAPropertyDefinition -Name one -String -StringDisplay DROPDOWN -ValueType Static -Values @{Name1="Value1";Name2="Value2"} .EXAMPLE # Create an integer slider with min, max and increment New-vRAPropertyDefinition -Name IntegerName -Label "Select an Integer" -Integer -IntegerDisplay SLIDER -MinimumValue 1 -MaximumValue 10 -Increment 1 .EXAMPLE # Create a boolean checkbox New-vRAPropertyDefinition -Name BooleanName -Label "Check this box" -Boolean -BooleanDisplay CHECKBOX .EXAMPLE # Create a new decimal slider with min, max and increment New-vRAPropertyDefinition -Name DecimalTest -Decimal -DecimalDisplay SLIDER -MinimumValue 0 -MaximumValue 10 -Increment 0.5 #> [CmdletBinding(SupportsShouldProcess,ConfirmImpact="Low")][OutputType('System.Management.Automation.PSObject')] Param ( [parameter(Mandatory=$true)] [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=$false)] [ValidateNotNullOrEmpty()] [Int]$Index, [parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [Switch]$Required, [parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [Switch]$Encrypted, [parameter(Mandatory=$false,ParameterSetName="String")] [ValidateNotNullOrEmpty()] [Switch]$String, [parameter(Mandatory=$true,ParameterSetName="String")] [ValidateNotNullOrEmpty()] [ValidateSet("DROPDOWN","TEXTBOX","EMAIL","HYPERLINK","TEXTAREA")] [String]$StringDisplay, [parameter(Mandatory=$false,ParameterSetName="Boolean")] [ValidateNotNullOrEmpty()] [Switch]$Boolean, [parameter(Mandatory=$true,ParameterSetName="Boolean")] [ValidateNotNullOrEmpty()] [ValidateSet("CHECKBOX","YES_NO")] [String]$BooleanDisplay, [parameter(Mandatory=$false,ParameterSetName="Integer")] [ValidateNotNullOrEmpty()] [Switch]$Integer, [parameter(Mandatory=$true,ParameterSetName="Integer")] [ValidateNotNullOrEmpty()] [ValidateSet("DROPDOWN","SLIDER","TEXTBOX")] [String]$IntegerDisplay, [parameter(Mandatory=$false,ParameterSetName="Decimal")] [ValidateNotNullOrEmpty()] [Switch]$Decimal, [parameter(Mandatory=$true,ParameterSetName="Decimal")] [ValidateNotNullOrEmpty()] [ValidateSet("DROPDOWN","SLIDER","TEXTBOX")] [String]$DecimalDisplay, [parameter(Mandatory=$false,ParameterSetName="Datetime")] [ValidateNotNullOrEmpty()] [Switch]$Datetime, [parameter(Mandatory=$true,ParameterSetName="Datetime")] [ValidateNotNullOrEmpty()] [ValidateSet("DATE_TIME_PICKER")] [String]$DatetimeDisplay, # This is redundant, only one option [parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName="JSON")] [ValidateNotNullOrEmpty()] [String]$JSON ) DynamicParam { if ($PSBoundParameters.ContainsKey("JSON")){ # Do not evaluate dynamic parameters for JSON input return; } else { $Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary if($StringDisplay -eq "DROPDOWN") { $ValueTypes = "Static","Dynamic" NewDynamicParam -Name "EnableCustomValues" -Type switch -ParameterSet "String" -DPDictionary $Dictionary NewDynamicParam -Name "ValueType" -Mandatory -ValidateSet $ValueTypes -ParameterSet "String" -DPDictionary $Dictionary NewDynamicParam -Name "Values" -Type hashtable -Mandatory -ParameterSet "String" -DPDictionary $Dictionary } if($Integer) { if($IntegerDisplay -eq "DROPDOWN") { # Dropdown should have value type (static or dynamic) and a hashtable of values $ValueTypes = "Static","Dynamic" NewDynamicParam -Name "EnableCustomValues" -Type switch -ParameterSet "Integer" -DPDictionary $Dictionary NewDynamicParam -Name "ValueType" -Mandatory -ValidateSet $ValueTypes -ParameterSet "Integer" -DPDictionary $Dictionary NewDynamicParam -Name "Values" -Type hashtable -Mandatory -ParameterSet "Integer" -DPDictionary $Dictionary } elseif($IntegerDisplay -eq "SLIDER") { # Min/Max value are mandatory for a slider NewDynamicParam -Name "MinimumValue" -Mandatory -Type int -ParameterSet "Integer" -DPDictionary $Dictionary NewDynamicParam -Name "MaximumValue" -Mandatory -Type int -ParameterSet "Integer" -DPDictionary $Dictionary NewDynamicParam -Name "Increment" -Type decimal -ParameterSet "Integer" -DPDictionary $Dictionary } else { # Otherwise add some optional for Integers NewDynamicParam -Name "MinimumValue" -Type int -ParameterSet "Integer" -DPDictionary $Dictionary NewDynamicParam -Name "MaximumValue" -Type int -ParameterSet "Integer" -DPDictionary $Dictionary NewDynamicParam -Name "Increment" -Type decimal -ParameterSet "Integer" -DPDictionary $Dictionary } } if($Decimal) { if($DecimalDisplay -eq "DROPDOWN") { # Dropdown should have value type (static or dynamic) and a hashtable of values $ValueTypes = "Static","Dynamic" NewDynamicParam -Name "EnableCustomValues" -Type switch -ParameterSet "Decimal" -DPDictionary $Dictionary NewDynamicParam -Name "ValueType" -Mandatory -ValidateSet $ValueTypes -ParameterSet "Decimal" -DPDictionary $Dictionary NewDynamicParam -Name "Values" -Type hashtable -Mandatory -ParameterSet "Decimal" -DPDictionary $Dictionary } elseif($IntegerDisplay -eq "SLIDER") { # Min/Max value are mandatory for a slider NewDynamicParam -Name "MinimumValue" -Mandatory -Type decimal -ParameterSet "Decimal" -DPDictionary $Dictionary NewDynamicParam -Name "MaximumValue" -Mandatory -Type decimal -ParameterSet "Decimal" -DPDictionary $Dictionary NewDynamicParam -Name "Increment" -Type decimal -ParameterSet "Decimal" -DPDictionary $Dictionary } else { # Otherwise add some optional for Decimal NewDynamicParam -Name "MinimumValue" -Type decimal -ParameterSet "Decimal" -DPDictionary $Dictionary NewDynamicParam -Name "MaximumValue" -Type decimal -ParameterSet "Decimal" -DPDictionary $Dictionary NewDynamicParam -Name "Increment" -Type decimal -ParameterSet "Decimal" -DPDictionary $Dictionary } } if($Datetime) { # TODO - Datetime needs to be a string as UniversalSortableDateTimePattern NewDynamicParam -Name "MinimumValue" -Type DateTime -ParameterSet "Datetime" -DPDictionary $Dictionary NewDynamicParam -Name "MaximumValue" -Type DateTime -ParameterSet "Datetime" -DPDictionary $Dictionary } return $Dictionary } } begin { # --- Test for vRA API version xRequires -Version 7.0 #Get common parameters, pick out bound parameters not in that set Function _temp { [cmdletbinding()] param() } $BoundKeys = $PSBoundParameters.keys | Where-Object { (get-command _temp | Select-Object -ExpandProperty parameters).Keys -notcontains $_} foreach($param in $BoundKeys) { if (-not ( Get-Variable -name $param -scope 0 -ErrorAction SilentlyContinue ) ) { New-Variable -Name $Param -Value $PSBoundParameters.$param Write-Verbose "Adding variable for dynamic parameter '$param' with value '$($PSBoundParameters.$param)'" } } } process { try { # --- Set Body for REST request depending on ParameterSet if ($PSBoundParameters.ContainsKey("JSON")){ $Body = $JSON } else { $MultiValued = if($IsMultiValued) { "true" } else { "false" } $Mandatory = if($Required) { "true" } else { "false" } $CustomValues = if($AllowCustomValues) { "true" } else { "false" } if($Index -eq 0) { $IndexString = "null" } else { $IndexString = $Index.ToString() } if($String) { $DataType = "STRING" $Display = $StringDisplay } if($Boolean) { $DataType = "BOOLEAN" $Display = $BooleanDisplay } if($Integer) { $DataType = "INTEGER" $Display = $IntegerDisplay } if($Decimal) { $DataType = "DECIMAL" $Display = $DecimalDisplay } if($Datetime) { $DataType = "DATETIME" $Display = $DatetimeDisplay } # $DataType BOOLEAN cannot be Required $facets = "" if($DataType -ne "BOOLEAN") { $facets = @" "mandatory": { "type": "constant", "value": { "type": "boolean", "value": $($Mandatory) } }, "@ } $facets += @" "encrypted": { "type": "constant", "value": { "type": "boolean", "value": $($Encrypted.ToString().ToLower()) } }, "@ if($MinimumValue) { $facets += @" "minValue": { "type": "constant", "value": { "type": "integer", "value": $($MinimumValue) } }, "@ } if($MaximumValue) { $facets += @" "maxValue": { "type": "constant", "value": { "type": "integer", "value": $($MaximumValue) } }, "@ } if($Increment) { $facets += @" "increment": { "type": "constant", "value": { "type": "decimal", "value": $($Increment) } }, "@ } # Build permissible values if($ValueTypes -eq "Static") { $ValueJSON = "" foreach($Value in $Values.GetEnumerator()) { $ValueJSON += @" { "underlyingValue": { "type": "$($DataType.ToLower())", "value": "$($Value.Value)" }, "label": "$($Value.Name)" }, "@ } $PermissibleValues = @" "permissibleValues": { "type": "static", "customAllowed": $($CustomValues), "values": [ $($ValueJSON.Trim(",")) ] }, "@ } elseif($ValueTypes -eq "Dynamic") { # Not implemented yet!! $PermissibleValues = @" "permissibleValues": { "type": "dynamic", "customAllowed": false, "dependencies": [], "context": { "providerEntityId": "com.vmware.library.vc.storage/listDatastores", "parameterMappings": { "params": [ { "key": "host", "value": { "type": "constant", "value": { "type": "string", "value": "" } } } ] } } }, "@ } else { $PermissibleValues = @" "permissibleValues": null, "@ } $Body = @" { "id" : "$($Name)", "label" : "$($Label)", "description" : "$($Description)", "dataType" : { "type" : "primitive", "typeId" : "$($DataType)" }, "isMultiValued" : $($MultiValued), "displayAdvice" : "$($Display)", "tenantId" : "$($Tenant)", "orderIndex": $($IndexString), $($PermissibleValues) "facets": { $($facets.Trim(",")) } } "@ } $URI = "/properties-service/api/propertydefinitions" Write-Verbose -Message "Preparing POST to $($URI)" # --- Run vRA REST Request if ($PSCmdlet.ShouldProcess($Id)) { Invoke-vRARestMethod -Method POST -URI $URI -Body $Body | Out-Null Get-vRAPropertyDefinition -Id $Name } } catch [Exception]{ throw } } end { } } |