Skip to content

Commit

Permalink
feat(GaussDB): gaussdb mysql proxy support consistence mode (#5256)
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 authored Jul 23, 2024
1 parent 4521c8b commit 59bd5bd
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/resources/gaussdb_mysql_proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ The following arguments are supported:

Defaults to **OFF**.

* `consistence_mode` - (Optional, String) Specifies the consistency mode of the proxy. Value options:
+ **session**: session consistency.
+ **global**: global consistency.
+ **eventual**: eventual consistency.

Defaults to **eventual**.

<a name="node_weight_struct"></a>
The `master_node_weight` and `readonly_nodes_weight` block supports:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestAccGaussDBMySQLProxy_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "parameters.0.name", "multiStatementType"),
resource.TestCheckResourceAttr(resourceName, "parameters.0.value", "Loose"),
resource.TestCheckResourceAttr(resourceName, "parameters.0.elem_type", "system"),
resource.TestCheckResourceAttr(resourceName, "consistence_mode", "session"),
),
},
{
Expand All @@ -125,6 +126,7 @@ func TestAccGaussDBMySQLProxy_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "parameters.0.name", "looseImciApThreshold"),
resource.TestCheckResourceAttr(resourceName, "parameters.0.value", "6000"),
resource.TestCheckResourceAttr(resourceName, "parameters.0.elem_type", "system"),
resource.TestCheckResourceAttr(resourceName, "consistence_mode", "eventual"),
),
},
{
Expand Down Expand Up @@ -201,6 +203,7 @@ resource "huaweicloud_gaussdb_mysql_proxy" "test" {
new_node_weight = 20
port = 3339
transaction_split = "ON"
consistence_mode = "session"
master_node_weight {
id = local.sort_nodes[0].id
Expand Down Expand Up @@ -237,6 +240,7 @@ resource "huaweicloud_gaussdb_mysql_proxy" "test" {
new_node_weight = 20
port = 3338
transaction_split = "OFF"
consistence_mode = "eventual"
master_node_weight {
id = local.sort_nodes[0].id
Expand Down Expand Up @@ -278,6 +282,7 @@ resource "huaweicloud_gaussdb_mysql_proxy" "test" {
new_node_weight = 20
port = 3338
transaction_split = "OFF"
consistence_mode = "eventual"
master_node_weight {
id = local.sort_nodes[0].id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
// @API GaussDBforMySQL PUT /v3/{project_id}/instances/{instance_id}/proxy/{proxy_id}/weight
// @API GaussDBforMySQL POST /v3/{project_id}/instances/{instance_id}/proxy/{proxy_id}/new-node-auto-add
// @API GaussDBforMySQL POST /v3/{project_id}/instances/{instance_id}/proxy/transaction-split
// @API GaussDBforMySQL PUT /v3/{project_id}/instances/{instance_id}/proxy/{proxy_id}/session-consistence
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/proxies
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/proxy/{proxy_id}/configurations
// @API GaussDBforMySQL DELETE /v3/{project_id}/instances/{instance_id}/proxy
Expand Down Expand Up @@ -148,6 +149,11 @@ func ResourceGaussDBProxy() *schema.Resource {
Optional: true,
Computed: true,
},
"consistence_mode": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"address": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -278,6 +284,13 @@ func resourceGaussDBProxyCreate(ctx context.Context, d *schema.ResourceData, met
}
}

if consistenceMode, ok := d.GetOk("consistence_mode"); ok && consistenceMode != "eventual" {
err = updateGaussDBMySQLConsistenceMode(ctx, d, client)
if err != nil {
return diag.FromErr(err)
}
}

return resourceGaussDBProxyRead(ctx, d, meta)
}

Expand Down Expand Up @@ -349,6 +362,7 @@ func resourceGaussDBProxyRead(_ context.Context, d *schema.ResourceData, meta in
d.Set("readonly_nodes_weight", flattenGaussDBProxyResponseBodyReadonlyNodesWeight(proxy, d)),
d.Set("new_node_auto_add_status", utils.PathSearch("proxy.new_node_auto_add_status", proxy, nil)),
d.Set("port", utils.PathSearch("proxy.port", proxy, nil)),
d.Set("consistence_mode", utils.PathSearch("proxy.consistence_mode", proxy, nil)),
d.Set("address", utils.PathSearch("proxy.address", proxy, nil)),
d.Set("nodes", flattenGaussDBProxyResponseBodyNodes(proxy)),
)
Expand Down Expand Up @@ -590,6 +604,13 @@ func resourceGaussDBProxyUpdate(ctx context.Context, d *schema.ResourceData, met
}
}

if d.HasChange("consistence_mode") {
err = updateGaussDBMySQLConsistenceMode(ctx, d, client)
if err != nil {
return diag.FromErr(err)
}
}

return resourceGaussDBProxyRead(ctx, d, meta)
}

Expand Down Expand Up @@ -994,6 +1015,56 @@ func buildUpdateGaussDBMySQLTransactionSplitBodyParams(d *schema.ResourceData) m
return bodyParams
}

func updateGaussDBMySQLConsistenceMode(ctx context.Context, d *schema.ResourceData, client *golangsdk.ServiceClient) error {
var (
httpUrl = "v3/{project_id}/instances/{instance_id}/proxy/{proxy_id}/session-consistence"
)
updatePath := client.Endpoint + httpUrl
updatePath = strings.ReplaceAll(updatePath, "{project_id}", client.ProjectID)
updatePath = strings.ReplaceAll(updatePath, "{instance_id}", d.Get("instance_id").(string))
updatePath = strings.ReplaceAll(updatePath, "{proxy_id}", d.Id())

updateOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
updateOpt.JSONBody = buildUpdateGaussDBMySQLConsistenceModeBodyParams(d)

updateResp, err := client.Request("PUT", updatePath, &updateOpt)
if err != nil {
return fmt.Errorf("error updating GaussDB MySQL proxy consistence mode: %s", err)
}

updateRespBody, err := utils.FlattenResponse(updateResp)
if err != nil {
return err
}

jobId := utils.PathSearch("job_id", updateRespBody, nil)
if jobId == nil {
return fmt.Errorf("error updating GaussDB MySQL proxy consistence mode: job_id is not found in API response")
}

err = checkGaussDBMySQLProxyJobFinish(ctx, client, jobId.(string), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}

return nil
}

func buildUpdateGaussDBMySQLConsistenceModeBodyParams(d *schema.ResourceData) map[string]interface{} {
consistenceMode := d.Get("consistence_mode").(string)
bodyParams := map[string]interface{}{
"consistence_mode": consistenceMode,
}
if consistenceMode == "session" {
bodyParams["session_consistence"] = true
} else {
bodyParams["session_consistence"] = false
}
return bodyParams
}

func resourceGaussDBProxyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
Expand Down

0 comments on commit 59bd5bd

Please sign in to comment.