Public/Add-DuneVariable.ps1
|
<#
.SYNOPSIS Add a variable to a Dune object. .DESCRIPTION Adds a variable (name/value/type) to a tenant, collection, deployment, resource group, or resource. The variable type is auto-detected from the value if not specified. The full variable set is sent as a PATCH request to the API. .PARAMETER Name The variable name. .PARAMETER Value The variable value. .PARAMETER Type The variable type. Valid values: string, list, number, boolean. Auto-detected if omitted. .PARAMETER Tenant A DuneTenant object. Accepts pipeline input. .PARAMETER Collection A DuneCollection object. Accepts pipeline input. .PARAMETER Deployment A DuneDeployment object. Accepts pipeline input. .PARAMETER ResourceGroup A DuneResourceGroup object. Accepts pipeline input. .PARAMETER Resource A DuneResource object. Accepts pipeline input. .PARAMETER Description Optional description for the variable. .PARAMETER Environment Optional environment scope for the variable. .EXAMPLE PS> Get-DuneDeployment -Name "app" | Add-DuneVariable -Name "MaxRetries" -Value 3 Adds a numeric variable to the deployment. #> function Add-DuneVariable { [CmdletBinding()] param ( [Parameter(Mandatory, Position = 0)] [String]$Name, [Parameter(Mandatory, Position = 1)] [PSObject]$Value, [Parameter(Position = 2)] [ValidateSet('string', 'list', 'number', 'boolean')] [String]$Type, [Parameter(ValueFromPipeline, ParameterSetName = "Tenant")] [DuneTenant]$Tenant, [Parameter(ValueFromPipeline, ParameterSetName = "Collection")] [DuneCollection]$Collection, [Parameter(ValueFromPipeline, ParameterSetName = "Deployment")] [DuneDeployment]$Deployment, [Parameter(ValueFromPipeline, ParameterSetName = "ResourceGroup")] [DuneResourceGroup]$ResourceGroup, [Parameter(ValueFromPipeline, ParameterSetName = "Resource")] [DuneResource]$Resource, [Parameter()] [Environments]$Description, [Parameter()] [Environments]$Environment ) begin {} process { Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)" switch ($PSCmdlet.ParameterSetName) { 'Tenant' { $Uri = 'tenants/variables' $Tenant = Get-DuneTenant $Variables = $Tenant.Variables } 'Collection' { $Uri = 'collections/{0}/variables' -f $Collection.Id $Collection = Get-DuneCollection -Id $Collection.Id $Variables = $Collection.Variables } 'Deployment' { $Uri = 'deployments/{0}/variables' -f $Deployment.Id $Deployment = Get-DuneDeployment -Id $Deployment.Id $Variables = $Deployment.Variables } 'ResourceGroup' { $Uri = 'resourcegroups/{0}/variables' -f $ResourceGroup.Id $ResourceGroup = Get-DuneResourceGroup -Id $ResourceGroup.Id $Variables = $ResourceGroup.Variables } 'Resource' { $Uri = 'resources/{0}/variables' -f $Resource.Id $Resource = Get-DuneResource -Id $Resource.Id $Variables = $Resource.Variables } Default { return 'Type has no variables.' } } if (-not $Type) { $Type = switch ($Value.gettype().Name) { 'Object[]' {'list'} 'String' {'string'} 'Boolean' {'boolean'} default { if (isNumeric($Value)) { 'number' } else { throw "Wrong Value Type: $($Value.gettype().Name) for $Name (Value: $($Value))" } } } } $Value = ConvertTo-Json -InputObject $Value -Compress #value piping not allowed, due to array $Body = @{} if ($Variables) { [Array]$Body.variables = $Variables.ToPropertiesHashtableJsonValue() } $Body.variables += @{name = $Name; value = $Value; type = $Type; environment = $Environment; description = $Description } Write-Verbose "Add Variable to $($PSCmdlet.ParameterSetName): Name:$Name,Value:$Value,Type:$Type" $Null = Invoke-DuneApiRequest -Uri $Uri -Method PATCH -Body $Body } end {} } |