Functions/Public/catalog-service/Request-vRAResourceAction.psm1

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
function Request-vRAResourceAction {
<#
    .SYNOPSIS
    Request an available resourceAction for a catalog resource
    
    .DESCRIPTION
    A resourceAction is a specific type of ResourceOperation that is performed by submitting a request.
    Unlike ResourceExtensions, resource actions can be invoked via the Service Catalog service and subject to approvals.
    
    .PARAMETER ActionId
    The Id for the resource action
    
    .PARAMETER ResourceId
    The id of the resource that the resourceAction will execute against

    .PARAMETER ResourceName
    The name of the resource that the resourceAction will execute against

    .INPUTS
    System.String

    .EXAMPLE
    $ResourceActionId = (Get-vRAResource -Name vm01 | Get-vRAResourceAction "Reboot").id
    Request-vRAResourceAction -Id $ResourceActionId -ResourceName vm01

    .EXAMPLE
    Request-vRAResourceAction -Id 6a301f8c-d868-4908-8348-80ad0eb35b00 -ResourceId 20402e93-fb1d-4bd9-8a51-b809fbb946fd

    .EXAMPLE
    Request-vRAResourceAction -Id 6a301f8c-d868-4908-8348-80ad0eb35b00 -ResourceName vm01

    .EXAMPLE

    $JSON = @"
        {
            "type": "com.vmware.vcac.catalog.domain.request.CatalogResourceRequest",
            "resourceId": "448fcd09-b8c0-482c-abbc-b3ab818c2e31",
            "actionId": "fae08c75-3506-40f6-9c9b-35966fe9125c",
            "description": null,
            "data": {
                         "description": null,
                         "reasons": null
                     }
        }
    "@

    $JSON | Request-vRAResourceAction

#>

[CmdletBinding(SupportsShouldProcess,ConfirmImpact="High",DefaultParameterSetName="ByResourceId")][OutputType('System.Management.Automation.PSObject')]

    Param (

        [Parameter(Mandatory=$true,ParameterSetName="ByResourceId")]
        [Parameter(Mandatory=$true,ParameterSetName="ByResourceName")]
        [ValidateNotNullOrEmpty()]
        [Alias('Id')]
        [String]$ActionId,

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

        [Parameter(Mandatory=$true,ParameterSetName="ByResourceName")]
        [ValidateNotNullOrEmpty()]
        [String]$ResourceName,

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

    )

    Begin {

        xRequires -Version 7.0

    }

    Process {
                
         try {

            switch ($PsCmdlet.ParameterSetName) {

                'JSON' {

                    # --- Extract id's from json payload
                    $Body = $JSON | ConvertFrom-Json
                    $ResourceId = $Body.resourceId
                    $ActionId = $Body.actionId

                    break

                }

                'ByResourceName' {

                    # --- Get the resource id
                    Write-verbose -Message "Retrieving Id for resource $($Name)"
                    $Resource = Get-vRAResource -Name $ResourceName
                    $ResourceId = $Resource.ResourceId

                    break

                }

            }

            if (!$PSBoundParameters.ContainsKey("JSON")) {

                # --- Get the request template
                Write-Verbose -Message "Retrieving request template"

                $JSON = Get-vRAResourceActionRequestTemplate -ActionId $ActionId -ResourceId $ResourceId

            }

            # --- Execute the request
            if ($PSCmdlet.ShouldProcess($ResourceId)){

                $URI = "/catalog-service/api/consumer/resources/$($ResourceId)/actions/$($ActionId)/requests"

                Invoke-vRARestMethod -Method POST -URI $URI -Body $JSON -Verbose:$VerbosePreference | Out-Null

            }

        }
        catch [Exception]{

            throw

        }

    }

    End {

    }

}