databases.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  type (
  12  	DatabaseEngineType           string
  13  	DatabaseDayOfWeek            int
  14  	DatabaseMaintenanceFrequency string
  15  	DatabaseStatus               string
  16  	DatabasePlatform             string
  17  	DatabaseMemberType           string
  18  )
  19  
  20  const (
  21  	DatabaseMaintenanceDayMonday DatabaseDayOfWeek = iota + 1
  22  	DatabaseMaintenanceDayTuesday
  23  	DatabaseMaintenanceDayWednesday
  24  	DatabaseMaintenanceDayThursday
  25  	DatabaseMaintenanceDayFriday
  26  	DatabaseMaintenanceDaySaturday
  27  	DatabaseMaintenanceDaySunday
  28  )
  29  
  30  const (
  31  	DatabaseMaintenanceFrequencyWeekly  DatabaseMaintenanceFrequency = "weekly"
  32  	DatabaseMaintenanceFrequencyMonthly DatabaseMaintenanceFrequency = "monthly"
  33  )
  34  
  35  const (
  36  	DatabaseEngineTypeMySQL    DatabaseEngineType = "mysql"
  37  	DatabaseEngineTypePostgres DatabaseEngineType = "postgresql"
  38  )
  39  
  40  const (
  41  	DatabaseStatusProvisioning DatabaseStatus = "provisioning"
  42  	DatabaseStatusActive       DatabaseStatus = "active"
  43  	DatabaseStatusDeleting     DatabaseStatus = "deleting"
  44  	DatabaseStatusDeleted      DatabaseStatus = "deleted"
  45  	DatabaseStatusSuspending   DatabaseStatus = "suspending"
  46  	DatabaseStatusSuspended    DatabaseStatus = "suspended"
  47  	DatabaseStatusResuming     DatabaseStatus = "resuming"
  48  	DatabaseStatusRestoring    DatabaseStatus = "restoring"
  49  	DatabaseStatusFailed       DatabaseStatus = "failed"
  50  	DatabaseStatusDegraded     DatabaseStatus = "degraded"
  51  	DatabaseStatusUpdating     DatabaseStatus = "updating"
  52  	DatabaseStatusBackingUp    DatabaseStatus = "backing_up"
  53  )
  54  
  55  const (
  56  	DatabasePlatformRDBMSLegacy  DatabasePlatform = "rdbms-legacy"
  57  	DatabasePlatformRDBMSDefault DatabasePlatform = "rdbms-default"
  58  )
  59  
  60  const (
  61  	DatabaseMemberTypePrimary  DatabaseMemberType = "primary"
  62  	DatabaseMemberTypeFailover DatabaseMemberType = "failover"
  63  )
  64  
  65  // A Database is a instance of Linode Managed Databases
  66  type Database struct {
  67  	ID              int                       `json:"id"`
  68  	Status          DatabaseStatus            `json:"status"`
  69  	Label           string                    `json:"label"`
  70  	Hosts           DatabaseHost              `json:"hosts"`
  71  	Region          string                    `json:"region"`
  72  	Type            string                    `json:"type"`
  73  	Engine          string                    `json:"engine"`
  74  	Version         string                    `json:"version"`
  75  	ClusterSize     int                       `json:"cluster_size"`
  76  	Platform        DatabasePlatform          `json:"platform"`
  77  	Fork            *DatabaseFork             `json:"fork"`
  78  	Updates         DatabaseMaintenanceWindow `json:"updates"`
  79  	UsedDiskSizeGB  int                       `json:"used_disk_size_gb"`
  80  	TotalDiskSizeGB int                       `json:"total_disk_size_gb"`
  81  	Port            int                       `json:"port"`
  82  
  83  	// Members has dynamic keys so it is a map
  84  	Members map[string]DatabaseMemberType `json:"members"`
  85  
  86  	// Deprecated: ReplicationType is a deprecated property, as it is no longer supported in DBaaS V2.
  87  	ReplicationType string `json:"replication_type"`
  88  	// Deprecated: SSLConnection is a deprecated property, as it is no longer supported in DBaaS V2.
  89  	SSLConnection bool `json:"ssl_connection"`
  90  	// Deprecated: Encrypted is a deprecated property, as it is no longer supported in DBaaS V2.
  91  	Encrypted bool `json:"encrypted"`
  92  
  93  	AllowList         []string   `json:"allow_list"`
  94  	InstanceURI       string     `json:"instance_uri"`
  95  	Created           *time.Time `json:"-"`
  96  	Updated           *time.Time `json:"-"`
  97  	OldestRestoreTime *time.Time `json:"-"`
  98  
  99  	PrivateNetwork *DatabasePrivateNetwork `json:"private_network,omitempty"`
 100  }
 101  
 102  // DatabaseHost for Primary/Secondary of Database
 103  type DatabaseHost struct {
 104  	Primary string `json:"primary"`
 105  	Standby string `json:"standby"`
 106  }
 107  
 108  type DatabasePrivateNetwork struct {
 109  	VPCID        int  `json:"vpc_id"`
 110  	SubnetID     int  `json:"subnet_id"`
 111  	PublicAccess bool `json:"public_access"`
 112  }
 113  
 114  // DatabaseEngine is information about Engines supported by Linode Managed Databases
 115  type DatabaseEngine struct {
 116  	ID      string `json:"id"`
 117  	Engine  string `json:"engine"`
 118  	Version string `json:"version"`
 119  }
 120  
 121  // DatabaseMaintenanceWindow stores information about a MySQL cluster's maintenance window
 122  type DatabaseMaintenanceWindow struct {
 123  	DayOfWeek DatabaseDayOfWeek            `json:"day_of_week"`
 124  	Duration  int                          `json:"duration"`
 125  	Frequency DatabaseMaintenanceFrequency `json:"frequency"`
 126  	HourOfDay int                          `json:"hour_of_day"`
 127  
 128  	Pending []DatabaseMaintenanceWindowPending `json:"pending,omitempty"`
 129  
 130  	// Deprecated: WeekOfMonth is a deprecated property, as it is no longer supported in DBaaS V2.
 131  	WeekOfMonth *int `json:"week_of_month,omitempty"`
 132  }
 133  
 134  type DatabaseMaintenanceWindowPending struct {
 135  	Deadline    *time.Time `json:"-"`
 136  	Description string     `json:"description"`
 137  	PlannedFor  *time.Time `json:"-"`
 138  }
 139  
 140  // DatabaseType is information about the supported Database Types by Linode Managed Databases
 141  type DatabaseType struct {
 142  	ID          string                `json:"id"`
 143  	Label       string                `json:"label"`
 144  	Class       string                `json:"class"`
 145  	VirtualCPUs int                   `json:"vcpus"`
 146  	Disk        int                   `json:"disk"`
 147  	Memory      int                   `json:"memory"`
 148  	Engines     DatabaseTypeEngineMap `json:"engines"`
 149  	Deprecated  bool                  `json:"deprecated"`
 150  }
 151  
 152  // DatabaseTypeEngineMap stores a list of Database Engine types by engine
 153  type DatabaseTypeEngineMap struct {
 154  	MySQL      []DatabaseTypeEngine `json:"mysql"`
 155  	PostgreSQL []DatabaseTypeEngine `json:"postgresql"`
 156  }
 157  
 158  // DatabaseTypeEngine Sizes and Prices
 159  type DatabaseTypeEngine struct {
 160  	Quantity int          `json:"quantity"`
 161  	Price    ClusterPrice `json:"price"`
 162  }
 163  
 164  // ClusterPrice for Hourly and Monthly price models
 165  type ClusterPrice struct {
 166  	Hourly  float32 `json:"hourly"`
 167  	Monthly float32 `json:"monthly"`
 168  }
 169  
 170  // DatabaseFork describes the source and restore time for the fork for forked DBs
 171  type DatabaseFork struct {
 172  	Source      int        `json:"source"`
 173  	RestoreTime *time.Time `json:"-,omitempty"`
 174  }
 175  
 176  func (d *Database) UnmarshalJSON(b []byte) error {
 177  	type Mask Database
 178  
 179  	p := struct {
 180  		*Mask
 181  
 182  		Created           *parseabletime.ParseableTime `json:"created"`
 183  		Updated           *parseabletime.ParseableTime `json:"updated"`
 184  		OldestRestoreTime *parseabletime.ParseableTime `json:"oldest_restore_time"`
 185  	}{
 186  		Mask: (*Mask)(d),
 187  	}
 188  
 189  	if err := json.Unmarshal(b, &p); err != nil {
 190  		return err
 191  	}
 192  
 193  	d.Created = (*time.Time)(p.Created)
 194  	d.Updated = (*time.Time)(p.Updated)
 195  	d.OldestRestoreTime = (*time.Time)(p.OldestRestoreTime)
 196  
 197  	return nil
 198  }
 199  
 200  func (d *DatabaseFork) UnmarshalJSON(b []byte) error {
 201  	type Mask DatabaseFork
 202  
 203  	p := struct {
 204  		*Mask
 205  
 206  		RestoreTime *parseabletime.ParseableTime `json:"restore_time"`
 207  	}{
 208  		Mask: (*Mask)(d),
 209  	}
 210  
 211  	if err := json.Unmarshal(b, &p); err != nil {
 212  		return err
 213  	}
 214  
 215  	d.RestoreTime = (*time.Time)(p.RestoreTime)
 216  
 217  	return nil
 218  }
 219  
 220  func (d *DatabaseMaintenanceWindowPending) UnmarshalJSON(b []byte) error {
 221  	type Mask DatabaseMaintenanceWindowPending
 222  
 223  	p := struct {
 224  		*Mask
 225  
 226  		Deadline   *parseabletime.ParseableTime `json:"deadline"`
 227  		PlannedFor *parseabletime.ParseableTime `json:"planned_for"`
 228  	}{
 229  		Mask: (*Mask)(d),
 230  	}
 231  
 232  	if err := json.Unmarshal(b, &p); err != nil {
 233  		return err
 234  	}
 235  
 236  	d.Deadline = (*time.Time)(p.Deadline)
 237  	d.PlannedFor = (*time.Time)(p.PlannedFor)
 238  
 239  	return nil
 240  }
 241  
 242  // ListDatabases lists all Database instances in Linode Managed Databases for the account
 243  func (c *Client) ListDatabases(ctx context.Context, opts *ListOptions) ([]Database, error) {
 244  	return getPaginatedResults[Database](ctx, c, "databases/instances", opts)
 245  }
 246  
 247  // ListDatabaseEngines lists all Database Engines. This endpoint is cached by default.
 248  func (c *Client) ListDatabaseEngines(ctx context.Context, opts *ListOptions) ([]DatabaseEngine, error) {
 249  	return getPaginatedResults[DatabaseEngine](ctx, c, "databases/engines", opts)
 250  }
 251  
 252  // GetDatabaseEngine returns a specific Database Engine. This endpoint is cached by default.
 253  func (c *Client) GetDatabaseEngine(ctx context.Context, _ *ListOptions, engineID string) (*DatabaseEngine, error) {
 254  	e := formatAPIPath("databases/engines/%s", engineID)
 255  	return doGETRequest[DatabaseEngine](ctx, c, e)
 256  }
 257  
 258  // ListDatabaseTypes lists all Types of Database provided in Linode Managed Databases. This endpoint is cached by default.
 259  func (c *Client) ListDatabaseTypes(ctx context.Context, opts *ListOptions) ([]DatabaseType, error) {
 260  	return getPaginatedResults[DatabaseType](ctx, c, "databases/types", opts)
 261  }
 262  
 263  // GetDatabaseType returns a specific Database Type. This endpoint is cached by default.
 264  func (c *Client) GetDatabaseType(ctx context.Context, _ *ListOptions, typeID string) (*DatabaseType, error) {
 265  	e := formatAPIPath("databases/types/%s", typeID)
 266  	return doGETRequest[DatabaseType](ctx, c, e)
 267  }
 268