Skip to content

Commit

Permalink
add init function for gcp compute image resources
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev committed Jan 23, 2023
1 parent 1523288 commit 217805d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
6 changes: 3 additions & 3 deletions motor/discovery/gcp/mql_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type gcpObjectPlatformInfo struct {
platform string
}

func MondooObjectID(o gcpObject) string {
func GcpPlatformID(o gcpObject) string {
return "//platformid.api.mondoo.app/runtime/gcp/" + o.service + "/v1/projects/" + o.project + "/regions/" + o.region + "/" + o.objectType + "/" + o.name
}

Expand All @@ -109,11 +109,11 @@ func MqlObjectToAsset(account string, mqlObject mqlObject, tc *providers.Config)
log.Error().Err(err).Msg("missing runtime info")
return nil
}
platformid := MondooObjectID(mqlObject.gcpObject)
platformid := GcpPlatformID(mqlObject.gcpObject)
t := tc.Clone()
t.PlatformId = platformid
return &asset.Asset{
PlatformIds: []string{platformid, mqlObject.gcpObject.id},
PlatformIds: []string{platformid},
Name: mqlObject.name,
Platform: &platform.Platform{
Name: info.platform,
Expand Down
26 changes: 26 additions & 0 deletions resources/packs/gcp/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
"time"

"go.mondoo.com/cnquery/resources"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand All @@ -20,3 +21,28 @@ func parseResourceName(fullPath string) string {
segments := strings.Split(fullPath, "/")
return segments[len(segments)-1]
}

type assetIdentifier struct {
name string
region string
project string
}

func getAssetIdentifier(runtime *resources.Runtime) *assetIdentifier {
a := runtime.Motor.GetAsset()
if a == nil {
return nil
}
var name, region, project string
for _, id := range a.PlatformIds {
if strings.HasPrefix(id, "//platformid.api.mondoo.app/runtime/gcp/") {
// "//platformid.api.mondoo.app/runtime/gcp/{o.service}/v1/projects/{project}/regions/{region}/{objectType}/{name}"
segments := strings.Split(id, "/")
name = segments[len(segments)-1]
region = segments[8]
project = segments[6]
break
}
}
return &assetIdentifier{name: name, region: region, project: project}
}
39 changes: 39 additions & 0 deletions resources/packs/gcp/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,44 @@ func (g *mqlGcpProjectComputeServiceImage) id() (string, error) {
return "gcloud.compute.image/" + id, nil
}

func (g *mqlGcpProjectComputeServiceImage) init(args *resources.Args) (*resources.Args, GcpProjectComputeServiceImage, error) {
if len(*args) > 2 {
return args, nil, nil
}

if ids := getAssetIdentifier(g.MotorRuntime); ids != nil {
(*args)["name"] = ids.name
(*args)["projectId"] = ids.project
}

obj, err := g.MotorRuntime.CreateResource("gcp.project.computeService", "projectId", (*args)["projectId"])
if err != nil {
return nil, nil, err
}
computeSvc := obj.(GcpProjectComputeService)
images, err := computeSvc.Images()
if err != nil {
return nil, nil, err
}

for _, i := range images {
image := i.(GcpProjectComputeServiceImage)
name, err := image.Name()
if err != nil {
return nil, nil, err
}
projectId, err := image.ProjectId()
if err != nil {
return nil, nil, err
}

if name == (*args)["name"] && projectId == (*args)["projectId"] {
return args, image, nil
}
}
return nil, nil, &resources.ResourceNotFound{}
}

func (g *mqlGcpProjectComputeServiceImage) GetSourceDisk() (interface{}, error) {
// TODO: implement
return nil, errors.New("not implemented")
Expand Down Expand Up @@ -945,6 +983,7 @@ func (g *mqlGcpProjectComputeService) GetImages() ([]interface{}, error) {
for _, image := range page.Items {
mqlImage, err := g.MotorRuntime.CreateResource("gcp.project.computeService.image",
"id", strconv.FormatUint(image.Id, 10),
"projectId", projectId,
"name", image.Name,
"description", image.Description,
"architecture", image.Architecture,
Expand Down
2 changes: 2 additions & 0 deletions resources/packs/gcp/gcp.lr
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ private gcp.project.computeService.snapshot @defaults("name") {
private gcp.project.computeService.image @defaults("id name") {
// Unique identifier
id string
// Project ID
projectId string
// Name of the resource
name string
// Optional description
Expand Down
39 changes: 39 additions & 0 deletions resources/packs/gcp/gcp.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion resources/packs/gcp/info/gcp.lr.json

Large diffs are not rendered by default.

0 comments on commit 217805d

Please sign in to comment.