Functions/Public/catalog-service/New-vRAService.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
function New-vRAService {
<#
    .SYNOPSIS
    Create a vRA Service for the current tenant
    
    .DESCRIPTION
    Create a vRA Service for the current tenant

    Currently unsupported interactive actions:

    * HoursStartTime
    * HoursEndTime
    * ChangeWindowDayOfWeek
    * ChangeWindowStartTime
    * ChangeWindowEndTime

    .PARAMETER Name
    The name of the service

    .PARAMETER Description
    A description of the service

    .PARAMETER Owner
    The owner of the service

    .PARAMETER SupportTeam
    The support team of the service

    .PARAMETER IconId
    The Icon Id of the service. This must already exist in the Service Catalog. Typically it would have already been created via Import-vRAServiceIcon

    .PARAMETER JSON
    A json string of type service (catalog-service/api/docs/el_ns0_service.html)
    
    .INPUTS
    System.String

    .OUTPUTS
    System.Management.Automation.PSObject

    .EXAMPLE
    New-vRAService -Name "New Service"
    
    .EXAMPLE
    New-vRAService -Name "New Service" -Description "A new service" -Owner user@vsphere.local -SupportTeam customgroup@vsphere.local -IconId "cafe_icon_Service01"
    
    .EXAMPLE
    $JSON = @"

        {
          "name": "New Service",
          "description": "A new Service",
          "status": "ACTIVE",
          "statusName": "Active",
          "version": 1,
          "organization": {
            "tenantRef": "Tenant01",
            "tenantLabel": "Tenant01",
            "subtenantRef": null,
            "subtenantLabel": null
          },
          "newDuration": null,
          "iconId": "cafe_default_icon_genericService"
        }
"@

    $JSON | New-vRAService
       
    
#>

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

    Param (
        
        [Parameter(Mandatory=$true,ParameterSetName="Standard")]
        [ValidateNotNullOrEmpty()]
        [String]$Name,
        
        [Parameter(Mandatory=$false,ParameterSetName="Standard")]
        [ValidateNotNullOrEmpty()]
        [String]$Description,            

        [Parameter(Mandatory=$false,ParameterSetName="Standard")]
        [ValidateNotNullOrEmpty()]
        [String]$Owner,

        [Parameter(Mandatory=$false,ParameterSetName="Standard")]
        [ValidateNotNullOrEmpty()]
        [String]$SupportTeam,

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

    Begin {
    
    }
    
    Process {

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

                $Data = ($JSON | ConvertFrom-Json)
        
                $Body = $JSON
                $Name = $Data.name
            }
            else {

                $Body = @"
                    {
                        "name": "$($Name)",
                        "description": "$($Description)",
                        "status": "ACTIVE",
                        "statusName": "Active",
                        "version": 1,
                        "organization": {
                            "tenantRef": "$($Global:vRAConnection.Tenant)",
                            "tenantLabel": null,
                            "subtenantRef": null,
                            "subtenantLabel": null
                        },
                        "newDuration": null,
                        "iconId": "cafe_default_icon_genericService"
                    }
"@


                # --- If certain parameters are specified, ConvertFrom-Json, update, then ConvertTo-Json
                if ($PSBoundParameters.ContainsKey("Owner") -or $PSBoundParameters.ContainsKey("SupportTeam") -or $PSBoundParameters.ContainsKey("IconId")){

                    $Object = $Body | ConvertFrom-Json

                    # --- Add owner catalogPrincipal
                    if ($PSBoundParameters.ContainsKey("Owner")) {

                        Write-Verbose -Message "Adding owner principal: $($Owner)"

                        $CatalogPrincipal = Get-vRACatalogPrincipal -Id $Owner   

                        $Object | Add-Member -MemberType NoteProperty -Name "owner" -Value $CatalogPrincipal                                         

                    }

                    # --- Add supportTeam catalogPrincipal
                    if ($PSBoundParameters.ContainsKey("SupportTeam")) {

                        Write-Verbose -Message "Adding support team principal: $($SupportTeam)"

                        $CatalogPrincipal = Get-vRACatalogPrincipal -Id $SupportTeam   

                        $Object | Add-Member -MemberType NoteProperty -Name "supportTeam" -Value $CatalogPrincipal

                    }

                    if ($PSBoundParameters.ContainsKey("IconId")) {

                        Write-Verbose -Message "Setting IconId: $($IconId)"

                        $Object.iconId = $IconId

                    }

                    $Body = $Object | ConvertTo-Json -Compress

                }
                        
            }
       
        # --- Create new service
        try {
            if ($PSCmdlet.ShouldProcess($Name)){
                
                # --- Build the URI string for the service
            
                $URI = "/catalog-service/api/services"
                           
                Invoke-vRARestMethod -Method POST -URI $URI -Body $Body -Verbose:$VerbosePreference | Out-Null
                Get-vRAService -Name "$($Name)"

            }

        }
        catch [Exception] {
            
            throw
            
        }
    
    }

    End {

    }

}