From 1fa6b5af4ccddabfa6cfbf2b69a4a5e80770781a Mon Sep 17 00:00:00 2001 From: houpeng80 <114376095+houpeng80@users.noreply.github.com> Date: Wed, 30 Oct 2024 10:04:38 +0800 Subject: [PATCH] feat(GaussDBforMySQL): gaussdb mysql instance support slow log show original switch (#5766) --- docs/resources/gaussdb_mysql_instance.md | 2 + go.mod | 2 +- go.sum | 4 +- ...huaweicloud_gaussdb_mysql_instance_test.go | 6 +++ ...urce_huaweicloud_gaussdb_mysql_instance.go | 43 +++++++++++++++++++ .../taurusdb/v3/instances/requests.go | 40 +++++++++++++++++ .../taurusdb/v3/instances/results.go | 28 ++++++++++++ .../openstack/taurusdb/v3/instances/urls.go | 4 ++ vendor/modules.txt | 2 +- 9 files changed, 127 insertions(+), 4 deletions(-) diff --git a/docs/resources/gaussdb_mysql_instance.md b/docs/resources/gaussdb_mysql_instance.md index e6da988943..118ef0c88d 100644 --- a/docs/resources/gaussdb_mysql_instance.md +++ b/docs/resources/gaussdb_mysql_instance.md @@ -82,6 +82,8 @@ The following arguments are supported: Defaults to **true**. +* `slow_log_show_original_switch` - (Optional, Bool) Specifies the slow log show original switch of the instance. + * `description` - (Optional, String) Specifies the description of the instance. * `private_write_ip` - (Optional, String) Specifies the private IP address of the DB instance. diff --git a/go.mod b/go.mod index ee49b0c853..ce6020b3e1 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/GehirnInc/crypt v0.0.0-20200316065508-bb7000b8a962 - github.com/chnsz/golangsdk v0.0.0-20241024112204-a96c8484ffa3 + github.com/chnsz/golangsdk v0.0.0-20241026031406-eeb6712069c7 github.com/hashicorp/go-cleanhttp v0.5.2 github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-uuid v1.0.3 diff --git a/go.sum b/go.sum index 8b14533e9a..1d1334c66a 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,8 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6 github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/chnsz/golangsdk v0.0.0-20241024112204-a96c8484ffa3 h1:afaYCtfr7XhRkJQd2UupmTst+/ProhdVdp6tE616mdg= -github.com/chnsz/golangsdk v0.0.0-20241024112204-a96c8484ffa3/go.mod h1:Erm4hDWxXgAdbkG3+hhJFgRzEL1TvvcroWzw2Gax4uI= +github.com/chnsz/golangsdk v0.0.0-20241026031406-eeb6712069c7 h1:pjlmJMw0DC5orJnnPxHooMQpMCuKxRFn0ZpiC1EdrBI= +github.com/chnsz/golangsdk v0.0.0-20241026031406-eeb6712069c7/go.mod h1:Erm4hDWxXgAdbkG3+hhJFgRzEL1TvvcroWzw2Gax4uI= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= diff --git a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_instance_test.go b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_instance_test.go index 6e8002cdb1..66851234ee 100644 --- a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_instance_test.go +++ b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_instance_test.go @@ -57,6 +57,7 @@ func TestAccGaussDBInstance_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "maintain_end", "11:00"), resource.TestCheckResourceAttr(resourceName, "seconds_level_monitoring_enabled", "true"), resource.TestCheckResourceAttr(resourceName, "seconds_level_monitoring_period", "1"), + resource.TestCheckResourceAttr(resourceName, "slow_log_show_original_switch", "true"), resource.TestCheckResourceAttr(resourceName, "description", "test_description"), resource.TestCheckResourceAttr(resourceName, "encryption_status", "ON"), resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar"), @@ -111,6 +112,7 @@ func TestAccGaussDBInstance_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "maintain_begin", "14:00"), resource.TestCheckResourceAttr(resourceName, "maintain_end", "18:00"), resource.TestCheckResourceAttr(resourceName, "seconds_level_monitoring_enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "slow_log_show_original_switch", "false"), resource.TestCheckResourceAttr(resourceName, "description", ""), resource.TestCheckResourceAttr(resourceName, "encryption_status", "OFF"), resource.TestCheckResourceAttr(resourceName, "tags.foo_update", "bar"), @@ -325,6 +327,8 @@ resource "huaweicloud_gaussdb_mysql_instance" "test" { seconds_level_monitoring_enabled = true seconds_level_monitoring_period = 1 + slow_log_show_original_switch = true + encryption_status = "ON" encryption_type = "kms" kms_key_id = huaweicloud_kms_key.test.id @@ -392,6 +396,8 @@ resource "huaweicloud_gaussdb_mysql_instance" "test" { seconds_level_monitoring_enabled = false + slow_log_show_original_switch = false + encryption_status = "OFF" parameters { diff --git a/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_instance.go b/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_instance.go index 02ffc47096..ac41078c04 100644 --- a/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_instance.go +++ b/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_instance.go @@ -235,6 +235,11 @@ func ResourceGaussDBInstance() *schema.Resource { Optional: true, ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false), }, + "slow_log_show_original_switch": { + Type: schema.TypeBool, + Optional: true, + Computed: true, + }, "description": { Type: schema.TypeString, Optional: true, @@ -819,6 +824,12 @@ func resourceGaussDBInstanceCreate(ctx context.Context, d *schema.ResourceData, } } + if _, ok := d.GetOk("slow_log_show_original_switch"); ok { + if err = updateSlowLogShowOriginalSwitch(client, d); err != nil { + return diag.FromErr(err) + } + } + if _, ok := d.GetOk("description"); ok { if err = updateDescription(client, d); err != nil { return diag.FromErr(err) @@ -980,6 +991,8 @@ func resourceGaussDBInstanceRead(ctx context.Context, d *schema.ResourceData, me mErr = multierror.Append(mErr, setEncryption(d, client, instanceID)) // set version mErr = multierror.Append(mErr, setVersion(d, client, instanceID)...) + // set slow log show original + mErr = multierror.Append(mErr, setSlowLogShowOriginalSwitch(d, client, instanceID)) // save tags if resourceTags, err := tags.Get(client, "instances", d.Id()).Extract(); err == nil { @@ -1197,6 +1210,16 @@ func setVersion(d *schema.ResourceData, client *golangsdk.ServiceClient, instanc return errs } +func setSlowLogShowOriginalSwitch(d *schema.ResourceData, client *golangsdk.ServiceClient, instanceId string) error { + resp, err := instances.GetSlowLogShowOriginalSwitch(client, instanceId).Extract() + if err != nil { + log.Printf("[WARN] query instance %s slow log show original failed: %s", instanceId, err) + return nil + } + slowLogShowOriginalSwitch, _ := strconv.ParseBool(resp.OpenSlowLogSwitch) + return d.Set("slow_log_show_original_switch", slowLogShowOriginalSwitch) +} + func setGaussDBMySQLParameters(ctx context.Context, d *schema.ResourceData, client *golangsdk.ServiceClient) diag.Diagnostics { parametersList, err := parameters.List(client, d.Id()) if err != nil { @@ -1406,6 +1429,13 @@ func resourceGaussDBInstanceUpdate(ctx context.Context, d *schema.ResourceData, } } + if d.HasChange("slow_log_show_original_switch") { + err = updateSlowLogShowOriginalSwitch(client, d) + if err != nil { + return diag.FromErr(err) + } + } + if d.HasChange("description") { err = updateDescription(client, d) if err != nil { @@ -2280,6 +2310,19 @@ func updateSslOption(ctx context.Context, client *golangsdk.ServiceClient, d *sc return checkGaussDBMySQLJobFinish(ctx, client, job.JobID, d.Timeout(timeout)) } +func updateSlowLogShowOriginalSwitch(client *golangsdk.ServiceClient, d *schema.ResourceData) error { + opts := instances.UpdateSlowLogShowOriginalSwitchOpts{ + OpenSlowLogSwitch: d.Get("slow_log_show_original_switch").(bool), + } + + _, err := instances.UpdateSlowLogShowOriginalSwitch(client, d.Id(), opts).ExtractUpdateSlowLogShowOriginalSwitchResponse() + if err != nil { + return fmt.Errorf("error updating slow low show original switch for instance %s: %s ", d.Id(), err) + } + + return nil +} + func updateDescription(client *golangsdk.ServiceClient, d *schema.ResourceData) error { updateAliasOpts := instances.UpdateAliasOpts{ Alias: d.Get("description").(string), diff --git a/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/requests.go b/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/requests.go index adc1ba3da4..73e6ae14d9 100644 --- a/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/requests.go +++ b/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/requests.go @@ -759,3 +759,43 @@ func GetVersion(client *golangsdk.ServiceClient, instanceId string) (r GetVersio return } + +type UpdateSlowLogShowOriginalSwitchOpts struct { + OpenSlowLogSwitch bool `json:"open_slow_log_switch"` +} + +type UpdateSlowLogShowOriginalSwitchBuilder interface { + ToSlowLogShowOriginalSwitchUpdateMap() (map[string]interface{}, error) +} + +func (opts UpdateSlowLogShowOriginalSwitchOpts) ToSlowLogShowOriginalSwitchUpdateMap() (map[string]interface{}, error) { + b, err := golangsdk.BuildRequestBody(opts, "") + if err != nil { + return nil, err + } + return b, nil +} + +func UpdateSlowLogShowOriginalSwitch(client *golangsdk.ServiceClient, instanceId string, + opts UpdateSlowLogShowOriginalSwitchBuilder) (r UpdateSlowLogShowOriginalSwitchResult) { + b, err := opts.ToSlowLogShowOriginalSwitchUpdateMap() + if err != nil { + r.Err = err + return + } + + _, r.Err = client.Post(updateURL(client, instanceId, "slowlog/modify"), b, &r.Body, &golangsdk.RequestOpts{ + MoreHeaders: requestOpts.MoreHeaders, + }) + return +} + +func GetSlowLogShowOriginalSwitch(client *golangsdk.ServiceClient, instanceId string) (r GetSlowLogShowOriginalSwitchResult) { + url := slowLogShowOriginalSwitchURL(client, instanceId) + + _, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{ + MoreHeaders: requestOpts.MoreHeaders, + }) + + return +} diff --git a/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/results.go b/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/results.go index cb3ada7d3b..0b22ffd719 100644 --- a/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/results.go +++ b/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/results.go @@ -311,3 +311,31 @@ func (r GetVersionResult) Extract() (*Version, error) { err := r.ExtractInto(&version) return &version, err } + +type UpdateSlowLogShowOriginalSwitchResponse struct { + Response string `json:"response"` +} + +type UpdateSlowLogShowOriginalSwitchResult struct { + commonResult +} + +func (r UpdateSlowLogShowOriginalSwitchResult) ExtractUpdateSlowLogShowOriginalSwitchResponse() (*UpdateSlowLogShowOriginalSwitchResponse, error) { + res := new(UpdateSlowLogShowOriginalSwitchResponse) + err := r.ExtractInto(res) + return res, err +} + +type SlowLogShowOriginalSwitch struct { + OpenSlowLogSwitch string `json:"open_slow_log_switch"` +} + +type GetSlowLogShowOriginalSwitchResult struct { + commonResult +} + +func (r GetSlowLogShowOriginalSwitchResult) Extract() (*SlowLogShowOriginalSwitch, error) { + var slowLogShowOriginalSwitch SlowLogShowOriginalSwitch + err := r.ExtractInto(&slowLogShowOriginalSwitch) + return &slowLogShowOriginalSwitch, err +} diff --git a/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/urls.go b/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/urls.go index 1195d814a5..9ff85153ca 100644 --- a/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/urls.go +++ b/vendor/github.com/chnsz/golangsdk/openstack/taurusdb/v3/instances/urls.go @@ -38,6 +38,10 @@ func versionURL(c *golangsdk.ServiceClient, instanceID string) string { return c.ServiceURL("instances", instanceID, "database-version") } +func slowLogShowOriginalSwitchURL(c *golangsdk.ServiceClient, instanceID string) string { + return c.ServiceURL("instances", instanceID, "slowlog/query") +} + func jobURL(sc *golangsdk.ServiceClient) string { return sc.ServiceURL("jobs") } diff --git a/vendor/modules.txt b/vendor/modules.txt index fce3e8df62..c82f1d560f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -13,7 +13,7 @@ github.com/apparentlymart/go-cidr/cidr # github.com/apparentlymart/go-textseg/v13 v13.0.0 ## explicit; go 1.16 github.com/apparentlymart/go-textseg/v13/textseg -# github.com/chnsz/golangsdk v0.0.0-20241024112204-a96c8484ffa3 +# github.com/chnsz/golangsdk v0.0.0-20241026031406-eeb6712069c7 ## explicit; go 1.14 github.com/chnsz/golangsdk github.com/chnsz/golangsdk/auth