@@ -45,13 +45,66 @@ const (
4545 DiscoveryComputeNetworks = "compute-networks"
4646 DiscoveryComputeSubnetworks = "compute-subnetworks"
4747 DiscoveryGkeClusters = "gke-clusters"
48- DiscoveryInstances = "instances"
48+ DiscoveryComputeInstances = "instances"
4949 DiscoveryStorageBuckets = "storage-buckets"
5050)
5151
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+
5284// List of all CloudSQL types, this will be used during discovery
5385var AllCloudSQLTypes = []string {DiscoverCloudSQLPostgreSQL , DiscoverCloudSQLSQLServer , DiscoverCloudSQLMySQL }
5486
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+
55108func Discover (runtime * plugin.Runtime ) (* inventory.Inventory , error ) {
56109 conn , ok := runtime .Connection .(* connection.GcpConnection )
57110 if ! ok {
@@ -61,6 +114,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
61114 in := & inventory.Inventory {Spec : & inventory.InventorySpec {
62115 Assets : []* inventory.Asset {},
63116 }}
117+ discoveryTargets := getDiscoveryTargets (conn .Conf )
64118
65119 if conn .ResourceType () == connection .Organization {
66120 res , err := NewResource (runtime , "gcp.organization" , nil )
@@ -70,7 +124,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
70124
71125 gcpOrg := res .(* mqlGcpOrganization )
72126
73- list , err := discoverOrganization (conn , gcpOrg )
127+ list , err := discoverOrganization (conn , gcpOrg , discoveryTargets )
74128 if err != nil {
75129 return nil , err
76130 }
@@ -82,7 +136,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
82136 }
83137
84138 gcpFolder := res .(* mqlGcpFolder )
85- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryFolders ) {
139+ if stringx .Contains ( discoveryTargets , DiscoveryFolders ) {
86140 in .Spec .Assets = append (in .Spec .Assets , & inventory.Asset {
87141 PlatformIds : []string {
88142 connection .NewFolderPlatformID (gcpFolder .Id .Data ),
@@ -101,7 +155,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
101155 })
102156 }
103157
104- list , err := discoverFolder (conn , gcpFolder )
158+ list , err := discoverFolder (conn , gcpFolder , discoveryTargets )
105159 if err != nil {
106160 return nil , err
107161 }
@@ -116,7 +170,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
116170 }
117171
118172 gcpProject := res .(* mqlGcpProject )
119- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryProjects ) {
173+ if stringx .Contains ( discoveryTargets , DiscoveryProjects ) {
120174 in .Spec .Assets = append (in .Spec .Assets , & inventory.Asset {
121175 PlatformIds : []string {
122176 connection .NewProjectPlatformID (gcpProject .Id .Data ),
@@ -135,7 +189,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
135189 })
136190 }
137191
138- list , err := discoverProject (conn , gcpProject )
192+ list , err := discoverProject (conn , gcpProject , discoveryTargets )
139193 if err != nil {
140194 return nil , err
141195 }
@@ -163,9 +217,9 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
163217 return in , nil
164218}
165219
166- func discoverOrganization (conn * connection.GcpConnection , gcpOrg * mqlGcpOrganization ) ([]* inventory.Asset , error ) {
220+ func discoverOrganization (conn * connection.GcpConnection , gcpOrg * mqlGcpOrganization , discoveryTargets [] string ) ([]* inventory.Asset , error ) {
167221 assetList := []* inventory.Asset {}
168- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryProjects ) {
222+ if stringx .Contains ( discoveryTargets , DiscoveryProjects ) {
169223 projects := gcpOrg .GetProjects ()
170224 if projects .Error != nil {
171225 return nil , projects .Error
@@ -203,14 +257,14 @@ func discoverOrganization(conn *connection.GcpConnection, gcpOrg *mqlGcpOrganiza
203257 Connections : []* inventory.Config {projectConf }, // pass-in the parent connection config
204258 })
205259
206- projectAssets , err := discoverProject (conn , project )
260+ projectAssets , err := discoverProject (conn , project , discoveryTargets )
207261 if err != nil {
208262 return nil , err
209263 }
210264 assetList = append (assetList , projectAssets ... )
211265 }
212266 }
213- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryFolders ) {
267+ if stringx .Contains ( discoveryTargets , DiscoveryFolders ) {
214268 folders := gcpOrg .GetFolders ()
215269 if folders .Error != nil {
216270 return nil , folders .Error
@@ -251,10 +305,10 @@ func discoverOrganization(conn *connection.GcpConnection, gcpOrg *mqlGcpOrganiza
251305 return assetList , nil
252306}
253307
254- func discoverFolder (conn * connection.GcpConnection , gcpFolder * mqlGcpFolder ) ([]* inventory.Asset , error ) {
308+ func discoverFolder (conn * connection.GcpConnection , gcpFolder * mqlGcpFolder , discoveryTargets [] string ) ([]* inventory.Asset , error ) {
255309 assetList := []* inventory.Asset {}
256310
257- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , DiscoveryAll , DiscoveryAuto , DiscoveryProjects ) {
311+ if stringx .Contains ( discoveryTargets , DiscoveryProjects ) {
258312 projects := gcpFolder .GetProjects ()
259313 if projects .Error != nil {
260314 return nil , projects .Error
@@ -296,13 +350,9 @@ func discoverFolder(conn *connection.GcpConnection, gcpFolder *mqlGcpFolder) ([]
296350 return assetList , nil
297351}
298352
299- func discoverProject (conn * connection.GcpConnection , gcpProject * mqlGcpProject ) ([]* inventory.Asset , error ) {
353+ func discoverProject (conn * connection.GcpConnection , gcpProject * mqlGcpProject , discoveryTargets [] string ) ([]* inventory.Asset , error ) {
300354 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 ) {
306356 compute := gcpProject .GetCompute ()
307357 if compute .Error != nil {
308358 return nil , compute .Error
@@ -350,7 +400,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
350400 })
351401 }
352402 }
353- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , append ( targets , DiscoveryComputeImages ) ... ) {
403+ if stringx .Contains ( discoveryTargets , DiscoveryComputeImages ) {
354404 compute := gcpProject .GetCompute ()
355405 if compute .Error != nil {
356406 return nil , compute .Error
@@ -384,7 +434,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
384434 })
385435 }
386436 }
387- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , append ( targets , DiscoverCloudKMSKeyrings ) ... ) {
437+ if stringx .Contains ( discoveryTargets , DiscoverCloudKMSKeyrings ) {
388438 kmsservice := gcpProject .GetKms ()
389439 if kmsservice .Error != nil {
390440 return nil , kmsservice .Error
@@ -414,7 +464,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
414464 })
415465 }
416466 }
417- if stringx .ContainsAnyOf ( conn . Conf . Discover . Targets , append ( targets , DiscoverCloudDNSZones ) ... ) {
467+ if stringx .Contains ( discoveryTargets , DiscoverCloudDNSZones ) {
418468 dnsservice := gcpProject .GetDns ()
419469 if dnsservice .Error != nil {
420470 return nil , dnsservice .Error
@@ -445,7 +495,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
445495 }
446496 }
447497 // all Cloud SQL discovery flags/types
448- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , AllCloudSQLTypes ... ) ... ) {
498+ if stringx .ContainsAnyOf (discoveryTargets , AllCloudSQLTypes ... ) {
449499 sqlservice := gcpProject .GetSql ()
450500 if sqlservice .Error != nil {
451501 return nil , sqlservice .Error
@@ -463,7 +513,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
463513 platformName = fmt .Sprintf ("gcp-sql-%s" , sqlType )
464514 )
465515
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 )) {
467517 log .Debug ().
468518 Str ("sql_type" , sqlType ).
469519 Msg ("gcp.discovery> skipping cloud sql instance" )
@@ -488,7 +538,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
488538 })
489539 }
490540 }
491- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryComputeNetworks ) ... ) {
541+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryComputeNetworks ) {
492542 compute := gcpProject .GetCompute ()
493543 if compute .Error != nil {
494544 return nil , compute .Error
@@ -517,7 +567,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
517567 })
518568 }
519569 }
520- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryComputeSubnetworks ) ... ) {
570+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryComputeSubnetworks ) {
521571 compute := gcpProject .GetCompute ()
522572 if compute .Error != nil {
523573 return nil , compute .Error
@@ -550,7 +600,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
550600 })
551601 }
552602 }
553- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryComputeFirewalls ) ... ) {
603+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryComputeFirewalls ) {
554604 compute := gcpProject .GetCompute ()
555605 if compute .Error != nil {
556606 return nil , compute .Error
@@ -579,7 +629,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
579629 })
580630 }
581631 }
582- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryGkeClusters ) ... ) {
632+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryGkeClusters ) {
583633 gke := gcpProject .GetGke ()
584634 if gke .Error != nil {
585635 return nil , gke .Error
@@ -608,7 +658,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
608658 })
609659 }
610660 }
611- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryStorageBuckets ) ... ) {
661+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryStorageBuckets ) {
612662 storage := gcpProject .GetStorage ()
613663 if storage .Error != nil {
614664 return nil , storage .Error
@@ -637,7 +687,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
637687 })
638688 }
639689 }
640- if stringx .ContainsAnyOf (conn . Conf . Discover . Targets , append ( targets , DiscoveryBigQueryDatasets ) ... ) {
690+ if stringx .ContainsAnyOf (discoveryTargets , DiscoveryBigQueryDatasets ) {
641691 bq := gcpProject .GetBigquery ()
642692 if bq .Error != nil {
643693 return nil , bq .Error
0 commit comments