Skip to content

Commit

Permalink
add iec subnet port data source support
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiChangkuo committed May 21, 2021
1 parent 1ad8634 commit 3918992
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/data-sources/iec_port.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
subcategory: "Intelligent EdgeCloud (IEC)"
---

# huaweicloud_iec_port

Use this data source to get the details of a specific IEC subnet port.

## Example Usage

```hcl
variable "subent_id" {}
data "huaweicloud_iec_port" "port_1" {
subnet_id = var.subent_id
fixed_ip = "192.168.1.123"
}
```

## Argument Reference

* `region` - (Optional, String) The region in which to obtain the port.
If omitted, the provider-level region will be used.

* `id` - (Optional, String) The ID of the port.

* `subnet_id` - (Optional, String) The ID of the subnet which the port belongs to.

* `fixed_ip` - (Optional, String) The IP address of the port.

* `mac_address` - (Optional, String) The MAC address of the port.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - Specifies a data source ID in UUID format.
* `status` - Indicates the status of the port.
* `site_id` - Indicates the ID of the IEC site.
* `security_groups` - Indicates the list of security group IDs applied on the port.
109 changes: 109 additions & 0 deletions huaweicloud/data_source_huaweicloud_iec_port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package huaweicloud

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"github.com/huaweicloud/golangsdk/openstack/iec/v1/ports"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)

func DataSourceIECPort() *schema.Resource {
return &schema.Resource{
Read: dataSourceIECPortRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"fixed_ip": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsIPAddress,
},
"mac_address": {
Type: schema.TypeString,
Optional: true,
},
"subnet_id": {
Type: schema.TypeString,
Optional: true,
},

"status": {
Type: schema.TypeString,
Computed: true,
},
"site_id": {
Type: schema.TypeString,
Computed: true,
},
"security_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceIECPortRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*config.Config)
iecClient, err := config.IECV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud IEC client: %s", err)
}

listOpts := ports.ListOpts{
ID: d.Get("id").(string),
NetworkID: d.Get("subnet_id").(string),
MacAddress: d.Get("mac_address").(string),
}

var ipFilter bool
if v, ipFilter := d.GetOk("fixed_ip"); ipFilter {
listOpts.FixedIPs = []string{fmt.Sprintf("ip_address=%s", v)}
}

allPorts, err := ports.List(iecClient, listOpts).Extract()
if err != nil {
return fmt.Errorf("Unable to retrieve huaweicloud IEC port: %s", err)
}

total := len(allPorts.Ports)
if total < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if total > 1 {
return fmt.Errorf("Your query returned more than one result. " +
"Please try a more specific search criteria.")
}

port := allPorts.Ports[0]
log.Printf("[DEBUG] Retrieved IEC port %s: %+v", port.ID, port)
d.SetId(port.ID)

d.Set("region", GetRegion(d, config))
d.Set("mac_address", port.MacAddress)
d.Set("subnet_id", port.NetworkID)
d.Set("status", port.Status)
d.Set("site_id", port.SiteID)
d.Set("security_groups", port.SecurityGroups)

if !ipFilter && len(port.FixedIPs) > 0 {
fixedIP := port.FixedIPs[0].IpAddress
d.Set("fixed_ip", fixedIP)
}

return nil
}
43 changes: 43 additions & 0 deletions huaweicloud/data_source_huaweicloud_iec_port_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package huaweicloud

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccIECPortDataSource_basic(t *testing.T) {
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
resourceName := "data.huaweicloud_iec_port.port_1"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckIecVpcSubnetV1Destroy,
Steps: []resource.TestStep{
{
Config: testAccIECNetworkConfig_base(rName),
},
{
Config: testAccIECPortDataSource_basic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "mac_address"),
resource.TestCheckResourceAttrSet(resourceName, "site_id"),
),
},
},
})
}

func testAccIECPortDataSource_basic(rName string) string {
return fmt.Sprintf(`
%s
data "huaweicloud_iec_port" "port_1" {
fixed_ip = huaweicloud_iec_vpc_subnet.subnet_1.gateway_ip
subnet_id = huaweicloud_iec_vpc_subnet.subnet_1.id
}
`, testAccIECNetworkConfig_base(rName))
}
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_identity_custom_role": DataSourceIdentityCustomRole(),
"huaweicloud_iec_flavors": dataSourceIecFlavors(),
"huaweicloud_iec_images": dataSourceIecImages(),
"huaweicloud_iec_port": DataSourceIECPort(),
"huaweicloud_iec_sites": dataSourceIecSites(),
"huaweicloud_iec_vpc": DataSourceIECVpc(),
"huaweicloud_iec_vpc_subnets": DataSourceIECVpcSubnets(),
Expand Down

0 comments on commit 3918992

Please sign in to comment.