-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GaussDBforMySQL): add gaussdb mysql lts log resource (#5756)
- Loading branch information
Showing
4 changed files
with
516 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
subcategory: "GaussDB(for MySQL)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_gaussdb_mysql_lts_log" | ||
description: |- | ||
Manages a GaussDB MySQL LTS log resource within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_gaussdb_mysql_lts_log | ||
|
||
Manages a GaussDB MySQL LTS log resource within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "instance_id" {} | ||
variable "lts_group_id" {} | ||
variable "lts_stream_id" {} | ||
resource "huaweicloud_gaussdb_mysql_lts_log" "test" { | ||
instance_id = var.instance_id | ||
log_type = "error_log" | ||
lts_group_id = var.lts_group_id | ||
lts_stream_id = var.lts_stream_id | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource. | ||
If omitted, the provider-level region will be used. Changing this creates a new resource. | ||
|
||
* `instance_id` - (Required, String, ForceNew) Specifies the ID of the GaussDB MySQL instance. | ||
Changing this creates a new resource. | ||
|
||
* `log_type` - (Required, String, ForceNew) Specifies the type of the LTS log. | ||
Value options: **error_log**, **slow_log**. Changing this creates a new resource. | ||
|
||
* `lts_group_id` - (Required, String) Specifies the ID of the LTS log group. | ||
|
||
* `lts_stream_id` - (Required, String) Specifies the ID of the LTS log stream. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attribute is exported: | ||
|
||
* `id` - The resource ID in format of `<instance_id>/<log_type>`. | ||
|
||
## Timeouts | ||
|
||
This resource provides the following timeouts configuration options: | ||
|
||
* `create` - Default is 30 minutes. | ||
* `delete` - Default is 30 minutes. | ||
|
||
## Import | ||
|
||
The GaussDB MySQL LTS log can be imported using `instance_id` and `log_type` separated by a slash, e.g. | ||
|
||
```bash | ||
$ terraform import huaweicloud_gaussdb_mysql_lts_log.test <instance_id>/<log_type> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_lts_log_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
package gaussdb | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
||
"github.com/chnsz/golangsdk" | ||
"github.com/chnsz/golangsdk/pagination" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
func getGaussDBMysqlLtsLogResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { | ||
var ( | ||
httpUrl = "v3/{project_id}/logs/lts-configs" | ||
product = "gaussdb" | ||
) | ||
|
||
client, err := cfg.NewServiceClient(product, acceptance.HW_REGION_NAME) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating GaussDB client: %s", err) | ||
} | ||
|
||
listPath := client.Endpoint + httpUrl | ||
listPath = strings.ReplaceAll(listPath, "{project_id}", client.ProjectID) | ||
listPath += fmt.Sprintf("?instance_id=%s", state.Primary.Attributes["instance_id"]) | ||
|
||
listResp, err := pagination.ListAllItems( | ||
client, | ||
"offset", | ||
listPath, | ||
&pagination.QueryOpts{MarkerField: ""}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
listRespJson, err := json.Marshal(listResp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var listRespBody interface{} | ||
err = json.Unmarshal(listRespJson, &listRespBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
searchPath := fmt.Sprintf("instance_lts_configs|[0].lts_configs|[?log_type=='%s' && enabled]|[0]", | ||
state.Primary.Attributes["log_type"]) | ||
ltsConfig := utils.PathSearch(searchPath, listRespBody, nil) | ||
if ltsConfig == nil { | ||
return nil, golangsdk.ErrDefault404{} | ||
} | ||
|
||
return ltsConfig, nil | ||
} | ||
|
||
func TestAccGaussDBMysqlLtsLog_basic(t *testing.T) { | ||
var obj interface{} | ||
rName := acceptance.RandomAccResourceName() | ||
resourceName := "huaweicloud_gaussdb_mysql_lts_log.test" | ||
|
||
rc := acceptance.InitResourceCheck( | ||
resourceName, | ||
&obj, | ||
getGaussDBMysqlLtsLogResourceFunc, | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.TestAccPreCheck(t) }, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: rc.CheckResourceDestroy(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccGaussDBMysqlLtsLog_basic(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrPair(resourceName, "instance_id", | ||
"huaweicloud_gaussdb_mysql_instance.test", "id"), | ||
resource.TestCheckResourceAttrPair(resourceName, "lts_group_id", | ||
"huaweicloud_lts_group.test.0", "id"), | ||
resource.TestCheckResourceAttrPair(resourceName, "lts_stream_id", | ||
"huaweicloud_lts_stream.test.0", "id"), | ||
resource.TestCheckResourceAttr(resourceName, "log_type", "error_log"), | ||
), | ||
}, | ||
{ | ||
Config: testAccGaussDBMysqlLtsLog_update(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrPair(resourceName, "instance_id", | ||
"huaweicloud_gaussdb_mysql_instance.test", "id"), | ||
resource.TestCheckResourceAttrPair(resourceName, "lts_group_id", | ||
"huaweicloud_lts_group.test.1", "id"), | ||
resource.TestCheckResourceAttrPair(resourceName, "lts_stream_id", | ||
"huaweicloud_lts_stream.test.1", "id"), | ||
resource.TestCheckResourceAttr(resourceName, "log_type", "error_log"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateIdFunc: testAccGaussDBMysqlLtsLogImportStateIdFunc(resourceName), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccGaussDBMysqlLtsLog_base(rName string) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
data "huaweicloud_availability_zones" "test" {} | ||
data "huaweicloud_gaussdb_mysql_flavors" "test" { | ||
engine = "gaussdb-mysql" | ||
version = "8.0" | ||
} | ||
resource "huaweicloud_gaussdb_mysql_instance" "test" { | ||
name = "%[2]s" | ||
password = "Test@12345678" | ||
flavor = data.huaweicloud_gaussdb_mysql_flavors.test.flavors[0].name | ||
vpc_id = huaweicloud_vpc.test.id | ||
subnet_id = huaweicloud_vpc_subnet.test.id | ||
security_group_id = huaweicloud_networking_secgroup.test.id | ||
availability_zone_mode = "multi" | ||
master_availability_zone = data.huaweicloud_availability_zones.test.names[0] | ||
read_replicas = 2 | ||
enterprise_project_id = "0" | ||
} | ||
resource "huaweicloud_lts_group" "test" { | ||
count = 2 | ||
group_name = "%[2]s_${count.index}" | ||
ttl_in_days = 1 | ||
} | ||
resource "huaweicloud_lts_stream" "test" { | ||
count = 2 | ||
group_id = huaweicloud_lts_group.test[count.index].id | ||
stream_name = "%[2]s_${count.index}" | ||
} | ||
`, common.TestBaseNetwork(rName), rName) | ||
} | ||
|
||
func testAccGaussDBMysqlLtsLog_basic(rName string) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
resource "huaweicloud_gaussdb_mysql_lts_log" "test" { | ||
instance_id = huaweicloud_gaussdb_mysql_instance.test.id | ||
log_type = "error_log" | ||
lts_group_id = huaweicloud_lts_group.test[0].id | ||
lts_stream_id = huaweicloud_lts_stream.test[0].id | ||
}`, testAccGaussDBMysqlLtsLog_base(rName), rName) | ||
} | ||
|
||
func testAccGaussDBMysqlLtsLog_update(rName string) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
resource "huaweicloud_gaussdb_mysql_lts_log" "test" { | ||
instance_id = huaweicloud_gaussdb_mysql_instance.test.id | ||
log_type = "error_log" | ||
lts_group_id = huaweicloud_lts_group.test[1].id | ||
lts_stream_id = huaweicloud_lts_stream.test[1].id | ||
}`, testAccGaussDBMysqlLtsLog_base(rName), rName) | ||
} | ||
|
||
func testAccGaussDBMysqlLtsLogImportStateIdFunc(name string) resource.ImportStateIdFunc { | ||
return func(s *terraform.State) (string, error) { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return "", fmt.Errorf("the resource (%s) not found: %s", name, rs) | ||
} | ||
if rs.Primary.Attributes["instance_id"] == "" { | ||
return "", fmt.Errorf("attribute (instance_id) of resource(%s) not found: %s", name, rs) | ||
} | ||
if rs.Primary.Attributes["log_type"] == "" { | ||
return "", fmt.Errorf("attribute (log_type) of Resource (%s) not found: %s", name, rs) | ||
} | ||
return fmt.Sprintf("%s/%s", rs.Primary.Attributes["instance_id"], rs.Primary.Attributes["log_type"]), nil | ||
} | ||
} |
Oops, something went wrong.