diff --git a/databases.go b/databases.go index c70e9656..a0240735 100644 --- a/databases.go +++ b/databases.go @@ -9,28 +9,29 @@ import ( ) const ( - databaseBasePath = "/v2/databases" - databaseSinglePath = databaseBasePath + "/%s" - databaseCAPath = databaseBasePath + "/%s/ca" - databaseConfigPath = databaseBasePath + "/%s/config" - databaseResizePath = databaseBasePath + "/%s/resize" - databaseMigratePath = databaseBasePath + "/%s/migrate" - databaseMaintenancePath = databaseBasePath + "/%s/maintenance" - databaseBackupsPath = databaseBasePath + "/%s/backups" - databaseUsersPath = databaseBasePath + "/%s/users" - databaseUserPath = databaseBasePath + "/%s/users/%s" - databaseResetUserAuthPath = databaseUserPath + "/reset_auth" - databaseDBPath = databaseBasePath + "/%s/dbs/%s" - databaseDBsPath = databaseBasePath + "/%s/dbs" - databasePoolPath = databaseBasePath + "/%s/pools/%s" - databasePoolsPath = databaseBasePath + "/%s/pools" - databaseReplicaPath = databaseBasePath + "/%s/replicas/%s" - databaseReplicasPath = databaseBasePath + "/%s/replicas" - databaseEvictionPolicyPath = databaseBasePath + "/%s/eviction_policy" - databaseSQLModePath = databaseBasePath + "/%s/sql_mode" - databaseFirewallRulesPath = databaseBasePath + "/%s/firewall" - databaseOptionsPath = databaseBasePath + "/options" - databaseUpgradeMajorVersionPath = databaseBasePath + "/%s/upgrade" + databaseBasePath = "/v2/databases" + databaseSinglePath = databaseBasePath + "/%s" + databaseCAPath = databaseBasePath + "/%s/ca" + databaseConfigPath = databaseBasePath + "/%s/config" + databaseResizePath = databaseBasePath + "/%s/resize" + databaseMigratePath = databaseBasePath + "/%s/migrate" + databaseMaintenancePath = databaseBasePath + "/%s/maintenance" + databaseBackupsPath = databaseBasePath + "/%s/backups" + databaseUsersPath = databaseBasePath + "/%s/users" + databaseUserPath = databaseBasePath + "/%s/users/%s" + databaseResetUserAuthPath = databaseUserPath + "/reset_auth" + databaseDBPath = databaseBasePath + "/%s/dbs/%s" + databaseDBsPath = databaseBasePath + "/%s/dbs" + databasePoolPath = databaseBasePath + "/%s/pools/%s" + databasePoolsPath = databaseBasePath + "/%s/pools" + databaseReplicaPath = databaseBasePath + "/%s/replicas/%s" + databaseReplicasPath = databaseBasePath + "/%s/replicas" + databaseEvictionPolicyPath = databaseBasePath + "/%s/eviction_policy" + databaseSQLModePath = databaseBasePath + "/%s/sql_mode" + databaseFirewallRulesPath = databaseBasePath + "/%s/firewall" + databaseOptionsPath = databaseBasePath + "/options" + databaseUpgradeMajorVersionPath = databaseBasePath + "/%s/upgrade" + databasePromoteReplicaToPrimaryPath = databaseReplicaPath + "/promote" ) // SQL Mode constants allow for MySQL-specific SQL flavor configuration. @@ -130,6 +131,7 @@ type DatabasesService interface { ListReplicas(context.Context, string, *ListOptions) ([]DatabaseReplica, *Response, error) CreateReplica(context.Context, string, *DatabaseCreateReplicaRequest) (*DatabaseReplica, *Response, error) DeleteReplica(context.Context, string, string) (*Response, error) + PromoteReplicaToPrimary(context.Context, string, string) (*Response, error) GetEvictionPolicy(context.Context, string) (string, *Response, error) SetEvictionPolicy(context.Context, string, string) (*Response, error) GetSQLMode(context.Context, string) (string, *Response, error) @@ -1013,6 +1015,20 @@ func (svc *DatabasesServiceOp) DeleteReplica(ctx context.Context, databaseID, na return resp, nil } +// PromoteReplicaToPrimary will sever the read replica integration and then promote the replica cluster to be a R/W cluster +func (svc *DatabasesServiceOp) PromoteReplicaToPrimary(ctx context.Context, databaseID, name string) (*Response, error) { + path := fmt.Sprintf(databasePromoteReplicaToPrimaryPath, databaseID, name) + req, err := svc.client.NewRequest(ctx, http.MethodPut, path, nil) + if err != nil { + return nil, err + } + resp, err := svc.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + return resp, nil +} + // GetEvictionPolicy loads the eviction policy for a given Redis cluster. func (svc *DatabasesServiceOp) GetEvictionPolicy(ctx context.Context, databaseID string) (string, *Response, error) { path := fmt.Sprintf(databaseEvictionPolicyPath, databaseID) diff --git a/databases_test.go b/databases_test.go index 2c46dd61..9809c05b 100644 --- a/databases_test.go +++ b/databases_test.go @@ -1321,6 +1321,22 @@ func TestDatabases_CreateReplica(t *testing.T) { require.Equal(t, want, got) } +func TestDatabases_PromoteReplicaToPrimary(t *testing.T) { + setup() + defer teardown() + + dbID := "deadbeef-dead-4aa5-beef-deadbeef347d" + + path := fmt.Sprintf("/v2/databases/%s/replicas/replica/promote", dbID) + + mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPut) + }) + + _, err := client.Databases.PromoteReplicaToPrimary(ctx, dbID, "replica") + require.NoError(t, err) +} + func TestDatabases_DeleteReplica(t *testing.T) { setup() defer teardown()