From bbcf0dae59ae6fd26aff0dabbe0afe45993cb229 Mon Sep 17 00:00:00 2001 From: Zippo-Wang <852420284@qq.com> Date: Wed, 11 Oct 2023 15:37:22 +0800 Subject: [PATCH 1/2] feat(AOM): import AOM resource, unit test and document. --- docs/resources/aom_service_discovery_rule.md | 147 +++++++++++ ...eengine_aom_service_discovery_rule_test.go | 228 ++++++++++++++++++ flexibleengine/provider.go | 4 +- 3 files changed, 378 insertions(+), 1 deletion(-) create mode 100644 docs/resources/aom_service_discovery_rule.md create mode 100644 flexibleengine/acceptance/resource_flexibleengine_aom_service_discovery_rule_test.go diff --git a/docs/resources/aom_service_discovery_rule.md b/docs/resources/aom_service_discovery_rule.md new file mode 100644 index 00000000..bdfd7903 --- /dev/null +++ b/docs/resources/aom_service_discovery_rule.md @@ -0,0 +1,147 @@ +--- +subcategory: "Application Operations Management (AOM)" +--- + +# flexibleengine_aom_service_discovery_rule + +Manages an AOM service discovery rule resource within FlexibleEngineCloud. + +## Example Usage + +### Basic example + +```hcl +resource "flexibleengine_aom_service_discovery_rule" "discovery_rule" { + name = "test-rule" + priority = 9999 + detect_log_enabled = "true" + discovery_rule_enabled = "true" + is_default_rule = "false" + log_file_suffix = ["log"] + service_type = "Java" + + discovery_rules { + check_content = ["java"] + check_mode = "contain" + check_type = "cmdLine" + } + + log_path_rules { + name_type = "cmdLineHash" + args = ["java"] + value = ["/tmp/log"] + } + + name_rules { + service_name_rule { + name_type = "str" + args = ["java"] + } + application_name_rule { + name_type = "str" + args = ["java"] + } + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `region` - (Optional, String, ForceNew) The region in which to create the service discovery rule resource. If omitted, + the provider-level region will be used. Changing this creates a new resource. + +* `name` - (Required, String) Specifies the rule name, which contains 4 to 63 characters. It must start + with a lowercase letter but cannot end with a hyphen (-). Only digits, lowercase letters, and hyphens are allowed. + +* `service_type` - (Required, String) Specifies the service type, which is used only for rule classification and UI display. + You can enter any field. For example, enter Java or Python by technology stack, or enter collector or database by function. + +* `discovery_rules` - (Required, List) Specifies the discovery rule. If the array contains multiple conditions, only the + processes that meet all the conditions will be matched. If the value of `check_type` is **cmdLine**, set the value of + `check_mode` to **contain**. `check_content` is in the format of ["xxx"], indicating that the process must contain + the xxx parameter. If the value of `check_type` is **env**, set the value of `check_mode` to **contain**. + `check_content` is in the format of ["k1","v1"], indicating that the process must contain the environment variable + whose name is k1 and value is v1. If the value of `check_type` is **scope**, set the value of `check_mode` + to **equals**. `check_content` is in the format of ["hostId1","hostId2"], indicating that the rule takes effect only + on specified nodes. If no nodes are specified, the rule applies to all nodes of the project. + + `check_type` - (Required, String) Specifies the match type. The values can be **cmdLine**, **env** and **scope**. + + `check_mode` - (Required, String) Specifies the match condition. The values can be **contain** and **equals**. + + `check_content` - (Required, List) Specifies the matched value. This is a list of strings. + +* `name_rules` - (Required, List) Specifies the naming rules for discovered services and applications. + The [object](#name_rules_object) structure is documented below. + +* `log_file_suffix` - (Required, List) Specifies the log file suffix. This is a list of strings. + The values can be: **log**, **trace**, and **out**. + +* `detect_log_enabled` - (Optional, Bool) Specifies whether to enable log collection. The default value is true. + +* `priority` - (Optional, Int) Specifies the rule priority. Value range: 1 to 9999. The default value is 9999. + +* `discovery_rule_enabled` - (Optional, Bool) Specifies whether the rule is enabled. The default value is true. + +* `is_default_rule` - (Optional, Bool) Specifies whether the rule is the default one. The default value is false. + +* `log_path_rules` - (Optional, List) Specifies the log path configuration rule. If cmdLineHash is a fixed string, + logs in the specified log path or log file are collected. Otherwise, only the files whose names end with + .log or .trace are collected. If the value of `name_type` is **cmdLineHash**, args is in the format of ["00001"] and + value is in the format of ["/xxx/xx.log"], indicating that the log path is /xxx/xx.log when the startup command is 00001. + + `name_type` - (Required, String) Specifies the value type, which can be **cmdLineHash**. + + `args` - (Required, List) Specifies the command. This is a list of strings. + + `value` - (Required, List) Specifies the log path. This is a list of strings. + + +The `name_rules` block supports: + +* `service_name_rule` - (Required, List) Specifies the service name rule. If there are multiple objects in the array, + the character strings extracted from these objects constitute the service name. If the value of `name_type` is + **cmdLine**, `args` is in the format of ["start", "end"], indicating that the characters between start and end + in the command are extracted. If the value of `name_type` is **env**, `args` is in the format of ["aa"], + indicating that the environment variable named aa is extracted. If the value of `name_type` is **str**, `args` is in the + format of ["fix"], indicating that the service name is suffixed with fix. If the value of `name_type` is + **cmdLineHash**, `args` is in the format of ["0001"] and `value` is in the format of ["ser"], indicating that the + service name is ser when the startup command is 0001. The [object](#basic_name_rule_object) structure is + documented below. + +* `application_name_rule` - (Required, List) Specifies the application name rule. If the value of `name_type` is + **cmdLine**, `args` is in the format of ["start", "end"], indicating that the characters between start and end in + the command are extracted. If the value of `name_type` is **env**, `args` is in the format of ["aa"], indicating that + the environment variable named aa is extracted. If the value of `name_type` is **str**, `args` is in the format of + ["fix"], indicating that the application name is suffixed with fix. If the value of `name_type` is **cmdLineHash**, + `args` is in the format of ["0001"] and `value` is in the format of ["ser"], indicating that the application name is + ser when the startup command is 0001. The [object](#basic_name_rule_object) structure is documented below. + + +The `service_name_rule` block and `application_name_rule` block support: + +* `name_type` - (Required, String) Specifies the value type. The value can be **cmdLineHash**, **cmdLine**, **env** +and **str**. +* `args` - (Required, List) Specifies the input value. +* `value` - (Optional, List) Specifies the application name, which is mandatory only when the value of +`name_type` is **cmdLineHash**. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - Indicates the resource ID of the service discovery rule. The value is the rule name. + +* `rule_id` - The rule ID in uuid format. + +## Timeouts + +This resource provides the following timeouts configuration options: + +* `create` - Default is 5 minutes. +* `update` - Default is 5 minutes. +* `delete` - Default is 5 minutes. + +## Import + +AOM service discovery rules can be imported using the `name`, e.g. + +```shell +terraform import flexibleengine_aom_service_discovery_rule.alarm_rule rule_name +``` diff --git a/flexibleengine/acceptance/resource_flexibleengine_aom_service_discovery_rule_test.go b/flexibleengine/acceptance/resource_flexibleengine_aom_service_discovery_rule_test.go new file mode 100644 index 00000000..435af0f5 --- /dev/null +++ b/flexibleengine/acceptance/resource_flexibleengine_aom_service_discovery_rule_test.go @@ -0,0 +1,228 @@ +package acceptance + +import ( + "fmt" + "testing" + "time" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" + aomservice "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/aom" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + + aom "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2/model" +) + +func getServiceDiscoveryRuleResourceFunc(conf *config.Config, state *terraform.ResourceState) (interface{}, error) { + c, err := conf.HcAomV2Client(OS_REGION_NAME) + if err != nil { + return nil, fmt.Errorf("error creating AOM client: %s", err) + } + + // wait 30 seconds before listing the rule, to avoid error + // lintignore:R018 + time.Sleep(30 * time.Second) + + response, err := c.ListServiceDiscoveryRules(&aom.ListServiceDiscoveryRulesRequest{}) + if err != nil { + return nil, fmt.Errorf("error retrieving AOM service discovery rule: %s", state.Primary.ID) + } + + allRules := *response.AppRules + + return aomservice.FilterRules(allRules, state.Primary.ID) +} + +func TestAccAOMServiceDiscoveryRule_basic(t *testing.T) { + var ar aom.QueryAlarmResult + rName := acceptance.RandomAccResourceNameWithDash() + rNameUpdate := rName + "-update" + resourceName := "flexibleengine_aom_service_discovery_rule.test" + + rc := acceptance.InitResourceCheck( + resourceName, + &ar, + getServiceDiscoveryRuleResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAOMServiceDiscoveryRule_basic(rName), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "priority", "9999"), + resource.TestCheckResourceAttr(resourceName, "detect_log_enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "discovery_rule_enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "is_default_rule", "true"), + resource.TestCheckResourceAttr(resourceName, "log_file_suffix.0", "log"), + resource.TestCheckResourceAttr(resourceName, "service_type", "Python"), + resource.TestCheckResourceAttr(resourceName, "discovery_rules.0.check_content.0", "python"), + resource.TestCheckResourceAttr(resourceName, "log_path_rules.0.args.0", "python"), + resource.TestCheckResourceAttr( + resourceName, "name_rules.0.service_name_rule.0.args.0", "python"), + resource.TestCheckResourceAttr( + resourceName, "name_rules.0.application_name_rule.0.args.0", "python"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAOMServiceDiscoveryRule_update(rNameUpdate), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "name", rNameUpdate), + resource.TestCheckResourceAttr(resourceName, "priority", "9998"), + resource.TestCheckResourceAttr(resourceName, "detect_log_enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "discovery_rule_enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "is_default_rule", "false"), + resource.TestCheckResourceAttr(resourceName, "log_file_suffix.0", "trace"), + resource.TestCheckResourceAttr(resourceName, "service_type", "Java"), + resource.TestCheckResourceAttr(resourceName, "discovery_rules.0.check_content.0", "java"), + resource.TestCheckResourceAttr(resourceName, "log_path_rules.0.args.0", "java"), + resource.TestCheckResourceAttr( + resourceName, "name_rules.0.service_name_rule.0.args.0", "java"), + resource.TestCheckResourceAttr( + resourceName, "name_rules.0.application_name_rule.0.args.0", "java"), + ), + }, + { + Config: testAOMServiceDiscoveryRule_update2(rNameUpdate), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "name", rNameUpdate), + resource.TestCheckResourceAttr(resourceName, "priority", "9998"), + resource.TestCheckResourceAttr(resourceName, "detect_log_enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "discovery_rule_enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "is_default_rule", "false"), + resource.TestCheckResourceAttr(resourceName, "log_file_suffix.0", "out"), + resource.TestCheckResourceAttr(resourceName, "service_type", "Java"), + resource.TestCheckResourceAttr(resourceName, "discovery_rules.0.check_content.0", "java"), + resource.TestCheckResourceAttr(resourceName, "log_path_rules.0.args.0", "java"), + resource.TestCheckResourceAttr( + resourceName, "name_rules.0.service_name_rule.0.args.0", "java"), + resource.TestCheckResourceAttr( + resourceName, "name_rules.0.application_name_rule.0.args.0", "java"), + ), + }, + }, + }) +} + +func testAOMServiceDiscoveryRule_basic(rName string) string { + return fmt.Sprintf(` +resource "flexibleengine_aom_service_discovery_rule" "test" { + name = "%s" + priority = 9999 + detect_log_enabled = true + discovery_rule_enabled = true + is_default_rule = true + log_file_suffix = ["log"] + service_type = "Python" + + discovery_rules { + check_content = ["python"] + check_mode = "contain" + check_type = "cmdLine" + } + + log_path_rules { + name_type = "cmdLineHash" + args = ["python"] + value = ["/tmp/log"] + } + + name_rules { + service_name_rule { + name_type = "str" + args = ["python"] + } + application_name_rule { + name_type = "str" + args = ["python"] + } + } +} +`, rName) +} + +func testAOMServiceDiscoveryRule_update(rName string) string { + return fmt.Sprintf(` +resource "flexibleengine_aom_service_discovery_rule" "test" { + name = "%s" + priority = 9998 + detect_log_enabled = false + discovery_rule_enabled = false + is_default_rule = false + log_file_suffix = ["trace"] + service_type = "Java" + + discovery_rules { + check_content = ["java"] + check_mode = "contain" + check_type = "cmdLine" + } + + log_path_rules { + name_type = "cmdLineHash" + args = ["java"] + value = ["/tmp/log"] + } + + name_rules { + service_name_rule { + name_type = "str" + args = ["java"] + } + application_name_rule { + name_type = "str" + args = ["java"] + } + } +} +`, rName) +} + +func testAOMServiceDiscoveryRule_update2(rName string) string { + return fmt.Sprintf(` +resource "flexibleengine_aom_service_discovery_rule" "test" { + name = "%s" + priority = 9998 + detect_log_enabled = false + discovery_rule_enabled = false + is_default_rule = false + log_file_suffix = ["out"] + service_type = "Java" + + discovery_rules { + check_content = ["java"] + check_mode = "contain" + check_type = "cmdLine" + } + + log_path_rules { + name_type = "cmdLineHash" + args = ["java"] + value = ["/tmp/log"] + } + + name_rules { + service_name_rule { + name_type = "str" + args = ["java"] + } + application_name_rule { + name_type = "str" + args = ["java"] + } + } +} +`, rName) +} diff --git a/flexibleengine/provider.go b/flexibleengine/provider.go index 989b7a00..759977b2 100644 --- a/flexibleengine/provider.go +++ b/flexibleengine/provider.go @@ -9,10 +9,10 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/mutexkv" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/aom" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/apig" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/as" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cbr" @@ -449,6 +449,8 @@ func Provider() *schema.Provider { "flexibleengine_dli_queue": ResourceDliQueueV1(), // importing new resource + "flexibleengine_aom_service_discovery_rule": aom.ResourceServiceDiscoveryRule(), + "flexibleengine_apig_api": apig.ResourceApigAPIV2(), "flexibleengine_apig_api_publishment": apig.ResourceApigApiPublishment(), "flexibleengine_apig_instance": apig.ResourceApigInstanceV2(), From 8153da48a859cc3f86c8d86987932115a179af4f Mon Sep 17 00:00:00 2001 From: shichangkuo Date: Wed, 1 Nov 2023 14:44:03 +0800 Subject: [PATCH 2/2] Update docs/resources/aom_service_discovery_rule.md --- docs/resources/aom_service_discovery_rule.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/resources/aom_service_discovery_rule.md b/docs/resources/aom_service_discovery_rule.md index bdfd7903..0e9d3d93 100644 --- a/docs/resources/aom_service_discovery_rule.md +++ b/docs/resources/aom_service_discovery_rule.md @@ -4,7 +4,7 @@ subcategory: "Application Operations Management (AOM)" # flexibleengine_aom_service_discovery_rule -Manages an AOM service discovery rule resource within FlexibleEngineCloud. +Manages an AOM service discovery rule resource within FlexibleEngine. ## Example Usage