From af205b907bb4064e90248d3cf38c460772305ed6 Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Wed, 13 Mar 2024 23:00:10 +0100 Subject: [PATCH] implement gcp.project.computeService.attachedDisk.source Signed-off-by: Ivan Milchev --- providers/gcp/resources/common.go | 13 +++++++++++++ providers/gcp/resources/compute.go | 28 +++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/providers/gcp/resources/common.go b/providers/gcp/resources/common.go index 7c8892df4b..a47a9aa492 100644 --- a/providers/gcp/resources/common.go +++ b/providers/gcp/resources/common.go @@ -4,6 +4,7 @@ package resources import ( + "errors" "strings" "time" @@ -106,3 +107,15 @@ func getSubnetworkByUrl(subnetUrl string, runtime *plugin.Runtime) (*mqlGcpProje } return res.(*mqlGcpProjectComputeServiceSubnetwork), nil } + +func getDiskIdByUrl(diskUrl string) (*resourceId, error) { + // A reference to a subnetwork is not mandatory for this resource + if diskUrl == "" { + return nil, errors.New("diskUrl is empty") + } + + // Format is https://www.googleapis.com/compute/v1/projects/project1/regions/us-central1/subnetworks/subnet-1 + params := strings.TrimPrefix(diskUrl, "https://www.googleapis.com/compute/v1/") + parts := strings.Split(params, "/") + return &resourceId{Project: parts[1], Region: parts[3], Name: parts[5]}, nil +} diff --git a/providers/gcp/resources/compute.go b/providers/gcp/resources/compute.go index 42c51003dd..273868c6a4 100644 --- a/providers/gcp/resources/compute.go +++ b/providers/gcp/resources/compute.go @@ -443,9 +443,31 @@ func (g *mqlGcpProjectComputeServiceAttachedDisk) id() (string, error) { } func (g *mqlGcpProjectComputeServiceAttachedDisk) source() (*mqlGcpProjectComputeServiceDisk, error) { - // g.attachedDiskSource - // TODO search for reference resource - return nil, nil + diskId, err := getDiskIdByUrl(g.attachedDiskSource) + if err != nil { + return nil, err + } + + obj, err := CreateResource(g.MqlRuntime, "gcp.project.computeService", map[string]*llx.RawData{ + "projectId": llx.StringData(g.ProjectId.Data), + }) + if err != nil { + return nil, err + } + computeSvc := obj.(*mqlGcpProjectComputeService) + disks := computeSvc.GetDisks() + if disks.Error != nil { + return nil, disks.Error + } + + for _, d := range disks.Data { + disk := d.(*mqlGcpProjectComputeServiceDisk) + if disk.Zone.Data.GetName().Data == diskId.Region && disk.Name.Data == diskId.Name { + return disk, nil + } + + } + return nil, errors.New("disk not found") } type mqlGcpProjectComputeServiceInstanceInternal struct {