-
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(cse): add new data source to query microservice instances
- Loading branch information
1 parent
dc01f55
commit e10d0a6
Showing
4 changed files
with
558 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,116 @@ | ||
--- | ||
subcategory: "Cloud Service Engine (CSE)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_cse_microservice_instances" | ||
description: |- | ||
Use this data source to get the list of the instances under dedicated microservice within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_cse_microservice_instances | ||
|
||
Use this data source to get the list of the instances under dedicated microservice within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "microservice_engine_id" {} // Ensure EIP access is enabled. | ||
variable "microservice_id" {} | ||
variable "admin_user" {} | ||
variable "admin_password" {} | ||
data "huaweicloud_cse_microservice_engines" "test" {} | ||
locals { | ||
filter_engines = [for o in data.huaweicloud_cse_microservice_engines.test.engines : o if o.id == var.microservice_engine_id] | ||
} | ||
data "huaweicloud_cse_microservice_instances" "test" { | ||
auth_address = local.filter_engines[0].service_registry_addresses[0].public | ||
connect_address = local.filter_engines[0].service_registry_addresses[0].public | ||
microservice_id = var.microservice_id | ||
admin_user = var.admin_user | ||
admin_pass = var.admin_password | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `auth_address` - (Required, String) Specifies the address that used to request the access token. | ||
|
||
* `connect_address` - (Required, String) Specifies the address that used to send requests and manage configuration. | ||
|
||
-> We are only support IPv4 addresses yet (for `auth_address` and `connect_address`). | ||
|
||
* `admin_pass` - (Optional, String) Specifies the user password that used to pass the **RBAC** control. | ||
|
||
* `admin_user` - (Optional, String) Specifies the user name that used to pass the **RBAC** control. | ||
The password format must meet the following conditions: | ||
+ Must be `8` to `32` characters long. | ||
+ A password must contain at least one digit, one uppercase letter, one lowercase letter, and one special character | ||
(-~!@#%^*_=+?$&()|<>{}[]). | ||
+ Cannot be the account name or account name spelled backwards. | ||
+ The password can only start with a letter. | ||
|
||
-> Both `admin_user` and `admin_pass` are required if **RBAC** is enabled for the microservice engine. | ||
|
||
* `microservice_id` - (Required, String) Specifies the ID of the dedicated microservice to which the instances belong. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The data source ID. | ||
|
||
* `instances` - The list of the microservice instances. | ||
The [instances](#microservice_instances) structure is documented below. | ||
|
||
<a name="microservice_instances"></a> | ||
The `instances` block supports: | ||
|
||
* `id` - The ID of the microservice instance. | ||
|
||
* `host_name` - The host name of the microservice instance. | ||
|
||
* `endpoints` - The list of the access addresses of the microservice instance. | ||
|
||
* `version` - The version of the microservice instance. | ||
|
||
* `properties` - The extended attributes of the microservice instance, in key/value format. | ||
|
||
* `health_check` - The health check configuration of the microservice instance. | ||
The [health_check](#microservice_instances_health_check) structure is documented below. | ||
|
||
* `data_center` - The data center configuration of the microservice instance. | ||
The [data_center](#microservice_instances_data_center) structure is documented below. | ||
|
||
* `status` - The current status of the microservice instance. | ||
+ **UP** | ||
+ **DOWN** | ||
+ **STARTING** | ||
+ **OUTOFSERVICE** | ||
|
||
* `created_at` - The creation time of the microservice instance, in RFC3339 format. | ||
|
||
* `updated_at` - The latest update time of the microservice instance, in RFC3339 format. | ||
|
||
<a name="microservice_instances_data_center"></a> | ||
The `data_center` block supports: | ||
|
||
* `name` - The name of the data center. | ||
|
||
* `region` - The custom region name of the data center. | ||
|
||
* `availability_zone` - The custom availability zone of the data center. | ||
|
||
<a name="microservice_instances_health_check"></a> | ||
The `health_check` block supports: | ||
|
||
* `mode` - The heartbeat mode of the health check. | ||
|
||
* `interval` - The heartbeat interval of the health check, in seconds. | ||
|
||
* `max_retries` - The maximum retry number of the health check. | ||
|
||
* `port` - The port of the health check. |
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
165 changes: 165 additions & 0 deletions
165
...icloud/services/acceptance/cse/data_source_huaweicloud_cse_microservice_instances_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,165 @@ | ||
package cse | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccDataMicroserviceInstances_basic(t *testing.T) { | ||
var ( | ||
name = acceptance.RandomAccResourceName() | ||
|
||
dataSource = "data.huaweicloud_cse_microservice_instances.test" | ||
dc = acceptance.InitDataSourceCheck(dataSource) | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckCSEMicroserviceEngineID(t) | ||
acceptance.TestAccPreCheckCSEMicroserviceEngineAdminPassword(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataMicroserviceInstances_expectErr(), | ||
ExpectError: regexp.MustCompile(`Micro-service does not exist`), | ||
}, | ||
{ | ||
Config: testAccDataMicroserviceInstances_basic(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestMatchResourceAttr(dataSource, "instances.#", regexp.MustCompile(`[1-9]\d*`)), | ||
resource.TestCheckOutput("is_host_name_set", "true"), | ||
resource.TestCheckOutput("is_endpoints_set", "true"), | ||
resource.TestCheckOutput("is_version_set", "true"), | ||
resource.TestCheckOutput("is_properties_set", "true"), | ||
resource.TestCheckOutput("is_health_check_set", "true"), | ||
resource.TestCheckOutput("is_data_center_set", "true"), | ||
resource.TestCheckOutput("is_created_at_set_and_valid", "true"), | ||
resource.TestCheckOutput("is_updated_at_set_and_valid", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataMicroserviceInstances_expectErr() string { | ||
randUUID, _ := uuid.GenerateUUID() | ||
return fmt.Sprintf(` | ||
data "huaweicloud_cse_microservice_engines" "test" {} | ||
locals { | ||
id_filter_result = [for o in data.huaweicloud_cse_microservice_engines.test.engines : o if o.id == "%[1]s"] | ||
} | ||
data "huaweicloud_cse_microservice_instances" "test" { | ||
auth_address = local.id_filter_result[0].service_registry_addresses[0].public | ||
connect_address = local.id_filter_result[0].service_registry_addresses[0].public | ||
microservice_id = "%[2]s" | ||
admin_user = "root" | ||
admin_pass = "%[3]s" | ||
} | ||
`, acceptance.HW_CSE_MICROSERVICE_ENGINE_ID, randUUID, acceptance.HW_CSE_MICROSERVICE_ENGINE_ADMIN_PASSWORD) | ||
} | ||
|
||
func testAccDataMicroserviceInstances_basic(name string) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
resource "huaweicloud_cse_microservice_instance" "test" { | ||
auth_address = local.id_filter_result[0].service_registry_addresses[0].public | ||
connect_address = local.id_filter_result[0].service_registry_addresses[0].public | ||
microservice_id = huaweicloud_cse_microservice.test.id | ||
host_name = "localhost_with_auth_address" | ||
endpoints = ["grpc://127.0.1.132:9980", "rest://127.0.0.111:8081"] | ||
version = "1.0.1" | ||
properties = { | ||
"nodeIP" = "127.0.0.1" | ||
} | ||
health_check { | ||
mode = "push" | ||
interval = 30 | ||
max_retries = 3 | ||
port = 8080 | ||
} | ||
data_center { | ||
name = "dc1" | ||
region = "%[2]s" | ||
availability_zone = data.huaweicloud_availability_zones.test.names[0] | ||
} | ||
admin_user = "root" | ||
admin_pass = "%[3]s" | ||
} | ||
data "huaweicloud_cse_microservice_instances" "test" { | ||
depends_on = [huaweicloud_cse_microservice_instance.test] | ||
auth_address = local.id_filter_result[0].service_registry_addresses[0].public | ||
connect_address = local.id_filter_result[0].service_registry_addresses[0].public | ||
microservice_id = huaweicloud_cse_microservice.test.id | ||
admin_user = "root" | ||
admin_pass = "%[3]s" | ||
} | ||
locals { | ||
filter_result = try([ | ||
for v in data.huaweicloud_cse_microservice_instances.test.instances : v if v.id == huaweicloud_cse_microservice_instance.test.id][0], null) | ||
} | ||
output "is_host_name_set" { | ||
value = try(local.filter_result.host_name == huaweicloud_cse_microservice_instance.test.host_name, false) | ||
} | ||
output "is_endpoints_set" { | ||
value = try(length(local.filter_result.endpoints) == length(huaweicloud_cse_microservice_instance.test.endpoints), false) | ||
} | ||
output "is_version_set" { | ||
value = try(local.filter_result.version == huaweicloud_cse_microservice_instance.test.version, false) | ||
} | ||
output "is_properties_set" { | ||
value = try(length(local.filter_result.properties) > 0, false) | ||
} | ||
output "is_health_check_set" { | ||
value = try(alltrue([ | ||
length(local.filter_result.health_check) > 0, | ||
local.filter_result.health_check[0].interval == huaweicloud_cse_microservice_instance.test.health_check[0].interval, | ||
local.filter_result.health_check[0].max_retries == huaweicloud_cse_microservice_instance.test.health_check[0].max_retries, | ||
local.filter_result.health_check[0].mode == huaweicloud_cse_microservice_instance.test.health_check[0].mode, | ||
local.filter_result.health_check[0].port == huaweicloud_cse_microservice_instance.test.health_check[0].port, | ||
]), false) | ||
} | ||
output "is_data_center_set" { | ||
value = try(alltrue([ | ||
length(local.filter_result.data_center) > 0, | ||
local.filter_result.data_center[0].name == huaweicloud_cse_microservice_instance.test.data_center[0].name, | ||
local.filter_result.data_center[0].region == huaweicloud_cse_microservice_instance.test.data_center[0].region, | ||
local.filter_result.data_center[0].availability_zone == huaweicloud_cse_microservice_instance.test.data_center[0].availability_zone, | ||
]), false) | ||
} | ||
output "is_created_at_set_and_valid" { | ||
value = try(length(regexall("^\\d{4}\\-\\d{2}\\-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:Z|[+-]\\d{2}:\\d{2})$", | ||
local.filter_result.created_at)) > 0, false) | ||
} | ||
output "is_updated_at_set_and_valid" { | ||
value = try(length(regexall("^\\d{4}\\-\\d{2}\\-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:Z|[+-]\\d{2}:\\d{2})$", | ||
local.filter_result.updated_at)) > 0, false) | ||
} | ||
`, testAccMicroserviceInstance_base(name), acceptance.HW_REGION_NAME, acceptance.HW_CSE_MICROSERVICE_ENGINE_ADMIN_PASSWORD) | ||
} |
Oops, something went wrong.