Skip to content

Commit 134b805

Browse files
first draft regional assets to avoid platform id collisions
1 parent b4b49ae commit 134b805

File tree

8 files changed

+94
-12
lines changed

8 files changed

+94
-12
lines changed

providers/gcp/resources/cloud_functions.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func (g *mqlGcpProject) cloudFunctions() ([]any, error) {
145145

146146
mqlCloudFuncs, err := CreateResource(g.MqlRuntime, "gcp.project.cloudFunction", map[string]*llx.RawData{
147147
"projectId": llx.StringData(projectId),
148+
"region": llx.StringData(parseLocationFromPath(f.Name)),
148149
"name": llx.StringData(parseResourceName(f.Name)),
149150
"description": llx.StringData(f.Description),
150151
"sourceArchiveUrl": llx.StringData(sourceArchiveUrl),
@@ -199,7 +200,7 @@ func (g *mqlGcpProjectCloudFunction) id() (string, error) {
199200
}
200201

201202
func initGcpProjectCloudFunction(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
202-
if len(args) > 2 {
203+
if len(args) > 3 {
203204
return args, nil, nil
204205
}
205206

@@ -209,6 +210,7 @@ func initGcpProjectCloudFunction(runtime *plugin.Runtime, args map[string]*llx.R
209210
}
210211
if ids := getAssetIdentifier(runtime); ids != nil {
211212
args["name"] = llx.StringData(ids.name)
213+
args["region"] = llx.StringData(ids.region)
212214
args["projectId"] = llx.StringData(ids.project)
213215
} else {
214216
return nil, nil, errors.New("no asset identifier found")
@@ -228,9 +230,13 @@ func initGcpProjectCloudFunction(runtime *plugin.Runtime, args map[string]*llx.R
228230
}
229231

230232
nameVal := args["name"].Value.(string)
233+
regionVal := ""
234+
if args["region"] != nil {
235+
regionVal = args["region"].Value.(string)
236+
}
231237
for _, f := range funcs.Data {
232238
fn := f.(*mqlGcpProjectCloudFunction)
233-
if fn.Name.Data == nameVal {
239+
if fn.Name.Data == nameVal && (regionVal == "" || fn.Region.Data == regionVal) {
234240
return args, fn, nil
235241
}
236242
}

providers/gcp/resources/common.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ func parseResourceName(fullPath string) string {
3434
return segments[len(segments)-1]
3535
}
3636

37+
// parseLocationFromPath extracts the location/region from a GCP resource path.
38+
// The path format is: projects/{project}/locations/{location}/...
39+
// Returns "global" if no location segment is found.
40+
func parseLocationFromPath(fullPath string) string {
41+
segments := strings.Split(fullPath, "/")
42+
for i, s := range segments {
43+
if s == "locations" && i+1 < len(segments) {
44+
return segments[i+1]
45+
}
46+
}
47+
return "global"
48+
}
49+
3750
type assetIdentifier struct {
3851
name string
3952
region string

providers/gcp/resources/dataproc.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ func (g *mqlGcpProjectDataprocService) clusters() ([]any, error) {
584584

585585
mqlCluster, err := CreateResource(g.MqlRuntime, "gcp.project.dataprocService.cluster", map[string]*llx.RawData{
586586
"projectId": llx.StringData(projectId),
587+
"region": llx.StringData(regionName),
587588
"name": llx.StringData(c.ClusterName),
588589
"uuid": llx.StringData(c.ClusterUuid),
589590
"config": llx.ResourceData(mqlConfig, "gcp.project.dataprocService.cluster.config"),
@@ -641,7 +642,7 @@ func (g *mqlGcpProjectDataprocServiceCluster) id() (string, error) {
641642
}
642643

643644
func initGcpProjectDataprocServiceCluster(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
644-
if len(args) > 2 {
645+
if len(args) > 3 {
645646
return args, nil, nil
646647
}
647648

@@ -651,6 +652,7 @@ func initGcpProjectDataprocServiceCluster(runtime *plugin.Runtime, args map[stri
651652
}
652653
if ids := getAssetIdentifier(runtime); ids != nil {
653654
args["name"] = llx.StringData(ids.name)
655+
args["region"] = llx.StringData(ids.region)
654656
args["projectId"] = llx.StringData(ids.project)
655657
} else {
656658
return nil, nil, errors.New("no asset identifier found")
@@ -671,9 +673,13 @@ func initGcpProjectDataprocServiceCluster(runtime *plugin.Runtime, args map[stri
671673
}
672674

673675
nameVal := args["name"].Value.(string)
676+
regionVal := ""
677+
if args["region"] != nil {
678+
regionVal = args["region"].Value.(string)
679+
}
674680
for _, c := range clusters.Data {
675681
cluster := c.(*mqlGcpProjectDataprocServiceCluster)
676-
if cluster.Name.Data == nameVal {
682+
if cluster.Name.Data == nameVal && (regionVal == "" || cluster.Region.Data == regionVal) {
677683
return args, cluster, nil
678684
}
679685
}

providers/gcp/resources/discovery.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,10 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
10321032
}
10331033
for i := range funcs.Data {
10341034
fn := funcs.Data[i].(*mqlGcpProjectCloudFunction)
1035+
region := fn.Region.Data
10351036
assetList = append(assetList, &inventory.Asset{
10361037
PlatformIds: []string{
1037-
connection.NewResourcePlatformID("cloud-functions", gcpProject.Id.Data, "global", "function", fn.Name.Data),
1038+
connection.NewResourcePlatformID("cloud-functions", gcpProject.Id.Data, region, "function", fn.Name.Data),
10381039
},
10391040
Name: fn.Name.Data,
10401041
Platform: &inventory.Platform{
@@ -1043,7 +1044,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
10431044
Runtime: "gcp",
10441045
Kind: "gcp-object",
10451046
Family: []string{"google"},
1046-
TechnologyUrlSegments: connection.ResourceTechnologyUrl("cloud-functions", gcpProject.Id.Data, "global", "function", fn.Name.Data),
1047+
TechnologyUrlSegments: connection.ResourceTechnologyUrl("cloud-functions", gcpProject.Id.Data, region, "function", fn.Name.Data),
10471048
},
10481049
Labels: mapStrInterfaceToMapStrStr(fn.GetLabels().Data),
10491050
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))},
@@ -1062,9 +1063,10 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
10621063
}
10631064
for i := range clusters.Data {
10641065
cluster := clusters.Data[i].(*mqlGcpProjectDataprocServiceCluster)
1066+
region := cluster.Region.Data
10651067
assetList = append(assetList, &inventory.Asset{
10661068
PlatformIds: []string{
1067-
connection.NewResourcePlatformID("dataproc", gcpProject.Id.Data, "global", "cluster", cluster.Name.Data),
1069+
connection.NewResourcePlatformID("dataproc", gcpProject.Id.Data, region, "cluster", cluster.Name.Data),
10681070
},
10691071
Name: cluster.Name.Data,
10701072
Platform: &inventory.Platform{
@@ -1073,7 +1075,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
10731075
Runtime: "gcp",
10741076
Kind: "gcp-object",
10751077
Family: []string{"google"},
1076-
TechnologyUrlSegments: connection.ResourceTechnologyUrl("dataproc", gcpProject.Id.Data, "global", "cluster", cluster.Name.Data),
1078+
TechnologyUrlSegments: connection.ResourceTechnologyUrl("dataproc", gcpProject.Id.Data, region, "cluster", cluster.Name.Data),
10771079
},
10781080
Labels: mapStrInterfaceToMapStrStr(cluster.GetLabels().Data),
10791081
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))},
@@ -1093,9 +1095,10 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
10931095
for i := range buckets.Data {
10941096
bucket := buckets.Data[i].(*mqlGcpProjectLoggingserviceBucket)
10951097
bucketName := parseResourceName(bucket.Name.Data)
1098+
location := bucket.Location.Data
10961099
assetList = append(assetList, &inventory.Asset{
10971100
PlatformIds: []string{
1098-
connection.NewResourcePlatformID("logging", gcpProject.Id.Data, "global", "bucket", bucketName),
1101+
connection.NewResourcePlatformID("logging", gcpProject.Id.Data, location, "bucket", bucketName),
10991102
},
11001103
Name: bucketName,
11011104
Platform: &inventory.Platform{
@@ -1104,7 +1107,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
11041107
Runtime: "gcp",
11051108
Kind: "gcp-object",
11061109
Family: []string{"google"},
1107-
TechnologyUrlSegments: connection.ResourceTechnologyUrl("logging", gcpProject.Id.Data, "global", "bucket", bucketName),
1110+
TechnologyUrlSegments: connection.ResourceTechnologyUrl("logging", gcpProject.Id.Data, location, "bucket", bucketName),
11081111
},
11091112
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))},
11101113
})

providers/gcp/resources/gcp.lr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,8 @@ private gcp.project.loggingservice {
24892489
private gcp.project.loggingservice.bucket @defaults("name") {
24902490
// Project ID
24912491
projectId string
2492+
// Location where the bucket is stored
2493+
location string
24922494
// CMEK settings of the log bucket
24932495
cmekSettings dict
24942496
// Creation timestamp
@@ -2606,6 +2608,8 @@ private gcp.project.iamService.serviceAccount.key @defaults("name") {
26062608
private gcp.project.cloudFunction @defaults("name") {
26072609
// Project ID
26082610
projectId string
2611+
// Region where the function is deployed
2612+
region string
26092613
// Cloud function name
26102614
name string
26112615
// Cloud function description
@@ -2687,6 +2691,8 @@ private gcp.project.dataprocService {
26872691
private gcp.project.dataprocService.cluster @defaults("name") {
26882692
// Project ID
26892693
projectId string
2694+
// Region where the cluster is deployed
2695+
region string
26902696
// Cluster name
26912697
name string
26922698
// Cluster UUID

providers/gcp/resources/gcp.lr.go

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

providers/gcp/resources/gcp.lr.manifest.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ resources:
347347
name: {}
348348
network: {}
349349
projectId: {}
350+
region:
351+
min_mondoo_version: 11.0.146
350352
runtime: {}
351353
secretEnvVars: {}
352354
secretVolumes: {}
@@ -1109,6 +1111,8 @@ resources:
11091111
metrics: {}
11101112
name: {}
11111113
projectId: {}
1114+
region:
1115+
min_mondoo_version: 11.0.146
11121116
status: {}
11131117
statusHistory: {}
11141118
uuid: {}
@@ -1914,6 +1918,8 @@ resources:
19141918
description: {}
19151919
indexConfigs: {}
19161920
lifecycleState: {}
1921+
location:
1922+
min_mondoo_version: 11.0.146
19171923
locked: {}
19181924
name: {}
19191925
projectId: {}

providers/gcp/resources/logging.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (g *mqlGcpProjectLoggingservice) buckets() ([]any, error) {
105105

106106
mqlBucket, err := CreateResource(g.MqlRuntime, "gcp.project.loggingservice.bucket", map[string]*llx.RawData{
107107
"projectId": llx.StringData(projectId),
108+
"location": llx.StringData(parseLocationFromPath(bucket.Name)),
108109
"cmekSettings": llx.DictData(mqlCmekSettingsDict),
109110
"created": llx.TimeDataPtr(parseTime(bucket.CreateTime)),
110111
"description": llx.StringData(bucket.Description),
@@ -364,7 +365,7 @@ func (g *mqlGcpProjectLoggingserviceBucket) id() (string, error) {
364365
}
365366

366367
func initGcpProjectLoggingserviceBucket(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
367-
if len(args) > 2 {
368+
if len(args) > 3 {
368369
return args, nil, nil
369370
}
370371

@@ -374,6 +375,7 @@ func initGcpProjectLoggingserviceBucket(runtime *plugin.Runtime, args map[string
374375
}
375376
if ids := getAssetIdentifier(runtime); ids != nil {
376377
args["name"] = llx.StringData(ids.name)
378+
args["location"] = llx.StringData(ids.region)
377379
args["projectId"] = llx.StringData(ids.project)
378380
} else {
379381
return nil, nil, errors.New("no asset identifier found")
@@ -393,9 +395,13 @@ func initGcpProjectLoggingserviceBucket(runtime *plugin.Runtime, args map[string
393395
}
394396

395397
nameVal := args["name"].Value.(string)
398+
locationVal := ""
399+
if args["location"] != nil {
400+
locationVal = args["location"].Value.(string)
401+
}
396402
for _, b := range buckets.Data {
397403
bucket := b.(*mqlGcpProjectLoggingserviceBucket)
398-
if parseResourceName(bucket.Name.Data) == nameVal {
404+
if parseResourceName(bucket.Name.Data) == nameVal && (locationVal == "" || bucket.Location.Data == locationVal) {
399405
return args, bucket, nil
400406
}
401407
}

0 commit comments

Comments
 (0)