Skip to content

Commit

Permalink
Merge pull request #715 from ShubhamPalriwala/feature/675-support-gcp…
Browse files Browse the repository at this point in the history
…-api-gateway

Add Support for API Gateways in GCP
  • Loading branch information
mlabouardy authored Apr 14, 2023
2 parents b5776f1 + add89c6 commit f4e819d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
104 changes: 104 additions & 0 deletions providers/gcp/gateway/gateways.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package gateway

import (
"context"
"fmt"
"time"

"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
"google.golang.org/api/apigateway/v1"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)

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

regions, err := listGCPRegions(client.GCPClient.Credentials.ProjectID, option.WithCredentials(client.GCPClient.Credentials))
if err != nil {
logrus.WithError(err).Errorf("failed to list zones to fetch api gateways")
return resources, err
}

apiGatewayService, err := apigateway.NewService(ctx, option.WithCredentials(client.GCPClient.Credentials))
if err != nil {
logrus.WithError(err).Errorf("failed to create API Gateway service")
return resources, err
}

RegionsLoop:
for _, regionName := range regions {
apiGateways, err := apiGatewayService.Projects.Locations.Gateways.List(
"projects/" + client.GCPClient.Credentials.ProjectID + "/locations/" + regionName,
).Do()
if err != nil {
if err.Error() == "googleapi: Error 403: Location "+regionName+" is not found or access is unauthorized., forbidden" {
continue RegionsLoop
} else {
logrus.WithError(err).Errorf("failed to list API Gateways")
return resources, err

}
}

for _, apiGateway := range apiGateways.Gateways {
parsedCreatedTime, err := time.Parse(time.RFC3339Nano, apiGateway.CreateTime)
if err != nil {
logrus.WithError(err).Errorf("failed to parse create time for API Gateways")
return resources, err
}

resources = append(resources, models.Resource{
Provider: "GCP",
Account: client.Name,
Service: "API Gateways",
ResourceId: apiGateway.Name,
Name: apiGateway.DisplayName,
CreatedAt: parsedCreatedTime,
Region: regionName,
Metadata: map[string]string{
"API Config": apiGateway.ApiConfig,
"Default Hostname": apiGateway.DefaultHostname,
"State": apiGateway.State,
},
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://console.cloud.google.com/api-gateway/gateway/%s/location/%s?project=%s", apiGateway.DisplayName, regionName, client.GCPClient.Credentials.ProjectID),
})

}

}

logrus.WithFields(logrus.Fields{
"provider": "GCP",
"account": client.Name,
"service": "API Gateway",
"resources": len(resources),
}).Info("Fetched resources")

return resources, nil
}

func listGCPRegions(projectId string, creds option.ClientOption) ([]string, error) {
var regions []string

ctx := context.Background()
computeService, err := compute.NewService(ctx, creds)
if err != nil {
logrus.WithError(err).Errorf("failed to create new service for fetching GCP regions for api gateway")
return nil, err
}

regionList, err := computeService.Regions.List(projectId).Do()
if err != nil {
logrus.WithError(err).Errorf("failed to list regions for fetching GCP regions for api gateway")
return nil, err
}

for _, region := range regionList.Items {
regions = append(regions, region.Name)
}
return regions, 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/gateway"
"github.com/tailwarden/komiser/providers/gcp/iam"
"github.com/tailwarden/komiser/providers/gcp/kms"
"github.com/tailwarden/komiser/providers/gcp/redis"
Expand All @@ -31,6 +32,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
redis.Instances,
container.Clusters,
kms.Keys,
gateway.ApiGateways,
}
}

Expand Down

0 comments on commit f4e819d

Please sign in to comment.