Skip to content

Commit

Permalink
Adding PromoteReplicaToPrimary to client interface. (#599)
Browse files Browse the repository at this point in the history
* Adding PromoteReplicaToPrimary to client interface.  Adding unit test for new method.

* Updating the PR number in the Change Log

* Update CHANGELOG.md

removing unneeded change

Co-authored-by: danaelhe <[email protected]>

---------

Co-authored-by: danaelhe <[email protected]>
  • Loading branch information
markpaulson and danaelhe authored Feb 1, 2023
1 parent 8f2b138 commit 7a914b3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
60 changes: 38 additions & 22 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7a914b3

Please sign in to comment.