Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ezilber-akamai committed Nov 21, 2024
1 parent b6c358f commit d7f6e19
Show file tree
Hide file tree
Showing 12 changed files with 4,765 additions and 45,219 deletions.
57 changes: 36 additions & 21 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,24 @@ const (

// A Database is a instance of Linode Managed Databases
type Database struct {
ID int `json:"id"`
Status DatabaseStatus `json:"status"`
Label string `json:"label"`
Hosts DatabaseHost `json:"hosts"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
Version string `json:"version"`
ClusterSize int `json:"cluster_size"`
ReplicationType string `json:"replication_type"`
SSLConnection bool `json:"ssl_connection"`
Encrypted bool `json:"encrypted"`
AllowList []string `json:"allow_list"`
InstanceURI string `json:"instance_uri"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
ID int `json:"id"`
Status DatabaseStatus `json:"status"`
Label string `json:"label"`
Hosts DatabaseHost `json:"hosts"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
Version string `json:"version"`
ClusterSize int `json:"cluster_size"`

ReplicationType string `json:"replication_type,omitempty"` // This field doesn't exist in DBaaS v2
SSLConnection bool `json:"ssl_connection"` // This field doesn't exist in DBaaS v2

Encrypted bool `json:"encrypted"`
AllowList []string `json:"allow_list"`
InstanceURI string `json:"instance_uri"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}

// DatabaseHost for Primary/Secondary of Database
Expand All @@ -85,11 +87,18 @@ type DatabaseEngine struct {

// DatabaseMaintenanceWindow stores information about a MySQL cluster's maintenance window
type DatabaseMaintenanceWindow struct {
DayOfWeek DatabaseDayOfWeek `json:"day_of_week"`
Duration int `json:"duration"`
Frequency DatabaseMaintenanceFrequency `json:"frequency"`
HourOfDay int `json:"hour_of_day"`
WeekOfMonth *int `json:"week_of_month"`
DayOfWeek DatabaseDayOfWeek `json:"day_of_week"`
Duration int `json:"duration"`
Frequency DatabaseMaintenanceFrequency `json:"frequency"`
HourOfDay int `json:"hour_of_day"`

Pending []struct {
Deadline *time.Time `json:"deadline"`
Description string `json:"description"`
PlannedFor *time.Time `json:"planned_for"`
} `json:"pending,omitempty"`

WeekOfMonth *int `json:"week_of_month,omitempty"` // This field doesn't exist in v2
}

// DatabaseType is information about the supported Database Types by Linode Managed Databases
Expand Down Expand Up @@ -120,6 +129,12 @@ type ClusterPrice struct {
Monthly float32 `json:"monthly"`
}

// DatabaseFork describes the source and restore time for the fork for forked DBs
type DatabaseFork struct {
Source int `json:"source"`
RestoreTime *time.Time `json:"restore_time"`
}

func (d *Database) UnmarshalJSON(b []byte) error {
type Mask Database

Expand Down
72 changes: 44 additions & 28 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,37 @@ const (

// A MySQLDatabase is an instance of Linode MySQL Managed Databases
type MySQLDatabase struct {
ID int `json:"id"`
Status DatabaseStatus `json:"status"`
Label string `json:"label"`
Hosts DatabaseHost `json:"hosts"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
Version string `json:"version"`
ClusterSize int `json:"cluster_size"`
ReplicationType string `json:"replication_type"`
SSLConnection bool `json:"ssl_connection"`
Encrypted bool `json:"encrypted"`
AllowList []string `json:"allow_list"`
InstanceURI string `json:"instance_uri"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
Updates DatabaseMaintenanceWindow `json:"updates"`
ID int `json:"id"`
Status DatabaseStatus `json:"status"`
Label string `json:"label"`
Hosts DatabaseHost `json:"hosts"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
Version string `json:"version"`
ClusterSize int `json:"cluster_size"`

ReplicationType string `json:"replication_type,omitempty"` // This field doesn't exist in DBaaS v2

SSLConnection bool `json:"ssl_connection"`
Encrypted bool `json:"encrypted"`
AllowList []string `json:"allow_list"`
InstanceURI string `json:"instance_uri"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
Updates DatabaseMaintenanceWindow `json:"updates"`
Fork DatabaseFork `json:"fork"`
OldestRestoreTime *time.Time `json:"-"`
}

func (d *MySQLDatabase) UnmarshalJSON(b []byte) error {
type Mask MySQLDatabase

p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
OldestRestoreTime *parseabletime.ParseableTime `json:"oldest_restore_time"`
}{
Mask: (*Mask)(d),
}
Expand All @@ -55,30 +60,36 @@ func (d *MySQLDatabase) UnmarshalJSON(b []byte) error {

d.Created = (*time.Time)(p.Created)
d.Updated = (*time.Time)(p.Updated)
d.OldestRestoreTime = (*time.Time)(p.OldestRestoreTime)
return nil
}

// MySQLCreateOptions fields are used when creating a new MySQL Database
type MySQLCreateOptions struct {
Label string `json:"label"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
AllowList []string `json:"allow_list,omitempty"`
ReplicationType string `json:"replication_type,omitempty"`
ClusterSize int `json:"cluster_size,omitempty"`
Encrypted bool `json:"encrypted,omitempty"`
SSLConnection bool `json:"ssl_connection,omitempty"`
Label string `json:"label"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
AllowList []string `json:"allow_list,omitempty"`
ClusterSize int `json:"cluster_size,omitempty"`

ReplicationType string `json:"replication_type,omitempty"` // This field doesn't exist in DBaaS v2
Encrypted bool `json:"encrypted,omitempty"` // This field doesn't exist in DBaaS v2
SSLConnection bool `json:"ssl_connection,omitempty"` // This field doesn't exist in DBaaS v2

Fork *DatabaseFork `json:"fork,omitempty"`
}

// MySQLUpdateOptions fields are used when altering the existing MySQL Database
type MySQLUpdateOptions struct {
Label string `json:"label,omitempty"`
AllowList *[]string `json:"allow_list,omitempty"`
Updates *DatabaseMaintenanceWindow `json:"updates,omitempty"`
Type string `json:"type,omitempty"`
}

// MySQLDatabaseBackup is information for interacting with a backup for the existing MySQL Database
// This struct is not supported in DBaaS v2
type MySQLDatabaseBackup struct {
ID int `json:"id"`
Label string `json:"label"`
Expand All @@ -87,6 +98,7 @@ type MySQLDatabaseBackup struct {
}

// MySQLBackupCreateOptions are options used for CreateMySQLDatabaseBackup(...)
// This struct is not supported in DBaaS v2
type MySQLBackupCreateOptions struct {
Label string `json:"label"`
Target MySQLDatabaseTarget `json:"target"`
Expand Down Expand Up @@ -132,6 +144,7 @@ func (c *Client) ListMySQLDatabases(ctx context.Context, opts *ListOptions) ([]M
}

// ListMySQLDatabaseBackups lists all MySQL Database Backups associated with the given MySQL Database
// Note: This method is not supported in DBaaS V2
func (c *Client) ListMySQLDatabaseBackups(ctx context.Context, databaseID int, opts *ListOptions) ([]MySQLDatabaseBackup, error) {
response, err := getPaginatedResults[MySQLDatabaseBackup](ctx, c, formatAPIPath("databases/mysql/instances/%d/backups", databaseID), opts)
if err != nil {
Expand Down Expand Up @@ -211,6 +224,7 @@ func (c *Client) ResetMySQLDatabaseCredentials(ctx context.Context, databaseID i
}

// GetMySQLDatabaseBackup returns a specific MySQL Database Backup with the given ids
// Note: This method is not supported in DBaaS V2
func (c *Client) GetMySQLDatabaseBackup(ctx context.Context, databaseID int, backupID int) (*MySQLDatabaseBackup, error) {
e := formatAPIPath("databases/mysql/instances/%d/backups/%d", databaseID, backupID)
response, err := doGETRequest[MySQLDatabaseBackup](ctx, c, e)
Expand All @@ -222,13 +236,15 @@ func (c *Client) GetMySQLDatabaseBackup(ctx context.Context, databaseID int, bac
}

// RestoreMySQLDatabaseBackup returns the given MySQL Database with the given Backup
// Note: This method is not supported in DBaaS V2
func (c *Client) RestoreMySQLDatabaseBackup(ctx context.Context, databaseID int, backupID int) error {
e := formatAPIPath("databases/mysql/instances/%d/backups/%d/restore", databaseID, backupID)
_, err := doPOSTRequest[MySQLDatabaseBackup, any](ctx, c, e)
return err
}

// CreateMySQLDatabaseBackup creates a snapshot for the given MySQL database
// Note: This method is not supported in DBaaS V2
func (c *Client) CreateMySQLDatabaseBackup(ctx context.Context, databaseID int, opts MySQLBackupCreateOptions) error {
e := formatAPIPath("databases/mysql/instances/%d/backups", databaseID)
_, err := doPOSTRequest[MySQLDatabaseBackup](ctx, c, e, opts)
Expand Down
76 changes: 46 additions & 30 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,38 @@ const (

// A PostgresDatabase is an instance of Linode Postgres Managed Databases
type PostgresDatabase struct {
ID int `json:"id"`
Status DatabaseStatus `json:"status"`
Label string `json:"label"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
Version string `json:"version"`
Encrypted bool `json:"encrypted"`
AllowList []string `json:"allow_list"`
Port int `json:"port"`
SSLConnection bool `json:"ssl_connection"`
ClusterSize int `json:"cluster_size"`
ReplicationCommitType PostgresCommitType `json:"replication_commit_type"`
ReplicationType PostgresReplicationType `json:"replication_type"`
Hosts DatabaseHost `json:"hosts"`
Updates DatabaseMaintenanceWindow `json:"updates"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
ID int `json:"id"`
Status DatabaseStatus `json:"status"`
Label string `json:"label"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
Version string `json:"version"`
Encrypted bool `json:"encrypted"`
AllowList []string `json:"allow_list"`
Port int `json:"port"`
SSLConnection bool `json:"ssl_connection"`
ClusterSize int `json:"cluster_size"`

ReplicationCommitType PostgresCommitType `json:"replication_commit_type,omitempty"` // This field doesn't exist in DBaaS v2
ReplicationType PostgresReplicationType `json:"replication_type,omitempty"` // This field doesn't exist in DBaaS v2

Hosts DatabaseHost `json:"hosts"`
Updates DatabaseMaintenanceWindow `json:"updates"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
Fork DatabaseFork `json:"fork"`
OldestRestoreTime *time.Time `json:"-"`
}

func (d *PostgresDatabase) UnmarshalJSON(b []byte) error {
type Mask PostgresDatabase

p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
OldestRestoreTime *parseabletime.ParseableTime `json:"oldest_restore_time"`
}{
Mask: (*Mask)(d),
}
Expand All @@ -72,28 +77,33 @@ func (d *PostgresDatabase) UnmarshalJSON(b []byte) error {

d.Created = (*time.Time)(p.Created)
d.Updated = (*time.Time)(p.Updated)
d.OldestRestoreTime = (*time.Time)(p.OldestRestoreTime)
return nil
}

// PostgresCreateOptions fields are used when creating a new Postgres Database
type PostgresCreateOptions struct {
Label string `json:"label"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
AllowList []string `json:"allow_list,omitempty"`
ClusterSize int `json:"cluster_size,omitempty"`
Encrypted bool `json:"encrypted,omitempty"`
SSLConnection bool `json:"ssl_connection,omitempty"`
ReplicationType PostgresReplicationType `json:"replication_type,omitempty"`
ReplicationCommitType PostgresCommitType `json:"replication_commit_type,omitempty"`
Label string `json:"label"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
AllowList []string `json:"allow_list,omitempty"`
ClusterSize int `json:"cluster_size,omitempty"`

Encrypted bool `json:"encrypted,omitempty"` // This field doesn't exist in DBaaS v2
SSLConnection bool `json:"ssl_connection,omitempty"` // This field doesn't exist in DBaaS v2
ReplicationType PostgresReplicationType `json:"replication_type,omitempty"` // This field doesn't exist in DBaaS v2
ReplicationCommitType PostgresCommitType `json:"replication_commit_type,omitempty"` // This field doesn't exist in DBaaS v2

Fork *DatabaseFork `json:"fork,omitempty"`
}

// PostgresUpdateOptions fields are used when altering the existing Postgres Database
type PostgresUpdateOptions struct {
Label string `json:"label,omitempty"`
AllowList *[]string `json:"allow_list,omitempty"`
Updates *DatabaseMaintenanceWindow `json:"updates,omitempty"`
Type string `json:"type,omitempty"`
}

// PostgresDatabaseSSL is the SSL Certificate to access the Linode Managed Postgres Database
Expand All @@ -114,6 +124,7 @@ func (c *Client) ListPostgresDatabases(ctx context.Context, opts *ListOptions) (
}

// PostgresDatabaseBackup is information for interacting with a backup for the existing Postgres Database
// This struct is not supported in DBaaS v2
type PostgresDatabaseBackup struct {
ID int `json:"id"`
Label string `json:"label"`
Expand All @@ -140,12 +151,14 @@ func (d *PostgresDatabaseBackup) UnmarshalJSON(b []byte) error {
}

// PostgresBackupCreateOptions are options used for CreatePostgresDatabaseBackup(...)
// This struct is not supported in DBaaS v2
type PostgresBackupCreateOptions struct {
Label string `json:"label"`
Target PostgresDatabaseTarget `json:"target"`
}

// ListPostgresDatabaseBackups lists all Postgres Database Backups associated with the given Postgres Database
// Note: This method is not supported in DBaaS V2
func (c *Client) ListPostgresDatabaseBackups(ctx context.Context, databaseID int, opts *ListOptions) ([]PostgresDatabaseBackup, error) {
response, err := getPaginatedResults[PostgresDatabaseBackup](ctx, c, formatAPIPath("databases/postgresql/instances/%d/backups", databaseID), opts)
return response, err
Expand Down Expand Up @@ -208,20 +221,23 @@ func (c *Client) GetPostgresDatabaseSSL(ctx context.Context, databaseID int) (*P
}

// GetPostgresDatabaseBackup returns a specific Postgres Database Backup with the given ids
// Note: This method is not supported in DBaaS V2
func (c *Client) GetPostgresDatabaseBackup(ctx context.Context, databaseID int, backupID int) (*PostgresDatabaseBackup, error) {
e := formatAPIPath("databases/postgresql/instances/%d/backups/%d", databaseID, backupID)
response, err := doGETRequest[PostgresDatabaseBackup](ctx, c, e)
return response, err
}

// RestorePostgresDatabaseBackup returns the given Postgres Database with the given Backup
// Note: This method is not supported in DBaaS V2
func (c *Client) RestorePostgresDatabaseBackup(ctx context.Context, databaseID int, backupID int) error {
e := formatAPIPath("databases/postgresql/instances/%d/backups/%d/restore", databaseID, backupID)
_, err := doPOSTRequest[PostgresDatabaseBackup, any](ctx, c, e)
return err
}

// CreatePostgresDatabaseBackup creates a snapshot for the given Postgres database
// Note: This method is not supported in DBaaS V2
func (c *Client) CreatePostgresDatabaseBackup(ctx context.Context, databaseID int, opts PostgresBackupCreateOptions) error {
e := formatAPIPath("databases/postgresql/instances/%d/backups", databaseID)
_, err := doPOSTRequest[PostgresDatabaseBackup](ctx, c, e, opts)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDatabase_Engine(t *testing.T) {
}

if engine.Engine != response.Engine {
t.Fatal("recieved engine does not match source")
t.Fatal("received engine does not match source")
}
}

Expand All @@ -57,7 +57,7 @@ func TestDatabase_Type(t *testing.T) {
}

if aType.Label != response.Label {
t.Fatal("recieved type does not match source")
t.Fatal("received type does not match source")
}

if response.Engines.MySQL[0].Quantity != aType.Engines.MySQL[0].Quantity {
Expand Down
Loading

0 comments on commit d7f6e19

Please sign in to comment.