Skip to content

Commit

Permalink
Merge pull request #710 from ShubhamPalriwala/feature/674-support-gcp…
Browse files Browse the repository at this point in the history
…-sql

Add Support for SQL Instances in GCP
  • Loading branch information
mlabouardy authored Apr 9, 2023
2 parents 936475d + 6a9f178 commit 0bbaba6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions providers/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/tailwarden/komiser/providers/gcp/bigquery"
certficate "github.com/tailwarden/komiser/providers/gcp/certificate"
"github.com/tailwarden/komiser/providers/gcp/compute"
"github.com/tailwarden/komiser/providers/gcp/sql"
"github.com/tailwarden/komiser/providers/gcp/storage"
"github.com/tailwarden/komiser/utils"
"github.com/uptrace/bun"
Expand All @@ -19,6 +20,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
storage.Buckets,
bigquery.BigQueryTables,
certficate.Certificates,
sql.SqlInstances,
}
}

Expand Down
53 changes: 53 additions & 0 deletions providers/gcp/sql/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package sql

import (
"context"
"fmt"
"time"

"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
"google.golang.org/api/option"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

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

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

instances, err := instancesClient.Instances.List(client.GCPClient.Credentials.ProjectID).Do()
if err != nil {
logrus.WithError(err).Errorf("failed to lisrt sql servers")
return resources, err
}

for _, sqlInstance := range instances.Items {
resources = append(resources, models.Resource{
Provider: "GCP",
Account: client.Name,
Service: "SQL Instance",
Name: sqlInstance.Name,
Region: sqlInstance.Region,
FetchedAt: time.Now(),
Metadata: map[string]string{
"databaseVersion": sqlInstance.DatabaseVersion,
},
Link: fmt.Sprintf("https://console.cloud.google.com/sql/instances/%s/overview?project=%s", sqlInstance.Name, client.GCPClient.Credentials.ProjectID),
})
}

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

return resources, nil
}

0 comments on commit 0bbaba6

Please sign in to comment.