@@ -45,13 +45,66 @@ const (
45
45
DiscoveryComputeNetworks = "compute-networks"
46
46
DiscoveryComputeSubnetworks = "compute-subnetworks"
47
47
DiscoveryGkeClusters = "gke-clusters"
48
- DiscoveryInstances = "instances"
48
+ DiscoveryComputeInstances = "instances"
49
49
DiscoveryStorageBuckets = "storage-buckets"
50
50
)
51
51
52
+ var All = []string {
53
+ DiscoveryOrganization ,
54
+ DiscoveryFolders ,
55
+ DiscoveryProjects ,
56
+ }
57
+
58
+ func allDiscovery () []string {
59
+ return append (All , AllAPIResources ... )
60
+ }
61
+
62
+ var Auto = []string {
63
+ DiscoveryOrganization ,
64
+ DiscoveryFolders ,
65
+ DiscoveryProjects ,
66
+ }
67
+
68
+ var AllAPIResources = []string {
69
+ DiscoveryComputeImages ,
70
+ DiscoveryComputeNetworks ,
71
+ DiscoveryComputeSubnetworks ,
72
+ DiscoveryComputeFirewalls ,
73
+ DiscoveryGkeClusters ,
74
+ DiscoveryStorageBuckets ,
75
+ DiscoveryBigQueryDatasets ,
76
+ DiscoverCloudSQLMySQL ,
77
+ DiscoverCloudSQLPostgreSQL ,
78
+ DiscoverCloudSQLSQLServer ,
79
+ DiscoverCloudDNSZones ,
80
+ DiscoverCloudKMSKeyrings ,
81
+ DiscoveryComputeInstances ,
82
+ }
83
+
52
84
// List of all CloudSQL types, this will be used during discovery
53
85
var AllCloudSQLTypes = []string {DiscoverCloudSQLPostgreSQL , DiscoverCloudSQLSQLServer , DiscoverCloudSQLMySQL }
54
86
87
+ func getDiscoveryTargets (config * inventory.Config ) []string {
88
+ targets := config .Discover .Targets
89
+
90
+ if stringx .ContainsAnyOf (targets , DiscoveryAll ) {
91
+ // return the All list + All Api Resources list
92
+ return allDiscovery ()
93
+ }
94
+ if stringx .ContainsAnyOf (targets , DiscoveryAuto ) {
95
+ for i , target := range targets {
96
+ if target == "auto" {
97
+ // remove the auto keyword
98
+ targets = slices .Delete (targets , i , i + 1 )
99
+ }
100
+ }
101
+ // add in the required discovery targets
102
+ return append (targets , Auto ... )
103
+ }
104
+ // random assortment of targets
105
+ return targets
106
+ }
107
+
55
108
func Discover (runtime * plugin.Runtime ) (* inventory.Inventory , error ) {
56
109
conn , ok := runtime .Connection .(* connection.GcpConnection )
57
110
if ! ok {
@@ -61,6 +114,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
61
114
in := & inventory.Inventory {Spec : & inventory.InventorySpec {
62
115
Assets : []* inventory.Asset {},
63
116
}}
117
+ discoveryTargets := getDiscoveryTargets (conn .Conf )
64
118
65
119
if conn .ResourceType () == connection .Organization {
66
120
res , err := NewResource (runtime , "gcp.organization" , nil )
@@ -70,7 +124,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
70
124
71
125
gcpOrg := res .(* mqlGcpOrganization )
72
126
73
- list , err := discoverOrganization (conn , gcpOrg )
127
+ list , err := discoverOrganization (conn , gcpOrg , discoveryTargets )
74
128
if err != nil {
75
129
return nil , err
76
130
}
@@ -82,7 +136,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
82
136
}
83
137
84
138
gcpFolder := res .(* mqlGcpFolder )
85
- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryFolders ) {
139
+ if stringx .Contains ( discoveryTargets , DiscoveryFolders ) {
86
140
in .Spec .Assets = append (in .Spec .Assets , & inventory.Asset {
87
141
PlatformIds : []string {
88
142
connection .NewFolderPlatformID (gcpFolder .Id .Data ),
@@ -101,7 +155,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
101
155
})
102
156
}
103
157
104
- list , err := discoverFolder (conn , gcpFolder )
158
+ list , err := discoverFolder (conn , gcpFolder , discoveryTargets )
105
159
if err != nil {
106
160
return nil , err
107
161
}
@@ -116,7 +170,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
116
170
}
117
171
118
172
gcpProject := res .(* mqlGcpProject )
119
- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryProjects ) {
173
+ if stringx .Contains ( discoveryTargets , DiscoveryProjects ) {
120
174
in .Spec .Assets = append (in .Spec .Assets , & inventory.Asset {
121
175
PlatformIds : []string {
122
176
connection .NewProjectPlatformID (gcpProject .Id .Data ),
@@ -135,7 +189,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
135
189
})
136
190
}
137
191
138
- list , err := discoverProject (conn , gcpProject )
192
+ list , err := discoverProject (conn , gcpProject , discoveryTargets )
139
193
if err != nil {
140
194
return nil , err
141
195
}
@@ -163,9 +217,9 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
163
217
return in , nil
164
218
}
165
219
166
- func discoverOrganization (conn * connection.GcpConnection , gcpOrg * mqlGcpOrganization ) ([]* inventory.Asset , error ) {
220
+ func discoverOrganization (conn * connection.GcpConnection , gcpOrg * mqlGcpOrganization , discoveryTargets [] string ) ([]* inventory.Asset , error ) {
167
221
assetList := []* inventory.Asset {}
168
- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryProjects ) {
222
+ if stringx .Contains ( discoveryTargets , DiscoveryProjects ) {
169
223
projects := gcpOrg .GetProjects ()
170
224
if projects .Error != nil {
171
225
return nil , projects .Error
@@ -203,14 +257,14 @@ func discoverOrganization(conn *connection.GcpConnection, gcpOrg *mqlGcpOrganiza
203
257
Connections : []* inventory.Config {projectConf }, // pass-in the parent connection config
204
258
})
205
259
206
- projectAssets , err := discoverProject (conn , project )
260
+ projectAssets , err := discoverProject (conn , project , discoveryTargets )
207
261
if err != nil {
208
262
return nil , err
209
263
}
210
264
assetList = append (assetList , projectAssets ... )
211
265
}
212
266
}
213
- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryFolders ) {
267
+ if stringx .Contains ( discoveryTargets , DiscoveryFolders ) {
214
268
folders := gcpOrg .GetFolders ()
215
269
if folders .Error != nil {
216
270
return nil , folders .Error
@@ -251,10 +305,10 @@ func discoverOrganization(conn *connection.GcpConnection, gcpOrg *mqlGcpOrganiza
251
305
return assetList , nil
252
306
}
253
307
254
- func discoverFolder (conn * connection.GcpConnection , gcpFolder * mqlGcpFolder ) ([]* inventory.Asset , error ) {
308
+ func discoverFolder (conn * connection.GcpConnection , gcpFolder * mqlGcpFolder , discoveryTargets [] string ) ([]* inventory.Asset , error ) {
255
309
assetList := []* inventory.Asset {}
256
310
257
- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryProjects ) {
311
+ if stringx .Contains ( discoveryTargets , DiscoveryProjects ) {
258
312
projects := gcpFolder .GetProjects ()
259
313
if projects .Error != nil {
260
314
return nil , projects .Error
@@ -296,13 +350,9 @@ func discoverFolder(conn *connection.GcpConnection, gcpFolder *mqlGcpFolder) ([]
296
350
return assetList , nil
297
351
}
298
352
299
- func discoverProject (conn * connection.GcpConnection , gcpProject * mqlGcpProject ) ([]* inventory.Asset , error ) {
353
+ func discoverProject (conn * connection.GcpConnection , gcpProject * mqlGcpProject , discoveryTargets [] string ) ([]* inventory.Asset , error ) {
300
354
assetList := []* inventory.Asset {}
301
- targets := []string {DiscoveryAll }
302
- if ENABLE_FINE_GRAINED_ASSETS {
303
- targets = append (targets , DiscoveryAuto )
304
- }
305
- if stringx .ContainsAnyOf (conn .Conf .Discover .Targets , append (targets , DiscoveryInstances )... ) {
355
+ if stringx .Contains (discoveryTargets , DiscoveryComputeInstances ) {
306
356
compute := gcpProject .GetCompute ()
307
357
if compute .Error != nil {
308
358
return nil , compute .Error
@@ -350,7 +400,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
350
400
})
351
401
}
352
402
}
353
- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , append ( targets , DiscoveryComputeImages ) ... ) {
403
+ if stringx .Contains ( discoveryTargets , DiscoveryComputeImages ) {
354
404
compute := gcpProject .GetCompute ()
355
405
if compute .Error != nil {
356
406
return nil , compute .Error
@@ -384,7 +434,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
384
434
})
385
435
}
386
436
}
387
- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , append ( targets , DiscoverCloudKMSKeyrings ) ... ) {
437
+ if stringx .Contains ( discoveryTargets , DiscoverCloudKMSKeyrings ) {
388
438
kmsservice := gcpProject .GetKms ()
389
439
if kmsservice .Error != nil {
390
440
return nil , kmsservice .Error
@@ -414,7 +464,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
414
464
})
415
465
}
416
466
}
417
- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , append ( targets , DiscoverCloudDNSZones ) ... ) {
467
+ if stringx .Contains ( discoveryTargets , DiscoverCloudDNSZones ) {
418
468
dnsservice := gcpProject .GetDns ()
419
469
if dnsservice .Error != nil {
420
470
return nil , dnsservice .Error
@@ -445,7 +495,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
445
495
}
446
496
}
447
497
// all Cloud SQL discovery flags/types
448
- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , AllCloudSQLTypes ... ) ... ) {
498
+ if stringx .ContainsAnyOf (discoveryTargets , AllCloudSQLTypes ... ) {
449
499
sqlservice := gcpProject .GetSql ()
450
500
if sqlservice .Error != nil {
451
501
return nil , sqlservice .Error
@@ -463,7 +513,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
463
513
platformName = fmt .Sprintf ("gcp-sql-%s" , sqlType )
464
514
)
465
515
466
- if ! slices .Contains (conn . Conf . Discover . Targets , fmt .Sprintf ("cloud-sql-%s" , sqlType )) {
516
+ if ! slices .Contains (discoveryTargets , fmt .Sprintf ("cloud-sql-%s" , sqlType )) {
467
517
log .Debug ().
468
518
Str ("sql_type" , sqlType ).
469
519
Msg ("gcp.discovery> skipping cloud sql instance" )
@@ -488,7 +538,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
488
538
})
489
539
}
490
540
}
491
- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryComputeNetworks ) ... ) {
541
+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryComputeNetworks ) {
492
542
compute := gcpProject .GetCompute ()
493
543
if compute .Error != nil {
494
544
return nil , compute .Error
@@ -517,7 +567,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
517
567
})
518
568
}
519
569
}
520
- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryComputeSubnetworks ) ... ) {
570
+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryComputeSubnetworks ) {
521
571
compute := gcpProject .GetCompute ()
522
572
if compute .Error != nil {
523
573
return nil , compute .Error
@@ -550,7 +600,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
550
600
})
551
601
}
552
602
}
553
- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryComputeFirewalls ) ... ) {
603
+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryComputeFirewalls ) {
554
604
compute := gcpProject .GetCompute ()
555
605
if compute .Error != nil {
556
606
return nil , compute .Error
@@ -579,7 +629,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
579
629
})
580
630
}
581
631
}
582
- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryGkeClusters ) ... ) {
632
+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryGkeClusters ) {
583
633
gke := gcpProject .GetGke ()
584
634
if gke .Error != nil {
585
635
return nil , gke .Error
@@ -608,7 +658,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
608
658
})
609
659
}
610
660
}
611
- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryStorageBuckets ) ... ) {
661
+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryStorageBuckets ) {
612
662
storage := gcpProject .GetStorage ()
613
663
if storage .Error != nil {
614
664
return nil , storage .Error
@@ -637,7 +687,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
637
687
})
638
688
}
639
689
}
640
- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryBigQueryDatasets ) ... ) {
690
+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryBigQueryDatasets ) {
641
691
bq := gcpProject .GetBigquery ()
642
692
if bq .Error != nil {
643
693
return nil , bq .Error
0 commit comments