Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for GCP Cloud Functions #732

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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