/
createorupdatespaces.go
1103 lines (984 loc) · 39.4 KB
/
createorupdatespaces.go
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
package createorupdatespaces
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"os/exec"
"strings"
"text/template"
"time"
)
type OrgListJson struct {
Pagination struct {
TotalResults int `json:"total_results"`
TotalPages int `json:"total_pages"`
First struct {
Href string `json:"href"`
} `json:"first"`
Last struct {
Href string `json:"href"`
} `json:"last"`
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
} `json:"pagination"`
Resources []struct {
GUID string `json:"guid"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Suspended bool `json:"suspended"`
Relationships struct {
Quota struct {
Data struct {
GUID string `json:"guid"`
} `json:"data"`
} `json:"quota"`
} `json:"relationships"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Domains struct {
Href string `json:"href"`
} `json:"domains"`
DefaultDomain struct {
Href string `json:"href"`
} `json:"default_domain"`
Quota struct {
Href string `json:"href"`
} `json:"quota"`
} `json:"links"`
Metadata struct {
Labels struct {
} `json:"labels"`
Annotations struct {
} `json:"annotations"`
} `json:"metadata"`
} `json:"resources"`
}
type SpaceIsoJson struct {
Data struct {
GUID string `json:"guid"`
} `json:"data"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Related struct {
Href string `json:"href"`
} `json:"related"`
} `json:"links"`
}
type IsoJson struct {
Pagination struct {
TotalResults int `json:"total_results"`
TotalPages int `json:"total_pages"`
First struct {
Href string `json:"href"`
} `json:"first"`
Last struct {
Href string `json:"href"`
} `json:"last"`
Next struct {
Href string `json:"href"`
} `json:"next"`
Previous interface{} `json:"previous"`
} `json:"pagination"`
Resources []struct {
GUID string `json:"guid"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Organizations struct {
Href string `json:"href"`
} `json:"organizations"`
} `json:"links"`
Metadata struct {
Annotations struct {
} `json:"annotations"`
Labels struct {
} `json:"labels"`
} `json:"metadata"`
} `json:"resources"`
}
type ProtectedList struct {
Org []string `yaml:"Org"`
Quota []string `yaml:"quota"`
DefaultRunningSecurityGroup string `yaml:"DefaultRunningSecurityGroup"`
}
type List struct {
OrgList []struct {
Name string `yaml:"Name"`
Quota string `yaml:"Quota"`
} `yaml:"OrgList"`
Audit string `yaml:"Audit"`
}
type GitList struct {
OrgList []struct {
Name string `yaml:"Name"`
Repo string `yaml:"Repo"`
Quota string `yaml:"Quota"`
Branch string `yaml:"Branch"`
} `yaml:"OrgList"`
Audit string `yaml:"Audit"`
}
type InitClusterConfigVals struct {
ClusterDetails struct {
EndPoint string `yaml:"EndPoint"`
User string `yaml:"User"`
Org string `yaml:"Org"`
Space string `yaml:"Space"`
EnableASG bool `yaml:"EnableASG"`
EnableGitSubTree bool `yaml:"EnableGitSubTree"`
GitHost string `yaml:"GitHost"`
SetOrgAuditor bool `yaml:"SetOrgAuditor"`
SetOrgManager bool `yaml:"SetOrgManager"`
SetSpaceAuditor bool `yaml:"SetSpaceAuditor"`
SetSpaceManager bool `yaml:"SetSpaceManager"`
SetSpaceDeveloper bool `yaml:"SetSpaceDeveloper"`
EnableSpaceAudit bool `yaml:"EnableSpaceAudit"`
EnableUserAudit bool `yaml:"EnableUserAudit"`
EnableASGAudit bool `yaml:"EnableASGAudit"`
EnableIsolationAudit bool `yaml:"EnableIsolationAudit"`
SSOProvider string `yaml:"SSOProvider"`
} `yaml:"ClusterDetails"`
}
type Orglist struct {
Org struct {
Name string `yaml:"Name"`
//Quota string `yaml:"Quota"`
OrgUsers struct {
LDAP struct {
OrgManagers []string `yaml:"OrgManagers"`
OrgAuditors []string `yaml:"OrgAuditors"`
} `yaml:"LDAP"`
SSO struct {
OrgManagers []string `yaml:"OrgManagers"`
OrgAuditors []string `yaml:"OrgAuditors"`
} `yaml:"SSO"`
UAA struct {
OrgManagers []string `yaml:"OrgManagers"`
OrgAuditors []string `yaml:"OrgAuditors"`
} `yaml:"UAA"`
} `yaml:"OrgUsers"`
Spaces []struct {
Name string `yaml:"Name"`
IsolationSeg string `yaml:"IsolationSeg"`
ASG string `yaml:"ASG"`
SpaceUsers struct {
LDAP struct {
SpaceManagers []string `yaml:"SpaceManagers"`
SpaceDevelopers []string `yaml:"SpaceDevelopers"`
SpaceAuditors []string `yaml:"SpaceAuditors"`
} `yaml:"LDAP"`
UAA struct {
SpaceManagers []string `yaml:"SpaceManagers"`
SpaceDevelopers []string `yaml:"SpaceDevelopers"`
SpaceAuditors []string `yaml:"SpaceAuditors"`
} `yaml:"UAA"`
SSO struct {
SpaceManagers []string `yaml:"SpaceManagers"`
SpaceDevelopers []string `yaml:"SpaceDevelopers"`
SpaceAuditors []string `yaml:"SpaceAuditors"`
} `yaml:"SSO"`
} `yaml:"SpaceUsers"`
} `yaml:"Spaces"`
} `yaml:"Org"`
SpaceAudit string `yaml:"SpaceAudit"`
UserAudit string `yaml:"UserAudit"`
ASGAudit string `yaml:"ASGAudit"`
IsolationAudit string `yaml:"IsolationAudit"`
}
type SpaceStateYaml struct {
SpaceState struct {
Org string `yaml:"Org"`
OrgGuid string `yaml:"OrgGuid"`
OldSpaceName string `yaml:"OldSpaceName"`
NewSpaceName string `yaml:"NewSpaceName"`
SpaceGuid string `yaml:"SpaceGuid"`
} `yaml:"SpaceState"`
}
type SpaceListJson struct {
Pagination struct {
TotalResults int `json:"total_results"`
TotalPages int `json:"total_pages"`
First struct {
Href string `json:"href"`
} `json:"first"`
Last struct {
Href string `json:"href"`
} `json:"last"`
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
} `json:"pagination"`
Resources []struct {
GUID string `json:"guid"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Relationships struct {
Organization struct {
Data struct {
GUID string `json:"guid"`
} `json:"data"`
} `json:"organization"`
Quota struct {
Data interface{} `json:"data"`
} `json:"quota"`
} `json:"relationships"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Organization struct {
Href string `json:"href"`
} `json:"organization"`
Features struct {
Href string `json:"href"`
} `json:"features"`
ApplyManifest struct {
Href string `json:"href"`
Method string `json:"method"`
} `json:"apply_manifest"`
} `json:"links"`
Metadata struct {
Labels struct {
} `json:"labels"`
Annotations struct {
} `json:"annotations"`
} `json:"metadata"`
} `json:"resources"`
}
func CreateOrUpdateSpaces(clustername string, cpath string, ostype string) error {
var ProtectedOrgs ProtectedList
var list List
var gitlist GitList
var InitClusterConfigVals InitClusterConfigVals
var ListYml string
var LenList int
spath := cpath + "/" + clustername + "-state/"
ConfigFile := cpath + "/" + clustername + "/config.yml"
fileConfigYml, err := ioutil.ReadFile(ConfigFile)
if err != nil {
fmt.Println(err)
}
err = yaml.Unmarshal([]byte(fileConfigYml), &InitClusterConfigVals)
if err != nil {
panic(err)
}
if InitClusterConfigVals.ClusterDetails.EnableGitSubTree != true {
ListYml = cpath + "/" + clustername + "/OrgsList.yml"
fileOrgYml, err := ioutil.ReadFile(ListYml)
if err != nil {
fmt.Println(err)
}
err = yaml.Unmarshal([]byte(fileOrgYml), &list)
if err != nil {
panic(err)
}
LenList = len(list.OrgList)
} else {
ListYml = cpath + "/" + clustername + "/GitOrgsList.yml"
fileOrgYml, err := ioutil.ReadFile(ListYml)
if err != nil {
fmt.Println(err)
}
err = yaml.Unmarshal([]byte(fileOrgYml), &gitlist)
if err != nil {
panic(err)
}
LenList = len(gitlist.OrgList)
}
var OrgsYml string
ProtectedOrgsYml := cpath + "/" + clustername + "/ProtectedResources.yml"
fileProtectedYml, err := ioutil.ReadFile(ProtectedOrgsYml)
if err != nil {
fmt.Println(err)
}
err = yaml.Unmarshal([]byte(fileProtectedYml), &ProtectedOrgs)
if err != nil {
panic(err)
}
LenProtectedOrgs := len(ProtectedOrgs.Org)
EnableIsolationAudit := InitClusterConfigVals.ClusterDetails.EnableIsolationAudit
for i := 0; i < LenList; i++ {
var count, totalcount int
var OrgName string
if InitClusterConfigVals.ClusterDetails.EnableGitSubTree != true {
OrgName = list.OrgList[i].Name
} else {
OrgName = gitlist.OrgList[i].Name
//RepoName = gitlist.OrgList[i].Name
}
fmt.Println(" ")
fmt.Println("Org: ", OrgName)
for p := 0; p < LenProtectedOrgs; p++ {
//fmt.Println("Protected Org: ", ProtectedOrgs.Org[p], ",", list.OrgList[i])
if ProtectedOrgs.Org[p] == OrgName {
count = 1
} else {
count = 0
}
totalcount = totalcount + count
}
if totalcount == 0 {
//fmt.Println("This is not Protected Org")
if ostype == "windows" {
OrgsYml = cpath + "\\" + clustername + "\\" + OrgName + "\\Org.yml"
} else {
OrgsYml = cpath + "/" + clustername + "/" + OrgName + "/Org.yml"
}
fileOrgYml, err := ioutil.ReadFile(OrgsYml)
if err != nil {
fmt.Println(err)
}
var Orgs Orglist
err = yaml.Unmarshal([]byte(fileOrgYml), &Orgs)
if err != nil {
panic(err)
}
if OrgName == Orgs.Org.Name {
var getorg *exec.Cmd
if ostype == "windows" {
path := "\"" + "/v3/organizations?names=" + Orgs.Org.Name + "\""
getorg = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_orgdetails.json")
} else {
path := "/v3/organizations?names=" + Orgs.Org.Name
getorg = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_orgdetails.json")
}
err := getorg.Run()
if err == nil {
// fmt.Println(getorg, getorg.Stdout, getorg.Stderr)
} else {
fmt.Println("err", getorg, getorg.Stdout, getorg.Stderr)
}
fileSpaceJson, err := ioutil.ReadFile("CreateOrUpdateSpaces_orgdetails.json")
if err != nil {
fmt.Println(err)
}
//var Orgs Orglist
var orgdetails OrgListJson
if err := json.Unmarshal(fileSpaceJson, &orgdetails); err != nil {
panic(err)
}
OrgLen := len(orgdetails.Resources)
if OrgLen != 0 {
target := exec.Command("cf", "t", "-o", Orgs.Org.Name)
if _, err := target.Output(); err == nil {
orgguid := orgdetails.Resources[0].GUID
SpaceLen := len(Orgs.Org.Spaces)
for j := 0; j < SpaceLen; j++ {
//Getting Org Guid from state file
fullpath := spath + Orgs.Org.Name + "_" + Orgs.Org.Spaces[j].Name + "_SpaceState.yml"
var SpaceGuidPull string
SpaceStateYml := fullpath
fileSpaceStateYml, err := ioutil.ReadFile(SpaceStateYml)
if err != nil {
fmt.Println(err)
}
var spacestatedetails SpaceStateYaml
err = yaml.Unmarshal([]byte(fileSpaceStateYml), &spacestatedetails)
if err != nil {
panic(err)
}
SpaceStateGuid := spacestatedetails.SpaceState.SpaceGuid
//Checking if space exist with Guid from State File
var getspaceguid *exec.Cmd
if ostype == "windows" {
path := "\"" + "/v3/spaces?guids=" + SpaceStateGuid + "\""
getspaceguid = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_guid.json")
} else {
path := "/v3/spaces?guids=" + SpaceStateGuid
getspaceguid = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_guid.json")
}
err = getspaceguid.Run()
if err == nil {
// fmt.Println(getorg, getorg.Stdout, getorg.Stderr)
} else {
fmt.Println("err", getspaceguid, getspaceguid.Stdout, getspaceguid.Stderr)
}
fileSpaceGuidJson, err := ioutil.ReadFile("CreateOrUpdateSpaces_spacedetails_guid.json")
if err != nil {
fmt.Println(err)
}
var spacedetailsguid SpaceListJson
if err := json.Unmarshal(fileSpaceGuidJson, &spacedetailsguid); err != nil {
panic(err)
}
// Checking if space exist with space Name in Org
var getspacename *exec.Cmd
if ostype == "windows" {
path := "\"" + "/v3/spaces?names=" + Orgs.Org.Spaces[j].Name + "&organization_guids=" + orgguid + "\""
getspacename = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_name.json")
} else {
path := "/v3/spaces?names=" + Orgs.Org.Spaces[j].Name + "&organization_guids=" + orgguid
getspacename = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_name.json")
}
err = getspacename.Run()
if err == nil {
// fmt.Println(getorg, getorg.Stdout, getorg.Stderr)
} else {
fmt.Println("err", getspacename, getspacename.Stdout, getspacename.Stderr)
}
fileSpaceNameJson, err := ioutil.ReadFile("CreateOrUpdateSpaces_spacedetails_name.json")
if err != nil {
fmt.Println(err)
}
var spacedetailsname SpaceListJson
if err := json.Unmarshal(fileSpaceNameJson, &spacedetailsname); err != nil {
panic(err)
}
SpaceStateGuidLen := len(spacedetailsguid.Resources)
SpaceStateNameLen := len(spacedetailsname.Resources)
if SpaceStateGuidLen != 0 {
//fmt.Println("Space exists in state and platform")
spacename := spacedetailsguid.Resources[0].Name
if Orgs.Org.Spaces[j].Name == spacename {
SpaceGuidPull = spacedetailsguid.Resources[0].GUID
var spacedets SpaceListJson
if err := json.Unmarshal(fileSpaceNameJson, &spacedets); err != nil {
panic(err)
}
spaceguid := spacedets.Resources[0].GUID
var getisoguid *exec.Cmd
if ostype == "windows" {
path := "\"" + "/v3/spaces/" + spaceguid + "/relationships/isolation_segment" + "\""
getisoguid = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spaceisodetails.json")
} else {
path := "/v3/spaces/" + spaceguid + "/relationships/isolation_segment"
getisoguid = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spaceisodetails.json")
}
err := getisoguid.Run() // it can be nill
if err == nil {
//fmt.Println(getspace, getspace.Stdout, getspace.Stderr)
} else {
fmt.Println("err", getisoguid, getisoguid.Stdout, getisoguid.Stderr)
}
fileSpaceJson, err := ioutil.ReadFile("CreateOrUpdateSpaces_spaceisodetails.json")
if err != nil {
fmt.Println(err)
}
var spaceiso SpaceIsoJson
if err := json.Unmarshal(fileSpaceJson, &spaceiso); err != nil {
panic(err)
}
isoguid := spaceiso.Data.GUID // will be null
// iso segment guid if noting specific it will be default
////
// Pulling isolation segment Name from Guid
var existingisoguid *exec.Cmd
if ostype == "windows" {
path := "\"" + "/v3/isolation_segments?guids=" + isoguid + "\""
existingisoguid = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_existingisodetails.json")
} else {
path := "/v3/isolation_segments?guids=" + isoguid
existingisoguid = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_existingisodetails.json")
}
err = existingisoguid.Run()
if err == nil {
//fmt.Println(getspace, getspace.Stdout, getspace.Stderr)
} else {
fmt.Println("err", existingisoguid, existingisoguid.Stdout, existingisoguid.Stderr)
}
fileSpaceJson, err = ioutil.ReadFile("CreateOrUpdateSpaces_existingisodetails.json")
if err != nil {
fmt.Println(err)
}
var isoexistingdetails IsoJson
if err := json.Unmarshal(fileSpaceJson, &isoexistingdetails); err != nil {
panic(err)
}
//// pulled info of iso
///////////From YAML
// Pulling Guid of Iso Specified in YAML file
var detailsisoguid *exec.Cmd
segname := Orgs.Org.Spaces[j].IsolationSeg
if ostype == "windows" {
path := "\"" + "/v3/isolation_segments?names=" + segname + "\""
detailsisoguid = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_isodetails.json")
} else {
path := "/v3/isolation_segments?names=" + segname
detailsisoguid = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_isodetails.json")
}
err = detailsisoguid.Run()
if err == nil {
//fmt.Println(getspace, getspace.Stdout, getspace.Stderr)
} else {
fmt.Println("err", detailsisoguid, detailsisoguid.Stdout, detailsisoguid.Stderr)
}
fileSpaceJson, err = ioutil.ReadFile("CreateOrUpdateSpaces_isodetails.json")
if err != nil {
fmt.Println(err)
}
var isodetails IsoJson
if err := json.Unmarshal(fileSpaceJson, &isodetails); err != nil {
panic(err)
}
// check if iso defined in yaml exist in platform
if len(isodetails.Resources) == 0 {
// If some name is defined in YAML but that does/t exits
if segname == "" && isoguid == "" {
// No Iso defined in yaml and No Iso required
// No action needs to be done
} else if segname != "" && isoguid == "" {
// This is just for stdout
// Iso is asked in YAML but that is installed in platform
fmt.Println("+ isolation segment", Orgs.Org.Spaces[j].IsolationSeg)
fmt.Println("+ Org, Space, Isolation Segment: ", Orgs.Org.Name, ",", Orgs.Org.Spaces[j].Name, ",", segname)
fmt.Println("No Isolation segment exists with name: ", segname)
} else if segname == "" && isoguid != "" {
if strings.ToLower(Orgs.IsolationAudit) == "unbind" {
fmt.Println("- isolation segment", isoexistingdetails.Resources[0].Name)
fmt.Println("Removing Isolation Segment")
if EnableIsolationAudit == true {
resetspace := exec.Command("cf", "reset-space-isolation-segment", Orgs.Org.Spaces[j].Name)
if _, err := resetspace.Output(); err != nil {
fmt.Println("command: ", resetspace)
fmt.Println("Err: ", resetspace.Stdout, err)
} else {
fmt.Println("command: ", resetspace)
fmt.Println(resetspace.Stdout)
}
} else {
fmt.Println("IsolationSegment Audit flag is not enabled, please work with pcf operator to unbind")
}
} else if strings.ToLower(Orgs.IsolationAudit) == "list" {
fmt.Println("Unbind!Unbind!")
fmt.Println("isolation segment:", isoexistingdetails.Resources[0].Name)
} else {
fmt.Println("Provide valid input")
}
}
} else {
if isodetails.Resources[0].GUID == isoguid {
//fmt.Println(isodetails.Resources[0].GUID, isoguid)
// iso guid defined yaml == iso attached to space
// Iso defined in YAML exist in platform
// Check if that is same as currectly binded
// No action needed
} else {
if isoguid == "" {
// Iso defined in YAML exist
// but Currently space is not binded to any Iso
// This is a new request, binding to isolation segment
fmt.Println("+ isolation segment", Orgs.Org.Spaces[j].IsolationSeg)
//fmt.Println("- isolation segment", isoexistingdetails.Resources[0].Name)
fmt.Println("Enabling Space Isolation Segment ")
fmt.Println("Org, Space, Isolation Segment: ", Orgs.Org.Name, ",", Orgs.Org.Spaces[j].Name, ",", Orgs.Org.Spaces[j].IsolationSeg)
iso := exec.Command("cf", "enable-org-isolation", Orgs.Org.Name, Orgs.Org.Spaces[j].IsolationSeg)
if _, err := iso.Output(); err != nil {
fmt.Println("command: ", iso)
fmt.Println("Err: ", iso.Stdout, err)
} else {
// fmt.Println("command: ", iso)
fmt.Println(iso.Stdout)
}
isospace := exec.Command("cf", "set-space-isolation-segment", Orgs.Org.Spaces[j].Name, Orgs.Org.Spaces[j].IsolationSeg)
if _, err := isospace.Output(); err != nil {
fmt.Println("command: ", isospace)
fmt.Println("Err: ", isospace.Stdout, err)
} else {
// fmt.Println("command: ", isospace)
fmt.Println(isospace.Stdout)
}
} else {
// Iso defined in YAML exist
// but Currently space is not binded to any Iso
// This is a change in request to remove isolation segment
fmt.Println("+ isolation segment", Orgs.Org.Spaces[j].IsolationSeg)
fmt.Println("- isolation segment", isoexistingdetails.Resources[0].Name)
fmt.Println("Currently doesn't support changing Isolation Segments, Please change isolation segment assigned to space manually")
}
}
}
} else {
// Checking space name
fmt.Println("Resetting Space Name")
fmt.Println("- ", spacename)
fmt.Println("+ ", Orgs.Org.Spaces[j].Name)
var path string
//path := "v3/spaces/"+spacedetailsguid.Resources[0].GUID+"/?name="+Orgs.Org.Spaces[j].Name
//path := "v3/spaces/"+spacedetailsguid.Resources[0].GUID+"/?name="+Orgs.Org.Spaces[j].Name
//renamespace := exec.Command("cf", "curl", path)
renamespace := exec.Command("cf", "rename-space", spacename, Orgs.Org.Spaces[j].Name)
err = renamespace.Run()
if err == nil {
// fmt.Println(err, renamespace, renamespace.Stdout, renamespace.Stderr)
} else {
fmt.Println("err", renamespace, renamespace.Stdout, renamespace.Stderr)
}
// checking isolation segments
////////////// From CF
// pulling if any isolation segment assigned to org
// var getspacename *exec.Cmd
if ostype == "windows" {
path := "\"" + "/v3/spaces?names=" + Orgs.Org.Spaces[j].Name + "&organization_guids=" + orgguid + "\""
getspacename = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_name.json")
} else {
path := "/v3/spaces?names=" + Orgs.Org.Spaces[j].Name + "&organization_guids=" + orgguid
getspacename = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_name.json")
}
err = getspacename.Run()
if err == nil {
// fmt.Println(getorg, getorg.Stdout, getorg.Stderr)
} else {
fmt.Println("err", getspacename, getspacename.Stdout, getspacename.Stderr)
}
fileSpaceNameJson, err := ioutil.ReadFile("CreateOrUpdateSpaces_spacedetails_name.json")
if err != nil {
fmt.Println(err)
}
var spacedets SpaceListJson
if err := json.Unmarshal(fileSpaceNameJson, &spacedets); err != nil {
panic(err)
}
// fmt.Println(fileSpaceNameJson)
// fmt.Println(spacedets)
var getisoguid *exec.Cmd
spaceguid := spacedets.Resources[0].GUID
if ostype == "windows" {
path = "\"" + "/v3/spaces/" + spaceguid + "/relationships/isolation_segment" + "\""
getisoguid = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spaceisodetails.json")
} else {
path = "/v3/spaces/" + spaceguid + "/relationships/isolation_segment"
getisoguid = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spaceisodetails.json")
}
err = getisoguid.Run() // it can be nill
if err == nil {
//fmt.Println(getspace, getspace.Stdout, getspace.Stderr)
} else {
fmt.Println("err", getisoguid, getisoguid.Stdout, getisoguid.Stderr)
}
fileSpaceJson, err := ioutil.ReadFile("CreateOrUpdateSpaces_spaceisodetails.json")
if err != nil {
fmt.Println(err)
}
var spaceiso SpaceIsoJson
if err := json.Unmarshal(fileSpaceJson, &spaceiso); err != nil {
panic(err)
}
isoguid := spaceiso.Data.GUID // will be null
// iso segment guid if noting specific it will be default
////
// Pulling isolation segment Name from Guid
var existingisoguid *exec.Cmd
if ostype == "windows" {
path = "\"" + "/v3/isolation_segments?guids=" + isoguid + "\""
existingisoguid = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_existingisodetails.json")
} else {
path = "/v3/isolation_segments?guids=" + isoguid
existingisoguid = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_existingisodetails.json")
}
err = existingisoguid.Run()
if err == nil {
//fmt.Println(getspace, getspace.Stdout, getspace.Stderr)
} else {
fmt.Println("err", existingisoguid, existingisoguid.Stdout, existingisoguid.Stderr)
}
fileSpaceJson, err = ioutil.ReadFile("CreateOrUpdateSpaces_existingisodetails.json")
if err != nil {
fmt.Println(err)
}
var isoexistingdetails IsoJson
if err := json.Unmarshal(fileSpaceJson, &isoexistingdetails); err != nil {
panic(err)
}
//// pulled info of iso
///////////From YAML
// Pulling Guid of Iso Specified in YAML file
segname := Orgs.Org.Spaces[j].IsolationSeg
var detailsisoguid *exec.Cmd
if ostype == "windows" {
path = "\"" + "/v3/isolation_segments?names=" + segname + "\""
detailsisoguid = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_isodetails.json")
} else {
path = "/v3/isolation_segments?names=" + segname
detailsisoguid = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_isodetails.json")
}
err = detailsisoguid.Run()
if err == nil {
//fmt.Println(getspace, getspace.Stdout, getspace.Stderr)
} else {
fmt.Println("err", detailsisoguid, detailsisoguid.Stdout, detailsisoguid.Stderr)
}
fileSpaceJson, err = ioutil.ReadFile("CreateOrUpdateSpaces_isodetails.json")
if err != nil {
fmt.Println(err)
}
var isodetails IsoJson
if err := json.Unmarshal(fileSpaceJson, &isodetails); err != nil {
panic(err)
}
// check if iso defined in yaml exist in platform
if len(isodetails.Resources) == 0 {
// If some name is defined in YAML but that does/t exits
if segname == "" && isoguid == "" {
// No Iso defined in yaml and No Iso required
// No action needs to be done
} else if segname != "" && isoguid == "" {
// This is just for stdout
// Iso is asked in YAML but that is installed in platform
fmt.Println("+ isolation segment", Orgs.Org.Spaces[j].IsolationSeg)
fmt.Println("+ Org, Space, Isolation Segment: ", Orgs.Org.Name, ",", Orgs.Org.Spaces[j].Name, ",", segname)
fmt.Println("No Isolation segment exists with name: ", segname)
} else if segname == "" && isoguid != "" {
fmt.Println("- isolation segment", isoexistingdetails.Resources[0].Name)
fmt.Println("Removing Isolation Segment")
resetspace := exec.Command("cf", "reset-space-isolation-segment", Orgs.Org.Spaces[j].Name)
if _, err := resetspace.Output(); err != nil {
fmt.Println("command: ", resetspace)
fmt.Println("Err: ", resetspace.Stdout, err)
} else {
fmt.Println("command: ", resetspace)
fmt.Println(resetspace.Stdout)
}
}
} else {
if isodetails.Resources[0].GUID == isoguid {
//fmt.Println(isodetails.Resources[0].GUID, isoguid)
// iso guid defined yaml == iso attached to space
// Iso defined in YAML exist in platform
// Check if that is same as currectly binded
// No action needed
} else {
if isoguid == "" {
// Iso defined in YAML exist
// but Currently space is not binded to any Iso
// This is a new request, binding to isolation segment
fmt.Println("+ isolation segment", Orgs.Org.Spaces[j].IsolationSeg)
//fmt.Println("- isolation segment", isoexistingdetails.Resources[0].Name)
fmt.Println("Enabling Space Isolation Segment")
fmt.Println("Org, Space, Isolation Segment: ", Orgs.Org.Name, ",", Orgs.Org.Spaces[j].Name, ",", Orgs.Org.Spaces[j].IsolationSeg)
iso := exec.Command("cf", "enable-org-isolation", Orgs.Org.Name, Orgs.Org.Spaces[j].IsolationSeg)
if _, err := iso.Output(); err != nil {
fmt.Println("command: ", iso)
fmt.Println("Err: ", iso.Stdout, err)
} else {
// fmt.Println("command: ", iso)
fmt.Println(iso.Stdout)
}
isospace := exec.Command("cf", "set-space-isolation-segment", Orgs.Org.Spaces[j].Name, Orgs.Org.Spaces[j].IsolationSeg)
if _, err := isospace.Output(); err != nil {
fmt.Println("command: ", isospace)
fmt.Println("Err: ", isospace.Stdout, err)
} else {
// fmt.Println("command: ", isospace)
fmt.Println(isospace.Stdout)
}
} else {
// Iso defined in YAML exist
// but Currently space is not binded to any Iso
// This is a change in request to remove isolation segment
fmt.Println("+ isolation segment", Orgs.Org.Spaces[j].IsolationSeg)
fmt.Println("- isolation segment", isoexistingdetails.Resources[0].Name)
fmt.Println("Currently doesn't support changing Isolation Segments, Please change isolation segment assigned to space manually")
}
}
}
// Pulling Space GUID
var getspacename *exec.Cmd
if ostype == "windows" {
path := "\"" + "/v3/spaces?names=" + Orgs.Org.Spaces[j].Name + "&organization_guids=" + orgguid + "\""
getspacename = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_name.json")
} else {
path := "/v3/spaces?names=" + Orgs.Org.Spaces[j].Name + "&organization_guids=" + orgguid
getspacename = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_name.json")
}
err = getspacename.Run()
if err == nil {
// fmt.Println(getorg, getorg.Stdout, getorg.Stderr)
} else {
fmt.Println("err", getspacename, getspacename.Stdout, getspacename.Stderr)
}
fileSpaceNameJson, err = ioutil.ReadFile("CreateOrUpdateSpaces_spacedetails_name.json")
if err != nil {
fmt.Println(err)
}
var spacedetailsname SpaceListJson
if err := json.Unmarshal(fileSpaceNameJson, &spacedetailsname); err != nil {
panic(err)
}
SpaceGuidPull = spacedetailsname.Resources[0].GUID
}
} else if SpaceStateGuidLen == 0 && SpaceStateNameLen != 0 {
fmt.Println("Missing State file, Please use org-init then space-init function to create state files")
} else if SpaceStateGuidLen == 0 && SpaceStateNameLen == 0 {
//fmt.Println("Creating Spaces")
fmt.Println("+ ", Orgs.Org.Spaces[j].Name)
CreateSpace := exec.Command("cf", "create-space", Orgs.Org.Spaces[j].Name, "-o", Orgs.Org.Name)
if _, err := CreateSpace.Output(); err != nil {
fmt.Println("command: ", CreateSpace)
fmt.Println("Err: ", CreateSpace.Stdout, err)
} else {
fmt.Println(CreateSpace.Stdout)
if Orgs.Org.Spaces[j].IsolationSeg != "" {
fmt.Println("Enabling Space Isolation Segment")
fmt.Println("SegName: ", Orgs.Org.Spaces[j].IsolationSeg)
fmt.Println("+ isolation segment", Orgs.Org.Spaces[j].IsolationSeg)
iso := exec.Command("cf", "enable-org-isolation", Orgs.Org.Name, Orgs.Org.Spaces[j].IsolationSeg)
if _, err := iso.Output(); err != nil {
fmt.Println("command: ", iso)
fmt.Println("Err: ", iso.Stdout, err)
} else {
//fmt.Println("command: ", iso)
fmt.Println(iso.Stdout)
}
fmt.Println("Attaching to space")
isospace := exec.Command("cf", "set-space-isolation-segment", Orgs.Org.Spaces[j].Name, Orgs.Org.Spaces[j].IsolationSeg)
if _, err := isospace.Output(); err != nil {
fmt.Println("command: ", isospace)
fmt.Println("Err: ", isospace.Stdout, err)
} else {
//fmt.Println("command: ", isospace)
fmt.Println(isospace.Stdout)
}
} else {
fmt.Println("No Isolation Segment Provided, Will be attached to Org default, if it exist")
}
// Pulling Space GUID
var getspacename *exec.Cmd
if ostype == "windows" {
path := "\"" + "/v3/spaces?names=" + Orgs.Org.Spaces[j].Name + "&organization_guids=" + orgguid + "\""
getspacename = exec.Command("powershell", "-command", "cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_name.json")
} else {
path := "/v3/spaces?names=" + Orgs.Org.Spaces[j].Name + "&organization_guids=" + orgguid
getspacename = exec.Command("cf", "curl", strings.TrimSpace(path), "--output", "CreateOrUpdateSpaces_spacedetails_name.json")
}
err = getspacename.Run()
if err == nil {
// fmt.Println(getorg, getorg.Stdout, getorg.Stderr)
} else {
fmt.Println("err", getspacename, getspacename.Stdout, getspacename.Stderr)
}
fileSpaceNameJson, err := ioutil.ReadFile("CreateOrUpdateSpaces_spacedetails_name.json")
if err != nil {
fmt.Println(err)
}
var spacedetailsname SpaceListJson
if err := json.Unmarshal(fileSpaceNameJson, &spacedetailsname); err != nil {
panic(err)
}
SpaceGuidPull = spacedetailsname.Resources[0].GUID
}
}
// Creating state file
if SpaceStateGuidLen != 0 {
orgguid := orgdetails.Resources[0].GUID
type SpaceState struct {
Org string
OrgGuid string
OldSpaceName string
NewSpaceName string
SpaceGuid string
}
values := SpaceState{Org: Orgs.Org.Name, OrgGuid: orgguid, OldSpaceName: Orgs.Org.Spaces[j].Name, NewSpaceName: Orgs.Org.Spaces[j].Name, SpaceGuid: SpaceGuidPull}
var templates *template.Template
var allFiles []string
if err != nil {
fmt.Println(err)
}
filename := "SpaceGuid.tmpl"
fullPath := spath + "SpaceGuid.tmpl"
if strings.HasSuffix(filename, ".tmpl") {
allFiles = append(allFiles, fullPath)
}
//fmt.Println(allFiles)
templates, err = template.ParseFiles(allFiles...)
if err != nil {
fmt.Println(err)
}
s1 := templates.Lookup("SpaceGuid.tmpl")
f, err := os.Create(spath + Orgs.Org.Name + "_" + Orgs.Org.Spaces[j].Name + "_SpaceState.yml")
if err != nil {