Skip to content

Commit

Permalink
feat(GaussDB): guassdb mysql proxy support connection pool type (#5266)
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 authored Jul 24, 2024
1 parent 938ec83 commit 2b8f6e8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/resources/gaussdb_mysql_proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ The following arguments are supported:

Defaults to **eventual**.

* `connection_pool_type` - (Optional, String) Specifies the connection pool type. Value options:
+ **CLOSED**: The connection pool is not used.
+ **SESSION**: The session-level connection pool is used.

Defaults to **CLOSED**.

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

Expand Down Expand Up @@ -117,6 +123,8 @@ In addition to all arguments above, the following attributes are exported:

* `address` - Indicates the address of the proxy.

* `switch_connection_pool_type_enabled` - Indicates whether the proxy supports session-level connection pool.

* `nodes` - Indicates the node information of the proxy.
The [nodes](#nodes_struct) structure is documented below.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func TestAccGaussDBMySQLProxy_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "new_node_auto_add_status", "OFF"),
resource.TestCheckResourceAttr(resourceName, "port", "3339"),
resource.TestCheckResourceAttr(resourceName, "transaction_split", "ON"),
resource.TestCheckResourceAttr(resourceName, "connection_pool_type", "SESSION"),
resource.TestCheckResourceAttr(resourceName, "switch_connection_pool_type_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "master_node_weight.#", "1"),
resource.TestCheckResourceAttr(resourceName, "readonly_nodes_weight.#", "1"),
resource.TestCheckResourceAttr(resourceName, "parameters.0.name", "multiStatementType"),
Expand Down Expand Up @@ -131,6 +133,8 @@ func TestAccGaussDBMySQLProxy_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "new_node_weight", "20"),
resource.TestCheckResourceAttr(resourceName, "port", "3338"),
resource.TestCheckResourceAttr(resourceName, "transaction_split", "OFF"),
resource.TestCheckResourceAttr(resourceName, "connection_pool_type", "CLOSED"),
resource.TestCheckResourceAttr(resourceName, "switch_connection_pool_type_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "master_node_weight.#", "1"),
resource.TestCheckResourceAttr(resourceName, "readonly_nodes_weight.#", "2"),
resource.TestCheckResourceAttr(resourceName, "parameters.0.name", "looseImciApThreshold"),
Expand Down Expand Up @@ -214,6 +218,7 @@ resource "huaweicloud_gaussdb_mysql_proxy" "test" {
port = 3339
transaction_split = "ON"
consistence_mode = "session"
connection_pool_type = "SESSION"
master_node_weight {
id = local.sort_nodes[0].id
Expand Down Expand Up @@ -251,6 +256,7 @@ resource "huaweicloud_gaussdb_mysql_proxy" "test" {
port = 3338
transaction_split = "OFF"
consistence_mode = "eventual"
connection_pool_type = "CLOSED"
master_node_weight {
id = local.sort_nodes[0].id
Expand Down Expand Up @@ -293,6 +299,7 @@ resource "huaweicloud_gaussdb_mysql_proxy" "test" {
port = 3338
transaction_split = "OFF"
consistence_mode = "eventual"
connection_pool_type = "CLOSED"
master_node_weight {
id = local.sort_nodes[0].id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
// @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 PUT /v3/{project_id}/instances/{instance_id}/proxy/{proxy_id}/connection-pool-type
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/proxies
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/proxy/{proxy_id}/{engine_name}/proxy-version
// @API GaussDBforMySQL GET /v3/{project_id}/instances/{instance_id}/proxy/{proxy_id}/configurations
Expand Down Expand Up @@ -155,10 +156,19 @@ func ResourceGaussDBProxy() *schema.Resource {
Optional: true,
Computed: true,
},
"connection_pool_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"address": {
Type: schema.TypeString,
Computed: true,
},
"switch_connection_pool_type_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"nodes": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -300,6 +310,13 @@ func resourceGaussDBProxyCreate(ctx context.Context, d *schema.ResourceData, met
}
}

if connectionPoolType, ok := d.GetOk("connection_pool_type"); ok && connectionPoolType == "SESSION" {
err = updateGaussDBMySQLConnectionPoolType(ctx, d, client)
if err != nil {
return diag.FromErr(err)
}
}

return resourceGaussDBProxyRead(ctx, d, meta)
}

Expand Down Expand Up @@ -372,6 +389,9 @@ func resourceGaussDBProxyRead(_ context.Context, d *schema.ResourceData, meta in
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("connection_pool_type", utils.PathSearch("proxy.connection_pool_type", proxy, nil)),
d.Set("switch_connection_pool_type_enabled", utils.PathSearch("proxy.switch_connection_pool_type_enabled",
proxy, nil)),
d.Set("address", utils.PathSearch("proxy.address", proxy, nil)),
d.Set("nodes", flattenGaussDBProxyResponseBodyNodes(proxy)),
)
Expand Down Expand Up @@ -653,6 +673,13 @@ func resourceGaussDBProxyUpdate(ctx context.Context, d *schema.ResourceData, met
}
}

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

return resourceGaussDBProxyRead(ctx, d, meta)
}

Expand Down Expand Up @@ -1107,6 +1134,50 @@ func buildUpdateGaussDBMySQLConsistenceModeBodyParams(d *schema.ResourceData) ma
return bodyParams
}

func updateGaussDBMySQLConnectionPoolType(ctx context.Context, d *schema.ResourceData, client *golangsdk.ServiceClient) error {
var (
httpUrl = "v3/{project_id}/instances/{instance_id}/proxy/{proxy_id}/connection-pool-type"
)
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 = utils.RemoveNil(buildUpdateGaussDBMySQLConnectionPoolTypeBodyParams(d))

updateResp, err := client.Request("PUT", updatePath, &updateOpt)
if err != nil {
return fmt.Errorf("error updating GaussDB MySQL proxy connection pool type: %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 connection pool type: 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 buildUpdateGaussDBMySQLConnectionPoolTypeBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"connection_pool_type": d.Get("connection_pool_type"),
}
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 2b8f6e8

Please sign in to comment.