Skip to content

Commit

Permalink
implement gcp.project.computeService.attachedDisk.source
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev committed Mar 13, 2024
1 parent d03b6fd commit d5160f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
13 changes: 13 additions & 0 deletions providers/gcp/resources/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package resources

import (
"errors"
"strings"
"time"

Expand Down Expand Up @@ -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/disks/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
}
28 changes: 25 additions & 3 deletions providers/gcp/resources/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d5160f8

Please sign in to comment.