-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathterraform-onboarding.ps1
1037 lines (887 loc) · 37.1 KB
/
terraform-onboarding.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright © 2024. Citrix Systems, Inc. All Rights Reserved.
<#
Currently this script is still in TechPreview
.SYNOPSIS
Script to onboard an existing site to terraform.
.DESCRIPTION
The script should be able to collect the list of resources from DDC, import into terraform, and generate the TF skeletons.
.Parameter CustomerId
The Citrix Cloud customer ID. Only applicable for Citrix Cloud customers.
When omitted, the default value is "CitrixOnPremises" for on-premises use case.
.Parameter ClientId
The Client Id for Citrix DaaS service authentication.
For Citrix on-premises customers: Use this to specify a DDC administrator username.
For Citrix Cloud customers: Use this to specify Cloud API Key Client Id.
.Parameter ClientSecret
The Client Secret for Citrix DaaS service authentication.
For Citrix on-premises customers: Use this to specify a DDC administrator password.
For Citrix Cloud customers: Use this to specify Cloud API Key Client Secret.
.Parameter Hostname
The Host name / base URL of Citrix DaaS service.
For Citrix on-premises customers (Required): Use this to specify Delivery Controller hostname.
For Citrix Cloud customers (Optional): Use this to force override the Citrix DaaS service hostname.
.Parameter Environment
The Citrix Cloud environment of the customer. Only applicable for Citrix Cloud customers. Available options: Production, Staging
.Parameter SetDependencyRelationship
Create dependency relationships between resources by replacing resource IDs with resource references.
.Parameter DisableSSLValidation
Disable SSL validation for this script. Required if DDC does not have a valid SSL certificate.
.Parameter ShowClientSecret
Specifies whether to display the client secret value in the generated Terraform configuration file; defaults to `$false` for security.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $false)]
[string] $CustomerId = "CitrixOnPremises",
[Parameter(Mandatory = $true)]
[string] $ClientId,
[Parameter(Mandatory = $true)]
[string] $ClientSecret,
[Parameter(Mandatory = $false)]
[string] $DomainFqdn,
[Parameter(Mandatory = $false)]
[string] $Hostname = "api.cloud.com",
[Parameter(Mandatory = $false)]
[ValidateSet("Production", "Staging")]
[string] $Environment = "Production",
[Parameter(Mandatory = $false)]
[switch] $SetDependencyRelationship,
[Parameter(Mandatory = $false)]
[switch] $DisableSSLValidation,
[Parameter(Mandatory=$false)]
[switch] $ShowClientSecret
)
### Helper Functions ###
function Get-Site {
if ($script:onPremise) {
$siteRequest = "https://$script:hostname/citrix/orchestration/api/me"
}
else {
$siteRequest = "https://$script:hostname/cvad/manage/me"
}
$response = Start-GetRequest -url $siteRequest
$script:siteId = $response.Customers[0].Sites[0].Id
}
function Get-RequestBaseUrl {
if ($script:onPremise) {
$url = "https://$script:hostname/citrix/orchestration/api/CitrixOnPremises/$script:siteId"
}
else {
$url = "https://$script:hostname/cvad/manage"
}
$script:urlBase = $url
}
function Invoke-WebRequestWithRetry {
param(
[Parameter(Mandatory = $true)]
[string]$Uri,
[Parameter(Mandatory = $true)]
[string]$Method,
[Parameter(Mandatory = $false)]
[HashTable]$Headers = @{},
[Parameter(Mandatory = $false)]
[string]$ContentType = 'application/json',
[Parameter(Mandatory = $false)]
[HashTable]$Body,
[Parameter(Mandatory = $false)]
[int]$MaxRetries = 5,
[Parameter(Mandatory = $false)]
[double]$JitterFactor = 0.1
)
$attempt = 0
while ($true) {
try {
$attempt++
Write-Verbose "Attempting $Method $Uri..."
if ($DisableSSLValidation -and $PSVersionTable.PSVersion.Major -ge 7) {
$response = Invoke-WebRequest -Uri $Uri -Method $Method -Headers $Headers -ContentType $ContentType -Body $Body -SkipCertificateCheck
}
else {
$response = Invoke-WebRequest -Uri $Uri -Method $Method -Headers $Headers -ContentType $ContentType -Body $Body
}
return $response
}
catch {
if ($attempt -ge $MaxRetries) {
Write-Verbose "Max retries reached. Throwing exception."
throw
}
else {
$baseDelay = [math]::Pow(2, $attempt)
# This is a random delay that is added to the base delay to prevent a thundering herd problem where many instances of the function might be retrying at the same time.
# The jitter is a random number between 0 and 10% of the base delay.
$jitter = Get-Random -Minimum 0 -Maximum ([math]::Ceiling($baseDelay * $JitterFactor))
$delay = $baseDelay + $jitter
Write-Verbose "Error occurred, retrying $Method $Uri after $delay seconds..."
Start-Sleep -Seconds $delay
}
}
}
}
function Get-AuthToken {
if ($script:onPremise) {
$url = "https://$script:hostname/citrix/orchestration/api/tokens"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}\{1}:{2}" -f $script:domainFqdn, $script:clientId, $script:clientSecret)))
$basicAuth = "Basic $base64AuthInfo"
$response = Invoke-WebRequestWithRetry -Uri $url -Method 'POST' -Headers @{Authorization = $basicAuth }
$jsonObj = ConvertFrom-Json $([String]::new($response.Content))
return $jsonObj.Token
}
else {
# Return the token if its still valid
if ($null -eq $script:Token) {
Write-Verbose "Requesting new token."
}
elseif ((Get-Date) -lt $script:TokenExpiryTime) {
Write-Verbose "Refresh token is still valid. Returning the existing token."
return $script:Token
}
else {
Write-Verbose "Refresh token Expired. Requesting new token."
}
if ($script:environment -eq "Production") {
$url = "https://api.cloud.com/cctrustoauth2/$script:customerId/tokens/clients"
}
elseif ($script:environment -eq "Staging") {
$url = "https://api.cloudburrito.com/cctrustoauth2/$script:customerId/tokens/clients"
}
$body = @{
grant_type = 'client_credentials'
client_id = $script:clientId
client_secret = $script:clientSecret
}
$contentType = 'application/x-www-form-urlencoded'
$response = Invoke-WebRequestWithRetry -Uri $url -Method 'POST' -body $body -ContentType $contentType
$jsonObj = ConvertFrom-Json $([String]::new($response.Content))
# Save the new token and calculate the expiry time of the refresh token
$script:Token = $jsonObj.access_token
$script:TokenExpiryTime = (Get-Date).AddSeconds([int]($jsonObj.expires_in * 0.9)) # Calculate the expiry time of the refresh token with buffer
return $script:Token
}
}
function Start-GetRequest {
param(
[parameter(Mandatory = $true)][string] $url
)
$token = Get-AuthToken
if ($script:onPremise) {
$headers = @{
Authorization = "Bearer $token"
}
}
else {
$headers = @{
"Authorization" = "CwsAuth Bearer=$token"
"Citrix-CustomerId" = $script:customerId
"Accept" = "application/json, text/plain, */*"
}
if ($null -ne $script:siteId) {
$headers["Citrix-InstanceId"] = $script:siteId
}
}
$contentType = 'application/json'
$response = Invoke-WebRequestWithRetry -Uri $url -Method 'GET' -Headers $headers -ContentType $contentType
$jsonObj = ConvertFrom-Json $([String]::new($response.Content))
return $jsonObj
}
function New-RequiredFiles {
Write-Verbose "Creating required files for terraform."
# Determine the client secret value based on the ShowClientSecret flag
$secretValue = if ($ShowClientSecret) { $script:clientSecret } else { "<Input client secret value>" }
if (!(Test-Path ".\citrix.tf")) {
New-Item -path ".\" -name "citrix.tf" -type "file" -Force
Write-Verbose "Created new file for terraform citrix provider configuration."
}
if ($script:onPremise) {
$disable_ssl_verification = $script:disable_ssl.ToString().ToLower()
$config = @"
provider "citrix" {
cvad_config = {
hostname = "$script:hostname"
client_id = "$script:domainFqdn\\$script:clientId"
# client_secret = "$secretValue"
disable_ssl_verification = $disable_ssl_verification
}
}
"@
Set-Content -Path ".\citrix.tf" -Value $config
}
else {
$config = @"
provider "citrix" {
cvad_config = {
customer_id = "$script:customerId"
client_id = "$script:clientId"
# client_secret = "$secretValue"
hostname = "$script:hostname"
environment = "$script:environment"
}
}
"@
Set-Content -Path ".\citrix.tf" -Value $config
}
# Create temporary import.tf for terraform import
if (!(Test-Path ".\import.tf")) {
New-Item -path ".\" -name "import.tf" -type "file" -Force
Write-Verbose "Created new file for terraform import."
}
else {
Clear-Content -path ".\import.tf"
Write-Verbose "Cleared content in terraform import file."
}
# Create resource.tf for final terraform resources
if (!(Test-Path ".\resource.tf")) {
New-Item -path ".\" -name "resource.tf" -type "file" -Force
Write-Verbose "Created new file for terraform resource."
}
else {
Clear-Content -path ".\resource.tf"
Write-Verbose "Cleared content in terraform resource file."
}
Write-Verbose "Required files created successfully."
}
# Function to get the URL for WEM objects
function Get-UrlForWemObjects {
param(
[parameter(Mandatory = $true)]
[string] $requestPath
)
if ($script:environment -eq "Production") {
$script:wemHostName = "api.wem.cloud.com"
}
else {
$script:wemHostName = "api.wem.cloudburrito.com"
}
if ($requestPath -eq "sites") {
return "https://$script:wemHostName/services/wem/sites?includeHidden=true&includeUnboundAgentsSite=true"
}
else {
return "https://$script:wemHostName/services/wem/machines"
}
}
# Function to find Catalog AD objects
function Find-CatalogADObjects {
param(
[parameter(Mandatory = $true)]
[array] $items
)
$catalogItems = @()
foreach ($item in $items) {
if ($item.type -eq "Catalog") {
$catalogItems += $item
}
}
return $catalogItems
}
# Function to get list of resources for a given resource provider
function Get-ResourceList {
param(
[parameter(Mandatory = $true)]
[string] $requestPath,
[parameter(Mandatory = $true)]
[string] $resourceProviderName
)
$url = "$script:urlBase/$requestPath"
# Update url for WEM Objects
if ($resourceProviderName -in "wem_configuration_set", "wem_directory_object") {
$url = Get-UrlForWemObjects -requestPath $requestPath
}
# Check if the resource provider is supported in the current environment (eg. WEM is not supported for most environments)
try {
$response = Start-GetRequest -url $url
}
catch {
# Ignore 503 errors for WEM objects
if (-not($_.Exception.Response.StatusCode -eq 503)) {
Write-Error "Failed to get $resourceProviderName. Error: $($_.Exception.Message)" -ErrorAction Continue
}
return @()
}
$items = $response.Items
# WEM supports AD object type 'Catalog'. Filter out other object types
if ($resourceProviderName -eq "wem_directory_object" -and $items.Count -gt 0) {
$items = Find-CatalogADObjects -items $items
}
$resourceList = @()
$pathMap = @{}
foreach ($item in $items) {
# Handle special case for Machine Catalogs
if ($requestPath -eq "machinecatalogs" -and $item.provisioningType -ne "Manual" -and $item.provisioningType -ne "MCS" -and $item.provisioningType -ne "PVSStreaming") {
Write-Warning "Currently the citrix terraform provider only supports Manual and MCS Machine Catalogs. Ignoring the Machine Catalog with Name: $($item.name) and Type: $($item.provisioningType)"
continue;
}
# Handle special case for hypervisors
if ($requestPath -eq "hypervisors") {
if (($item.ConnectionType -eq $script:hypervisorResourceMap.$resourceProviderName) -and ($item.ConnectionType -ne "Custom" -or $item.PluginId -eq $NUTANIX_PLUGIN_ID)) {
$resourceList += $item.Id
}
# Skip other hypervisors
continue
}
# Handle special case for Policy Sets
if($requestPath -eq "gpo/policySets" -and $item.name -eq "DefaultSitePolicies")
{
# Skip processing for the default site policies
continue
}
#Handle special case for Built-in Admin Roles
if($requestPath -eq "Admin/Roles"){
if($item.IsBuiltIn){
continue;
}
}
# Handle special case for Built-in Admin Scopes
if ($requestPath -eq "Admin/Scopes") {
if ($item.IsBuiltIn) {
continue
}
}
# Handle special case for Icons
if ($requestPath -like "Icons*") {
if ($item.Id -eq "0" -or $item.Id -eq "1") {
continue
}
}
# Handle special case for Policies
if ($item.policySetGuid -and $item.policySetType -like "*Policies*") {
$resourceList += $item.policySetGuid
}
# Check for id and ignore empty and default values
if ($item.Id -and $item.Id -ne "0" -and $item.Id -ne "00000000-0000-0000-0000-000000000000") {
$resourceList += $item.Id
}
# Check for ServiceAccountUid for Service Accounts
if ($resourceProviderName -eq "service_account" -and $item.ServiceAccountUid){
$resourceList += $item.ServiceAccountUid
}
# Check for Security Identifier for Admin Users
if ($resourceProviderName -eq "admin_user" -and $item.User -and $item.User.Sid){
$resourceList += $item.User.Sid
}
# Create a path map for ApplicationFolder paths
if ($requestPath -eq "AdminFolders") {
$pathMap[$item.Id] = $item.Path
}
# Store icons as files
if ($requestPath -like "Icons*") {
$iconsFolder = Join-Path -Path $PSScriptRoot -ChildPath "icons"
# Create the icons folder
if (-not (Test-Path -Path $iconsFolder)) {
New-Item -ItemType Directory -Path $iconsFolder | Out-Null
}
$iconBytes = [System.Convert]::FromBase64String($item.RawData)
$iconFileName = "$iconsFolder\app_icon_$($item.Id).ico"
try {
[System.IO.File]::WriteAllBytes($iconFileName, $iconBytes)
}
catch {
Write-Error "Failed to write icon file: $_"
continue
}
}
}
return $resourceList, $pathMap
}
# Function to get import map for each resource
function Get-ImportMap {
param(
[parameter(Mandatory = $true)]
[string] $resourceApi,
[parameter(Mandatory = $true)]
[string] $resourceProviderName,
[parameter(Mandatory = $false)]
[string] $parentId = "",
[parameter(Mandatory = $false)]
[int] $parentIndex = 0
)
$list, $pathMap = Get-ResourceList -requestPath $resourceApi -resourceProviderName $resourceProviderName
$resourceMap = @{}
$index = 0
foreach ($id in $list) {
if ($parentId -ne "") {
$resourceName = "$($resourceProviderName)_$($parentIndex)_$($index)"
$resourceMapKey = "$($parentId),$($id)"
if (-not $script:parentChildMap.ContainsKey($parentId)) {
# Initialize as a new list if not already present
$script:parentChildMap[$parentId] = [System.Collections.Generic.List[string]]::new()
}
$script:parentChildMap[$parentId].Add($id)
}
else {
$resourceName = "$($resourceProviderName)_$($index)"
$resourceMapKey = $id
}
if ($resourceApi -eq "AdminFolders" -and $pathMap.Count -gt 0) {
$script:applicationFolderPathMap[$pathMap.$id.TrimEnd('\')] = $resourceName
}
$resourceMap[$resourceMapKey] = $resourceName
$resourceContent = "resource `"citrix_$resourceProviderName`" `"$resourceName`" {}`n"
Add-Content -Path ".\import.tf" -Value $resourceContent
$index += 1
}
return $resourceMap
}
# List all CVAD objects from existing site
function Get-ExistingCVADResources {
Write-Verbose "Get list of all existing CVAD resources from the site."
$resources = @{
"zone" = @{
"resourceApi" = "zones"
"resourceProviderName" = "zone"
}
"azure_hypervisor" = @{
"resourceApi" = "hypervisors"
"resourceProviderName" = "azure_hypervisor"
}
"aws_hypervisor" = @{
"resourceApi" = "hypervisors"
"resourceProviderName" = "aws_hypervisor"
}
"gcp_hypervisor" = @{
"resourceApi" = "hypervisors"
"resourceProviderName" = "gcp_hypervisor"
}
"scvmm_hypervisor" = @{
"resourceApi" = "hypervisors"
"resourceProviderName" = "scvmm_hypervisor"
}
"xenserver_hypervisor" = @{
"resourceApi" = "hypervisors"
"resourceProviderName" = "xenserver_hypervisor"
}
"vsphere_hypervisor" = @{
"resourceApi" = "hypervisors"
"resourceProviderName" = "vsphere_hypervisor"
}
"nutanix_hypervisor" = @{
"resourceApi" = "hypervisors"
"resourceProviderName" = "nutanix_hypervisor"
}
"machine_catalog" = @{
"resourceApi" = "machinecatalogs"
"resourceProviderName" = "machine_catalog"
}
"delivery_group" = @{
"resourceApi" = "deliverygroups"
"resourceProviderName" = "delivery_group"
}
"admin_scope" = @{
"resourceApi" = "Admin/Scopes"
"resourceProviderName" = "admin_scope"
}
"admin_role" = @{
"resourceApi" = "Admin/Roles"
"resourceProviderName" = "admin_role"
}
"policy_set" = @{
"resourceApi" = "gpo/policySets"
"resourceProviderName" = "policy_set"
}
"application" = @{
"resourceApi" = "Applications"
"resourceProviderName" = "application"
}
"admin_folder" = @{
"resourceApi" = "AdminFolders"
"resourceProviderName" = "admin_folder"
}
"application_group" = @{
"resourceApi" = "ApplicationGroups"
"resourceProviderName" = "application_group"
}
"application_icon" = @{
"resourceApi" = "Icons?builtIn=false"
"resourceProviderName" = "application_icon"
}
"service_account" = @{
"resourceApi" = "Identity/ServiceAccounts"
"resourceProviderName" = "service_account"
}
"image_definition" = @{
"resourceApi" = "ImageDefinitions"
"resourceProviderName" = "image_definition"
}
"storefront_server" = @{
"resourceApi" = "StoreFrontServers"
"resourceProviderName" = "storefront_server"
}
"tag" = @{
"resourceApi" = "Tags"
"resourceProviderName" = "tag"
}
}
# Add WEM resources for cloud customer environment
if (-not($script:onPremise)) {
$wemResources = @{
"wem_configuration_set" = @{
"resourceApi" = "sites"
"resourceProviderName" = "wem_configuration_set"
}
"wem_directory_object" = @{
"resourceApi" = "ad_objects"
"resourceProviderName" = "wem_directory_object"
}
}
$resources += $wemResources
}else {
# If On-Prem add admin resource
$resources.Add("admin_user", @{
"resourceApi" = "Admin/Administrators"
"resourceProviderName" = "admin_user"
})
}
$script:cvadResourcesMap = @{}
foreach ($resource in $resources.Keys) {
$api = $resources[$resource].resourceApi
$resourceProviderName = $resources[$resource].resourceProviderName
$script:cvadResourcesMap[$resource] = Get-ImportMap -resourceApi $api -resourceProviderName $resourceProviderName
# Create resource pool map for each hypervisor if exists
if ($resource -like "*hypervisor") {
$index = 0
foreach ($id in $script:cvadResourcesMap[$resource].Keys) {
$resourcePoolAPI = "hypervisors/$($id)/resourcePools"
$script:cvadResourcesMap["$($resource)_resource_pool"] += Get-ImportMap -resourceApi $resourcePoolAPI -resourceProviderName "$($resource)_resource_pool" -parentId $id -parentIndex $index
$index += 1
}
}
# Create image_version map for all image_definitions
if ($resource -like "image_definition") {
$index = 0
foreach ($id in $script:cvadResourcesMap[$resource].Keys) {
$resourcePoolAPI = "ImageDefinitions/$($id)/ImageVersions"
$script:cvadResourcesMap["image_version"] += Get-ImportMap -resourceApi $resourcePoolAPI -resourceProviderName "image_version" -parentId $id -parentIndex $index
$index += 1
}
}
}
Write-Verbose "Successfully retrieved all CVAD resources from the site."
}
# Function to import terraform resources into state
function Import-ResourcesToState {
Write-Verbose "Importing terraform resources into state."
foreach ($resource in $script:cvadResourcesMap.Keys) {
foreach ($id in $script:cvadResourcesMap[$resource].Keys) {
terraform import "citrix_$($resource).$($script:cvadResourcesMap[$resource][$id])" "$id"
}
}
Write-Verbose "Successfully imported resources into state."
}
function PostProcessProviderConfig {
Write-Verbose "Post-processing provider config."
# Post-process the provider config output in citrix.tf
$content = Get-Content -Path ".\citrix.tf" -Raw
# Uncomment field for client secret in provider config
$content = $content -replace "# ", ""
# Overwrite provider config with processed value
Set-Content -Path ".\citrix.tf" -Value $content
}
function RemoveComputedPropertiesForZone {
param(
[parameter(Mandatory = $true)]
[string] $content
)
if ($script:onPremise) {
Write-Verbose "Removing computed properties for zone resource in on-premises."
# Remove resource_location_id property from each zone resource for on-premises
$resourceLocationIdRegex = "(\s+)resource_location_id(\s+)= (\S+)"
$content = $content -replace $resourceLocationIdRegex, ""
}
else {
Write-Verbose "Removing computed properties for zone resource in cloud."
# Remove name property from each zone resource in cloud
$filteredOutput = @()
$lines = $content -split "`r?`n"
foreach ($line in $lines) {
if ($line -like 'resource "citrix_zone"*') {
$insideCitrixZone = $true
}
if ($insideCitrixZone -and $line -like '*name*') {
continue
}
if ($insideCitrixZone -and $line -like '}*') {
$insideCitrixZone = $false
}
$filteredOutput += $line
}
$content = $filteredOutput -join "`n"
}
return $content
}
function RemoveComputedProperties {
param(
[parameter(Mandatory = $true)]
[string] $content
)
Write-Verbose "Removing computed properties from terraform output."
# Define an array of regex patterns to remove computed properties
$regexPatterns = @(
"(\s+)id(\s+)= (\S+)",
'(\s+)path\s*=\s*"(.*?)"',
"(\s+)assigned(\s+)= (\S+)",
"(\s+)is_built_in(\s+)= (\S+)",
"(\s+)built_in_scopes\s*=\s*\[[\s\S]*?\]",
"(\s+)inherited_scopes\s*=\s*\[[\s\S]*?\]",
"(\s+)total_application_groups(\s+)= (\S+)",
"(\s+)total_applications(\s+)= (\S+)",
"(\s+)total_delivery_groups(\s+)= (\S+)",
"(\s+)total_machine_catalogs(\s+)= (\S+)",
"(\s+)total_machines(\s+)= (\S+)",
"(\s+)is_all_scope(\s+)= (\S+)",
"(\s+)latest_version(\s+)= (\S+)",
"(\s+)version_number(\s+)= (\S+)",
"(\s+)associated_delivery_group_count(\s+)= (\S+)",
"(\s+)associated_machine_catalog_count(\s+)= (\S+)",
"(\s+)associated_machine_count(\s+)= (\S+)",
"(\s+)associated_application_group_count(\s+)= (\S+)",
"(\s+)associated_application_count(\s+)= (\S+)",
"(\s+)tenant_id(\s+)= (\S+)",
"(\s+)tenant_name(\s+)= (\S+)",
"(\s+)tenants\s*=\s*\[[\s\S]*?\]"
)
# Identify the delivery_groups_priority block
$deliveryGroupsPriorityPattern = "(\s*)delivery_groups_priority\s*=\s*\[[\s\S]*?\]"
$deliveryGroupsPriorityMatches = [regex]::Matches($content, $deliveryGroupsPriorityPattern, [System.Text.RegularExpressions.RegexOptions]::Singleline)
# Extract the delivery_groups_priority block and replace it with a unique placeholder
$index = 0
foreach ($match in $deliveryGroupsPriorityMatches) {
$deliveryGroupsPriorityBlock = $match.Value
$content = $content -replace [regex]::Escape($deliveryGroupsPriorityBlock), "PLACEHOLDER_DELIVERY_GROUPS_PRIORITY_$index"
$index++
}
# Loop through each regex pattern and replace matches in the content
foreach ($pattern in $regexPatterns) {
$content = [regex]::Replace($content, $pattern, "", [System.Text.RegularExpressions.RegexOptions]::Multiline)
}
# Restore the delivery_groups_priority block using unique placeholders
$index = 0
foreach ($match in $deliveryGroupsPriorityMatches) {
$content = $content -replace "PLACEHOLDER_DELIVERY_GROUPS_PRIORITY_$index", $match.Value
$index++
}
# Remove contents for zone resource
$content = RemoveComputedPropertiesForZone -content $content
Write-Verbose "Computed properties removed successfully."
return $content
}
function ReplaceDependencyRelationships {
param(
[parameter(Mandatory = $true)]
[string] $content
)
if (-not $script:SetDependencyRelationship) {
return $content
}
Write-Verbose "Creating dependency relationships between resources."
# Create dependency relationships between resources with id references
foreach ($resource in $script:cvadResourcesMap.Keys) {
if ($resource -like "wem_*") {
continue
}
foreach ($id in $script:cvadResourcesMap[$resource].Keys) {
if($resource -like "*_resource_pool" -or $resource -like "image_version") {
$idArray = $id -split ","
if($idArray.Count -gt 1) {
$resource_id = $idArray[1]
Write-Verbose "Replacing ID: $resource_id with citrix_$($resource).$($script:cvadResourcesMap[$resource][$id]).id"
$content = $content -replace "`"$resource_id`"", "citrix_$($resource).$($script:cvadResourcesMap[$resource][$id]).id"
}
}else{
Write-Verbose "Replacing ID: $id with citrix_$($resource).$($script:cvadResourcesMap[$resource][$id]).id"
$content = $content -replace "`"$id`"", "citrix_$($resource).$($script:cvadResourcesMap[$resource][$id]).id"
}
}
}
# Create dependency relationships between resources with path references
foreach ( $applicationFolderPath in $script:applicationFolderPathMap.Keys) {
$path = $applicationFolderPath.replace("\", "\\\\")
$content = $content -replace "(\s(parent_path|application_folder_path|application_group_folder_path|delivery_group_folder_path|machine_catalog_folder_path)\s+= )(`"$path`")", "`${1}citrix_admin_folder.$($script:applicationFolderPathMap[$applicationFolderPath]).path"
}
return $content
}
function InjectPlaceHolderSensitiveValues {
param(
[parameter(Mandatory = $true)]
[string] $content
)
$filteredOutput = @()
$lines = $content -split "`r?`n"
$iconsFolder = Join-Path -Path $PSScriptRoot -ChildPath "icons"
$previousLine = ""
foreach ($line in $lines) {
if ($line -match '^\s*resource\s*"citrix_image_version"\s*') {
$insideCitrixImageVersion = $true
} elseif ($insideCitrixImageVersion -and $line -eq "}") {
$insideCitrixImageVersion = $false
}
if ($line -match '^\s*resource\s*"citrix_image_definition"\s*') {
$insideCitrixImageDefinition = $true
} elseif ($insideCitrixImageDefinition -and $line -eq "}") {
$insideCitrixImageDefinition = $false
}
# Skip os_type and session_support if inside citrix_image_version
if ($insideCitrixImageVersion -and ($line -match '^\s*os_type\s*=' -or $line -match '^\s*session_support\s*=')) {
Write-Verbose "Removing os_type or session_support from citrix_image_version."
continue
}
if ($insideCitrixImageDefinition -and $line -match '^\s*hypervisor\s*=\s*"(.*)"') {
$hypervisorId = $matches[1]
$resourcePoolId = if ($script:parentChildMap.ContainsKey($hypervisorId) -and $script:parentChildMap[$hypervisorId].Count -gt 0) {
$script:parentChildMap[$hypervisorId][0]
} else {
"<Enter hypervisor pool id>"
}
$filteredOutput += $line
$filteredOutput += "hypervisor_resource_pool = `"$resourcePoolId`""
} elseif ($line -match 'raw_data' -and $previousLine -match 'id\s*=\s*"(.*)"') {
$iconId = $matches[1]
$iconFileName = "$iconsFolder\app_icon_$iconId.ico"
# Replace backslashes with double backslashes for terraform
$iconFileName = $iconFileName -replace '\\', '\\'
# Replace raw_data value with icon file path using filebase64 to encode a file's content in base64 format
$line = 'raw_data = filebase64("' + $iconFileName + '")'
$filteredOutput += $line
}elseif ($line -match '.*=\s*null') {
Write-Verbose "Ignoring lines with null values."
continue
}
elseif ($line -match '^\s*[^=]+\s*=\s*""') {
Write-Verbose "Ignoring lines with empty strings."
continue
}elseif($previousLine -match 'citrix_service_account' -and $line -match 'account_id'){
$filteredOutput += $line
$filteredOutput += 'account_secret = "<input application_secret value>"'
$filteredOutput += 'account_secret_format = "PlainText"'
}
elseif ($line -match "application_id") {
$filteredOutput += $line
$filteredOutput += 'application_secret = "<input application_secret value>"'
}
elseif ($line -match "api_key") {
$filteredOutput += $line
$filteredOutput += 'secret_key = "<input secret_key value>"'
}
elseif ($line -match "username") {
$filteredOutput += $line
$filteredOutput += 'password = "<input password value>"'
$filteredOutput += 'password_format = "PlainText"'
}
elseif (($previousLine -match "^\s*domain\s*=\s*.*$" -or $previousLine -match "^\s*domain_ou\s*=") -and $line -match "^\s*}\s*$") {
$filteredOutput += 'service_account = "<input service_account value>"'
$filteredOutput += 'service_account_password = "<input service_account_password value>"'
$filteredOutput += $line
}
else {
$filteredOutput += $line
}
$previousLine = $line
}
$content = $filteredOutput -join "`n"
return $content
}
function OrganizeTerraformResources {
param(
[parameter(Mandatory = $true)]
[string] $content
)
Write-Verbose "Organizing terraform resources into separate files."
# Post-process the terraform output
$content = Get-Content -Path ".\resource.tf" -Raw
# Regular expression to match resource blocks starting with # and ending with an empty line
$resourcePattern = '(#\s*(\w+)\.\w+:\s*.*?)(\n\s*\n|\s*$)'
# Find all resource blocks
$resources = [regex]::Matches($content, $resourcePattern, [System.Text.RegularExpressions.RegexOptions]::Singleline)
# Create a new .tf file for each resource type in its respective folder
foreach ($resource in $resources) {
$resourceBlock = $resource.Groups[1].Value
$resourceType = $resource.Groups[2].Value
$filename = "$resourceType.tf"
# Append the resource block to the file
Add-Content -Path $filename -Value $resourceBlock
Add-Content -Path $filename -Value "`n" # Add a newline for separation
}
Write-Verbose "Resource files created successfully."
}
function PostProcessTerraformOutput {
# Post-process the terraform output
$content = Get-Content -Path ".\resource.tf" -Raw
# Inject placeholder for sensitive values in tf
$content = InjectPlaceHolderSensitiveValues -content $content
# Set dependency relationships
$content = ReplaceDependencyRelationships -content $content
# Remove computed properties
$content = RemoveComputedProperties -content $content
# Overwrite extracted terraform with processed value
Set-Content -Path ".\resource.tf" -Value $content
# Organize terraform resources into separate files
OrganizeTerraformResources -content $content
}
if ($DisableSSLValidation -and $PSVersionTable.PSVersion.Major -lt 7) {
$code = @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) {
return true;
}
}
"@
Add-Type -TypeDefinition $code -Language CSharp
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
}
# Initialize script variables
$script:onPremise = ($CustomerId -eq "CitrixOnPremises")
$script:customerId = $CustomerId
$script:clientId = $ClientId
$script:clientSecret = $ClientSecret
$script:domainFqdn = $DomainFqdn
$script:hostname = $Hostname
$script:environment = $Environment
$script:disable_ssl = $DisableSSLValidation
$script:hypervisorResourceMap = @{
"azure_hypervisor" = "AzureRM"
"aws_hypervisor" = "AWS"
"gcp_hypervisor" = "GoogleCloudPlatform"
"scvmm_hypervisor" = "SCVMM"
"xenserver_hypervisor" = "XenServer"
"vsphere_hypervisor" = "VCenter"
"nutanix_hypervisor" = "Custom"
}
$NUTANIX_PLUGIN_ID = "AcropolisFactory"
$script:applicationFolderPathMap = @{}
$script:parentChildMap = @{} # Initialize the parent-child map for hypervisors and image_definitions
$script:TokenExpiryTime = (Get-Date).AddMinutes(-1) # Initialize the expiry time of the refresh token to an earlier time
# Set environment variables for client secret
$env:CITRIX_CLIENT_SECRET = $ClientSecret
try {
Get-Site
Get-RequestBaseUrl
New-RequiredFiles
# Get CVAD resources from existing site