Skip to content

Commit

Permalink
Add Postgres Cloud SQL support
Browse files Browse the repository at this point in the history
  • Loading branch information
r0mant committed Mar 22, 2021
1 parent 3e86d6e commit ebc2b30
Show file tree
Hide file tree
Showing 23 changed files with 2,870 additions and 710 deletions.
52 changes: 47 additions & 5 deletions api/types/databaseserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ type DatabaseServer interface {
GetURI() string
// GetCA returns the database CA certificate bytes.
GetCA() []byte
// GetRegion returns the AWS region for RDS/Aurora databases.
GetRegion() string
// GetAWS returns AWS information for RDS/Aurora databases.
GetAWS() AWS
// GetGCP returns GCP information for Cloud SQL databases.
GetGCP() GCPCloudSQL
// GetType returns the database type, self-hosted or AWS RDS.
GetType() string
// IsRDS returns true if this is an RDS/Aurora database.
IsRDS() bool
// IsCloudSQL returns true if this is a Cloud SQL database.
IsCloudSQL() bool
// CheckAndSetDefaults checks and set default values for any missing fields.
CheckAndSetDefaults() error
// Copy returns a copy of this database server object.
Expand Down Expand Up @@ -235,21 +239,34 @@ func (s *DatabaseServerV3) GetCA() []byte {
return s.Spec.CACert
}

// GetRegion returns the AWS region for RDS/Aurora databases.
func (s *DatabaseServerV3) GetRegion() string {
return s.Spec.AWS.Region
// GetAWS returns AWS information for RDS/Aurora databases.
func (s *DatabaseServerV3) GetAWS() AWS {
return s.Spec.AWS
}

// GetGCP returns GCP information for Cloud SQL databases.
func (s *DatabaseServerV3) GetGCP() GCPCloudSQL {
return s.Spec.GCP
}

// IsRDS returns true if this database represents AWS RDS/Aurora instance.
func (s *DatabaseServerV3) IsRDS() bool {
return s.GetType() == DatabaseTypeRDS
}

// IsCloudSQL returns true if this database is a Cloud SQL instance.
func (s *DatabaseServerV3) IsCloudSQL() bool {
return s.GetType() == DatabaseTypeCloudSQL
}

// GetType returns the database type, self-hosted or AWS RDS.
func (s *DatabaseServerV3) GetType() string {
if s.Spec.AWS.Region != "" {
return DatabaseTypeRDS
}
if s.Spec.GCP.ProjectID != "" {
return DatabaseTypeCloudSQL
}
return DatabaseTypeSelfHosted
}

Expand Down Expand Up @@ -297,6 +314,8 @@ const (
DatabaseTypeSelfHosted = "self-hosted"
// DatabaseTypeRDS is AWS-hosted RDS or Aurora database.
DatabaseTypeRDS = "rds"
// DatabaseTypeCloudSQL is GCP-hosted Cloud SQL database.
DatabaseTypeCloudSQL = "gcp"
)

// SortedDatabaseServers implements sorter for database servers.
Expand All @@ -310,3 +329,26 @@ func (s SortedDatabaseServers) Less(i, j int) bool { return s[i].GetName() < s[j

// Swap swaps two database servers.
func (s SortedDatabaseServers) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

// DatabaseServers is a list of database servers.
type DatabaseServers []DatabaseServer

// HasRDS returns true if an AWS RDS database is present among this list.
func (s DatabaseServers) HasRDS() bool {
for _, d := range s {
if d.IsRDS() {
return true
}
}
return false
}

// HasGCP returns true if a GCP Cloud SQL database is present among this list.
func (s DatabaseServers) HasGCP() bool {
for _, d := range s {
if d.IsCloudSQL() {
return true
}
}
return false
}
Loading

0 comments on commit ebc2b30

Please sign in to comment.