-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add iec subnet port data source support
- Loading branch information
1 parent
1ad8634
commit 3918992
Showing
4 changed files
with
193 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,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. |
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,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 | ||
} |
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,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)) | ||
} |
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