Skip to content

Commit

Permalink
Merge pull request #732 from ShubhamPalriwala/feature/669-gcp-cloud-f…
Browse files Browse the repository at this point in the history
…unctions

Add Support for GCP Cloud Functions
  • Loading branch information
eneskaya authored Apr 19, 2023
2 parents 0d4803d + 486dfe0 commit 458b968
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
77 changes: 77 additions & 0 deletions providers/gcp/function/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package function

import (
"context"
"fmt"
"regexp"
"time"

log "github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
"google.golang.org/api/cloudfunctions/v2"
"google.golang.org/api/option"
)

func Functions(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)

cloudFunctionsService, err := cloudfunctions.NewService(ctx, option.WithCredentials(client.GCPClient.Credentials))
if err != nil {
log.WithError(err).Errorf("failed to create Cloud Functions service")
return resources, err
}

functions, err := cloudFunctionsService.Projects.Locations.Functions.List(
"projects/" + client.GCPClient.Credentials.ProjectID + "/locations/-",
).Do()
if err != nil {
log.WithError(err).Errorf("failed to list Cloud Functions")
return resources, err
}

for _, function := range functions.Functions {
fmt.Printf("%+v\n", function)

re := regexp.MustCompile(`projects\/.*?\/locations\/(.+?)\/functions\/(.+)`)
match := re.FindStringSubmatch(function.Name)

generation := "gen1"
functionRegion := ""
functionName := function.Name

if function.Environment == "GEN_2" {
generation = "gen2"
}
if len(match) == 3 {
functionRegion = match[1]
functionName = match[2]

}

resources = append(resources, models.Resource{
Provider: "GCP",
Account: client.Name,
Service: "Cloud Functions",
ResourceId: function.Name,
Region: functionRegion,
Name: functionName,
Metadata: map[string]string{
"Version": function.Environment,
"Last Modified": function.UpdateTime,
},
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://console.cloud.google.com/functions/details/%s/%s?env=%sproject=%s", functionRegion, functionName, generation, client.GCPClient.Credentials.ProjectID),
})

}

log.WithFields(log.Fields{
"provider": "GCP",
"account": client.Name,
"service": "Cloud Functions",
"resources": len(resources),
}).Info("Fetched resources")

return resources, nil
}
2 changes: 2 additions & 0 deletions providers/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
certficate "github.com/tailwarden/komiser/providers/gcp/certificate"
"github.com/tailwarden/komiser/providers/gcp/compute"
"github.com/tailwarden/komiser/providers/gcp/container"
"github.com/tailwarden/komiser/providers/gcp/function"
"github.com/tailwarden/komiser/providers/gcp/gateway"
"github.com/tailwarden/komiser/providers/gcp/iam"
"github.com/tailwarden/komiser/providers/gcp/kms"
Expand All @@ -33,6 +34,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
container.Clusters,
kms.Keys,
gateway.ApiGateways,
function.Functions,
}
}

Expand Down

0 comments on commit 458b968

Please sign in to comment.