Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature][Docs] Nat gateway support querying by enterprise_project_id #891

Merged
merged 2 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/data-sources/nat_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ data "huaweicloud_nat_gateway" "natgateway" {
* `router_id` - (Optional, String) Specifies the ID of the router this nat
gateway belongs to.

* `enterprise_project_id` - (Optional, String) Specifies the enterprise project
ID of the NAT gateway.

* `spec` - (Optional, String) The NAT gateway type.
The value can be:
* `1`: small type, which supports up to 10,000 SNAT connections.
Expand Down
22 changes: 14 additions & 8 deletions huaweicloud/data_source_huaweicloud_nat_gateway_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,30 @@ func dataSourceNatGatewayV2() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func dataSourceNatGatewayV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
natClient, err := config.natV2Client(GetRegion(d, config))
natClient, err := config.natGatewayV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud nat client: %s", err)
}

listOpts := natgateways.ListOpts{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Spec: d.Get("spec").(string),
RouterID: d.Get("router_id").(string),
InternalNetworkID: d.Get("internal_network_id").(string),
Status: d.Get("status").(string),
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Spec: d.Get("spec").(string),
RouterID: d.Get("router_id").(string),
InternalNetworkID: d.Get("internal_network_id").(string),
Status: d.Get("status").(string),
EnterpriseProjectID: d.Get("enterprise_project_id").(string),
}

pages, err := natgateways.List(natClient, listOpts).AllPages()
Expand Down Expand Up @@ -104,6 +109,7 @@ func dataSourceNatGatewayV2Read(d *schema.ResourceData, meta interface{}) error
d.Set("spec", natgateway.Spec)
d.Set("status", natgateway.Status)
d.Set("admin_state_up", natgateway.AdminStateUp)
d.Set("enterprise_project_id", natgateway.EnterpriseProjectID)

return nil
}
45 changes: 29 additions & 16 deletions huaweicloud/data_source_huaweicloud_nat_gateway_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ func TestAccNatGatewayDataSource_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckNatGatewayV2DataSourceID("data.huaweicloud_nat_gateway.nat_by_name"),
testAccCheckNatGatewayV2DataSourceID("data.huaweicloud_nat_gateway.nat_by_id"),
testAccCheckNatGatewayV2DataSourceID("data.huaweicloud_nat_gateway.nat_by_epsId"),
resource.TestCheckResourceAttr(
"data.huaweicloud_nat_gateway.nat_by_name", "name", natgateway),
resource.TestCheckResourceAttr(
"data.huaweicloud_nat_gateway.nat_by_id", "name", natgateway),
resource.TestCheckResourceAttr(
"data.huaweicloud_nat_gateway.nat_by_epsId", "name", natgateway),
),
},
},
Expand All @@ -48,32 +51,42 @@ func testAccCheckNatGatewayV2DataSourceID(n string) resource.TestCheckFunc {

func testAccNatGatewayV2DataSource_basic(name string) string {
return fmt.Sprintf(`
data "huaweicloud_enterprise_project" "enterprise_project_demo" {
name = "terraform"
}

resource "huaweicloud_vpc" "vpc_1" {
name = "%s"
cidr = "192.168.0.0/16"
}
name = "%s"
cidr = "192.168.0.0/16"
}

resource "huaweicloud_vpc_subnet" "subnet_1" {
name = "%s"
cidr = "192.168.199.0/24"
gateway_ip = "192.168.199.1"
vpc_id = huaweicloud_vpc.vpc_1.id
}
name = "%s"
cidr = "192.168.199.0/24"
gateway_ip = "192.168.199.1"
vpc_id = huaweicloud_vpc.vpc_1.id
}

resource "huaweicloud_nat_gateway" "nat_1" {
name = "%s"
description = "test for terraform"
spec = "1"
internal_network_id = huaweicloud_vpc_subnet.subnet_1.id
router_id = huaweicloud_vpc.vpc_1.id
}
name = "%s"
description = "test for terraform"
spec = "1"
internal_network_id = huaweicloud_vpc_subnet.subnet_1.id
router_id = huaweicloud_vpc.vpc_1.id
enterprise_project_id = data.huaweicloud_enterprise_project.enterprise_project_demo.id
}

data "huaweicloud_nat_gateway" "nat_by_name" {
name = huaweicloud_nat_gateway.nat_1.name
name = huaweicloud_nat_gateway.nat_1.name
}

data "huaweicloud_nat_gateway" "nat_by_id" {
id = huaweicloud_nat_gateway.nat_1.id
id = huaweicloud_nat_gateway.nat_1.id
}

data "huaweicloud_nat_gateway" "nat_by_epsId" {
id = huaweicloud_nat_gateway.nat_1.id
enterprise_project_id = data.huaweicloud_enterprise_project.enterprise_project_demo.id
}
`, name, name, name)
}