Skip to content

Commit

Permalink
chore(nat): unify aotumatically code style and fix some error in test…
Browse files Browse the repository at this point in the history
… cases (#5785)

* chore(nat): unify automatically generated code style

* chore(nat): fix some error in test cases
  • Loading branch information
deer-hang authored Nov 1, 2024
1 parent 03be615 commit b7ca13f
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ data "huaweicloud_nat_private_gateways" "filter_by_tags" {
locals {
tags_filter_result = [
for v in data.huaweicloud_nat_private_gateways.filter_by_tags.gateways[*].tags : v == local.tags
for tagMap in data.huaweicloud_nat_private_gateways.filter_by_tags.gateways[*].tags : alltrue([
for k, v in local.tags : tagMap[k] == v
])
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ data "huaweicloud_nat_private_transit_ips" "filter_by_tags" {
locals {
tags_filter_result = [
for v in data.huaweicloud_nat_private_transit_ips.filter_by_tags.transit_ips[*].tags : v == local.tags
for tagMap in data.huaweicloud_nat_private_transit_ips.filter_by_tags.transit_ips[*].tags : alltrue([
for k, v in local.tags : tagMap[k] == v
])
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ func TestAccDatasourceSnatRules_basic(t *testing.T) {
bySourceType = "data.huaweicloud_nat_snat_rules.filter_by_source_type"
dcBySourceType = acceptance.InitDataSourceCheck(bySourceType)

bySubnetId = "data.huaweicloud_nat_snat_rules.filter_by_subnet_id"
dcBySubnetId = acceptance.InitDataSourceCheck(bySubnetId)

byEipId = "data.huaweicloud_nat_snat_rules.filter_by_floating_ip_id"
dcByEipId = acceptance.InitDataSourceCheck(byEipId)
)
Expand All @@ -50,9 +47,6 @@ func TestAccDatasourceSnatRules_basic(t *testing.T) {
dcBySourceType.CheckResourceExists(),
resource.TestCheckOutput("source_type_filter_is_useful", "true"),

dcBySubnetId.CheckResourceExists(),
resource.TestCheckOutput("subnet_id_filter_is_useful", "true"),

dcByEipId.CheckResourceExists(),
resource.TestCheckOutput("floating_ip_id_filter_is_useful", "true"),
),
Expand Down Expand Up @@ -110,6 +104,10 @@ locals {
}
data "huaweicloud_nat_snat_rules" "filter_by_rule_id" {
depends_on = [
huaweicloud_nat_snat_rule.test
]
rule_id = local.rule_id
}
Expand All @@ -128,6 +126,10 @@ locals {
}
data "huaweicloud_nat_snat_rules" "filter_by_gateway_id" {
depends_on = [
huaweicloud_nat_snat_rule.test
]
gateway_id = local.gateway_id
}
Expand All @@ -146,6 +148,10 @@ locals {
}
data "huaweicloud_nat_snat_rules" "filter_by_source_type" {
depends_on = [
huaweicloud_nat_snat_rule.test
]
source_type = local.source_type
}
Expand All @@ -159,29 +165,15 @@ output "source_type_filter_is_useful" {
value = alltrue(local.source_type_filter_result) && length(local.source_type_filter_result) > 0
}
locals {
subnet_id = data.huaweicloud_nat_snat_rules.test.rules[0].subnet_id
}
data "huaweicloud_nat_snat_rules" "filter_by_subnet_id" {
subnet_id = local.subnet_id
}
locals {
subnet_id_filter_result = [
for v in data.huaweicloud_nat_snat_rules.filter_by_subnet_id.rules[*].subnet_id : v == local.subnet_id
]
}
output "subnet_id_filter_is_useful" {
value = alltrue(local.subnet_id_filter_result) && length(local.subnet_id_filter_result) > 0
}
locals {
floating_ip_id = data.huaweicloud_nat_snat_rules.test.rules[0].floating_ip_id
}
data "huaweicloud_nat_snat_rules" "filter_by_floating_ip_id" {
depends_on = [
huaweicloud_nat_snat_rule.test
]
floating_ip_id = local.floating_ip_id
}
Expand All @@ -205,9 +197,6 @@ func TestAccDatasourceSnatRules_direct(t *testing.T) {
dataSourceName = "data.huaweicloud_nat_snat_rules.test"
dc = acceptance.InitDataSourceCheck(dataSourceName)

byCidr = "data.huaweicloud_nat_snat_rules.filter_by_cidr"
dcByCidr = acceptance.InitDataSourceCheck(byCidr)

bySourceType = "data.huaweicloud_nat_snat_rules.filter_by_source_type"
dcBySourceType = acceptance.InitDataSourceCheck(bySourceType)

Expand All @@ -226,8 +215,6 @@ func TestAccDatasourceSnatRules_direct(t *testing.T) {
Config: testAccDatasourceSnatRules_direct(baseConfig),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
dcByCidr.CheckResourceExists(),
resource.TestCheckOutput("cidr_filter_is_useful", "true"),

dcBySourceType.CheckResourceExists(),
resource.TestCheckOutput("source_type_filter_is_useful", "true"),
Expand Down Expand Up @@ -288,30 +275,15 @@ data "huaweicloud_nat_snat_rules" "test" {
]
}
locals {
cidr = data.huaweicloud_nat_snat_rules.test.rules[0].cidr
}
data "huaweicloud_nat_snat_rules" "filter_by_cidr" {
cidr = local.cidr
}
locals {
cidr_filter_result = [
for v in data.huaweicloud_nat_snat_rules.filter_by_cidr.rules[*].cidr :
v == local.cidr
]
}
output "cidr_filter_is_useful" {
value = alltrue(local.cidr_filter_result) && length(local.cidr_filter_result) > 0
}
locals {
source_type = data.huaweicloud_nat_snat_rules.test.rules[0].source_type
}
data "huaweicloud_nat_snat_rules" "filter_by_source_type" {
depends_on = [
huaweicloud_nat_snat_rule.direct
]
source_type = local.source_type
}
Expand All @@ -331,6 +303,10 @@ locals {
}
data "huaweicloud_nat_snat_rules" "filter_by_status" {
depends_on = [
huaweicloud_nat_snat_rule.direct
]
status = local.status
}
Expand All @@ -350,6 +326,10 @@ locals {
}
data "huaweicloud_nat_snat_rules" "filter_by_floating_ip_address" {
depends_on = [
huaweicloud_nat_snat_rule.direct
]
floating_ip_address = local.floating_ip_address
}
Expand Down
51 changes: 25 additions & 26 deletions huaweicloud/services/nat/data_source_huaweicloud_nat_dnat_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,57 +186,53 @@ func dnatRuleSchema() *schema.Resource {
}

func dataSourceDnatRulesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)

// listDnatRules: Query the DNAT rule list
var (
listDnatRulesHttpUrl = "v2/{project_id}/dnat_rules"
listDnatRulesProduct = "nat"
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v2/{project_id}/dnat_rules"
product = "nat"
)
listDnatRulesClient, err := cfg.NewServiceClient(listDnatRulesProduct, region)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating NAT client: %s", err)
}

listDnatRulesPath := listDnatRulesClient.Endpoint + listDnatRulesHttpUrl
listDnatRulesPath = strings.ReplaceAll(listDnatRulesPath, "{project_id}", listDnatRulesClient.ProjectID)

listDnatRulesQueryParams := buildListDnatRuleQueryParams(d)
listDnatRulesPath += listDnatRulesQueryParams
requestPath := client.Endpoint + httpUrl
requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID)
requestPath += buildListDnatRuleQueryParams(d)

listDnatRulesResp, err := pagination.ListAllItems(
listDnatRulesClient,
resp, err := pagination.ListAllItems(
client,
"marker",
listDnatRulesPath,
requestPath,
&pagination.QueryOpts{MarkerField: ""})

if err != nil {
return diag.Errorf("error retrieving DNAT rules %s", err)
}

listDnatRulesRespJson, err := json.Marshal(listDnatRulesResp)
respJson, err := json.Marshal(resp)
if err != nil {
return diag.FromErr(err)
}

var listDnatRulesRespBody interface{}
err = json.Unmarshal(listDnatRulesRespJson, &listDnatRulesRespBody)
var respBody interface{}
err = json.Unmarshal(respJson, &respBody)
if err != nil {
return diag.FromErr(err)
}

uuid, err := uuid.GenerateUUID()
generateUUID, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(uuid)
d.SetId(generateUUID)

var mErr *multierror.Error
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("rules", filterListDnatRuleResponseBody(flattenListDnatRulesResponseBody(listDnatRulesRespBody), d)),
d.Set("rules", filterListDnatRuleResponseBody(flattenListDnatRulesResponseBody(respBody), d)),
)

return diag.FromErr(mErr.ErrorOrNil())
Expand Down Expand Up @@ -274,15 +270,18 @@ func flattenListDnatRulesResponseBody(resp interface{}) []interface{} {
}

func filterListDnatRuleResponseBody(all []interface{}, d *schema.ResourceData) []interface{} {
rst := make([]interface{}, 0, len(all))
var (
globalEipID = d.Get("global_eip_id").(string)
globalEipAddress = d.Get("global_eip_address").(string)
rst = make([]interface{}, 0, len(all))
)

for _, v := range all {
if param, ok := d.GetOk("global_eip_id"); ok &&
fmt.Sprint(param) != utils.PathSearch("global_eip_id", v, nil) {
if globalEipID != "" && globalEipID != utils.PathSearch("global_eip_id", v, nil) {
continue
}

if param, ok := d.GetOk("global_eip_address"); ok &&
fmt.Sprint(param) != utils.PathSearch("global_eip_address", v, nil) {
if globalEipAddress != "" && globalEipAddress != utils.PathSearch("global_eip_address", v, nil) {
continue
}

Expand Down
30 changes: 16 additions & 14 deletions huaweicloud/services/nat/data_source_huaweicloud_nat_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,34 @@ func DataSourcePublicGateway() *schema.Resource {
}
}

func getVpdID(d *schema.ResourceData) string {
if v, ok := d.GetOk("vpc_id"); ok {
return v.(string)
}
return d.Get("router_id").(string)
}

func getSubnetID(d *schema.ResourceData) string {
if v, ok := d.GetOk("subnet_id"); ok {
return v.(string)
}
return d.Get("internal_network_id").(string)
}

func dataSourcePublicGatewayRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
natClient, err := cfg.NatGatewayClient(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating NAT v2 client: %s", err)
}

var vpcId, subnetId string
if v1, ok := d.GetOk("vpc_id"); ok {
vpcId = v1.(string)
} else {
vpcId = d.Get("router_id").(string)
}
if v2, ok := d.GetOk("subnet_id"); ok {
subnetId = v2.(string)
} else {
subnetId = d.Get("internal_network_id").(string)
}

listOpts := gateways.ListOpts{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Spec: d.Get("spec").(string),
VpcId: vpcId,
InternalNetworkId: subnetId,
VpcId: getVpdID(d),
InternalNetworkId: getSubnetID(d),
Status: d.Get("status").(string),
EnterpriseProjectId: d.Get("enterprise_project_id").(string),
}
Expand Down
Loading

0 comments on commit b7ca13f

Please sign in to comment.