-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #710 from ShubhamPalriwala/feature/674-support-gcp…
…-sql Add Support for SQL Instances in GCP
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |