Skip to content

Commit

Permalink
[VPC] name attribute in resource/opentelekomcloud_vpc_eip_v1 (#2345)
Browse files Browse the repository at this point in the history
[VPC] name attribute in `resource/opentelekomcloud_vpc_eip_v1`

Summary of the Pull Request
PR Checklist

 Refers to: #2272
 Tests added/passed.
 Documentation updated.
 Schema updated.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestAccVpcV1EIP_basic
=== PAUSE TestAccVpcV1EIP_basic
=== CONT  TestAccVpcV1EIP_basic
--- PASS: TestAccVpcV1EIP_basic (63.31s)
=== RUN   TestAccVpcV1EIP_UnAssing
=== PAUSE TestAccVpcV1EIP_UnAssing
=== CONT  TestAccVpcV1EIP_UnAssing
--- PASS: TestAccVpcV1EIP_UnAssing (91.86s)
=== RUN   TestAccVpcV1EIP_timeout
=== PAUSE TestAccVpcV1EIP_timeout
=== CONT  TestAccVpcV1EIP_timeout
--- PASS: TestAccVpcV1EIP_timeout (40.14s)
PASS

Process finished with the exit code 0

=== RUN   TestAccVpcEipV1DataSource_basic
=== PAUSE TestAccVpcEipV1DataSource_basic
=== CONT  TestAccVpcEipV1DataSource_basic
--- PASS: TestAccVpcEipV1DataSource_basic (134.12s)
PASS

Process finished with exit code 0

Reviewed-by: Aloento
Reviewed-by: Vladimir Vshivkov
Reviewed-by: Artem Lifshits
  • Loading branch information
anton-sidelnikov authored Oct 18, 2023
1 parent 86cbb7e commit 29f47f4
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 9 deletions.
29 changes: 29 additions & 0 deletions docs/data-sources/vpc_eip_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@ output "eip_vpc_id" {
}
```

## Search by name regex

```hcl
resource "opentelekomcloud_vpc_eip_v1" "eip" {
publicip {
type = "5_bgp"
name = "my_eip"
}
bandwidth {
name = "acc-band"
size = 8
share_type = "PER"
charge_mode = "traffic"
}
}
data "opentelekomcloud_vpc_eip_v1" "by_regex" {
name_regex = "^my_.+"
}
output "eip_vpc_id" {
value = data.opentelekomcloud_vpc_eip_v1.by_regex.name
}
```

## Argument Reference

The arguments of this data source act as filters for querying the available
Expand All @@ -26,6 +51,8 @@ elastic IP whose data will be exported as attributes.

* `id` - (Optional) Specifies a resource ID in UUID format.

* `name_regex` - (Optional) A regex string to apply to the eip list. This allows more advanced filtering.

* `status` - (Optional) The status of the specific elastic IP to retrieve.

* `public_ip_address` - (Optional) The public IP address of the elastic IP.
Expand Down Expand Up @@ -56,3 +83,5 @@ All the argument attributes are also exported as result attributes.
* `tenant_id` - Specifies the project ID.

* `type` - Specifies the elastic IP type.

* `name` - Specifies the elastic IP Name.
21 changes: 21 additions & 0 deletions docs/resources/vpc_eip_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ resource "opentelekomcloud_vpc_eip_v1" "eip_1" {
}
```

## EIP with name

```hcl
resource "opentelekomcloud_vpc_eip_v1" "eip_1" {
publicip {
type = "5_bgp"
name = "my_eip"
}
bandwidth {
name = "test"
size = 8
share_type = "PER"
charge_mode = "traffic"
}
}
```

## Argument Reference

The following arguments are supported:
Expand All @@ -48,6 +65,8 @@ The `publicip` block supports:
* `port_id` - (Optional) The port id which this eip will associate with. If the value
is `""` or this not specified, the eip will be in unbind state.

* `name` - (Required) The ip name, which is a string of 1 to 64 characters.

The `bandwidth` block supports:

* `name` - (Required) The bandwidth name, which is a string of 1 to 64 characters
Expand Down Expand Up @@ -79,6 +98,8 @@ The following attributes are exported:

* `publicip/port_id` - See Argument Reference above.

* `publicip/name` - See Argument Reference above.

* `bandwidth/name` - See Argument Reference above.

* `bandwidth/size` - See Argument Reference above.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/jinzhu/copier v0.3.5
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20231011130319-58bd572a1dd9
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20231016103610-e853752801cb
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.14.0
golang.org/x/sync v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20231011130319-58bd572a1dd9 h1:Coq9L2f9X59P+vaxGAicEtnSNDGB5uXMrKPHIchWop4=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20231011130319-58bd572a1dd9/go.mod h1:9Deb3q2gJvq5dExV+aX+iO+G+mD9Zr9uFt+YY9ONmq0=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20231016103610-e853752801cb h1:YfP0/9KxxMnBQcopgfj36dPf3U0jnS6bfDQ3B3Lf3Ik=
github.com/opentelekomcloud/gophertelekomcloud v0.8.1-0.20231016103610-e853752801cb/go.mod h1:9Deb3q2gJvq5dExV+aX+iO+G+mD9Zr9uFt+YY9ONmq0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestAccVpcEipV1DataSource_basic(t *testing.T) {
dataSourceNameByID := "data.opentelekomcloud_vpc_eip_v1.by_id"
dataSourceNameByTags := "data.opentelekomcloud_vpc_eip_v1.by_tags"
dataSourceNameByIP := "data.opentelekomcloud_vpc_eip_v1.by_ip"
dataSourceNameByRegex := "data.opentelekomcloud_vpc_eip_v1.by_regex"

t.Parallel()
quotas.BookOne(t, quotas.FloatingIP)
Expand All @@ -35,6 +36,7 @@ func TestAccVpcEipV1DataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr(dataSourceNameByIP, "type", "5_bgp"),
resource.TestCheckResourceAttr(dataSourceNameByIP, "bandwidth_share_type", "PER"),
resource.TestCheckResourceAttr(dataSourceNameByIP, "status", "DOWN"),
resource.TestCheckResourceAttr(dataSourceNameByRegex, "name", "my_eip"),
),
},
{
Expand All @@ -48,6 +50,7 @@ const testAccDataSourceVpcEipV1Init = `
resource "opentelekomcloud_vpc_eip_v1" "eip" {
publicip {
type = "5_bgp"
name = "my_eip"
}
bandwidth {
name = "acc-band"
Expand All @@ -57,8 +60,8 @@ resource "opentelekomcloud_vpc_eip_v1" "eip" {
}
tags = {
muh = "value-create"
kuh = "value-create"
otc-tf-test-1 = "value-create"
otc-tf-test-2 = "value-create"
}
}
`
Expand All @@ -67,6 +70,7 @@ const testAccDataSourceVpcEipV1Config = `
resource "opentelekomcloud_vpc_eip_v1" "eip" {
publicip {
type = "5_bgp"
name = "my_eip"
}
bandwidth {
name = "acc-band"
Expand All @@ -76,8 +80,8 @@ resource "opentelekomcloud_vpc_eip_v1" "eip" {
}
tags = {
muh = "value-create"
kuh = "value-create"
otc-tf-test-1 = "value-create"
otc-tf-test-2 = "value-create"
}
}
Expand All @@ -91,8 +95,12 @@ data "opentelekomcloud_vpc_eip_v1" "by_ip" {
data "opentelekomcloud_vpc_eip_v1" "by_tags" {
tags = {
muh = "value-create"
kuh = "value-create"
otc-tf-test-1 = "value-create"
otc-tf-test-2 = "value-create"
}
}
data "opentelekomcloud_vpc_eip_v1" "by_regex" {
name_regex = "^my_.+"
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ func TestAccVpcV1EIP_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcV1EIPExists(resourceVPCEIPName, &eip),
resource.TestCheckResourceAttr(resourceVPCEIPName, "bandwidth.0.name", "acc-band"),
resource.TestCheckResourceAttr(resourceVPCEIPName, "publicip.0.name", "my_ip"),
),
},
{
Config: testAccVpcV1EIPUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckVpcV1EIPExists(resourceVPCEIPName, &eip),
resource.TestCheckResourceAttr(resourceVPCEIPName, "bandwidth.0.name", "acc-band-update"),
resource.TestCheckResourceAttr(resourceVPCEIPName, "publicip.0.name", "my_ip"),
resource.TestCheckResourceAttr(resourceVPCEIPName, "bandwidth.0.size", "25"),
),
},
Expand Down Expand Up @@ -151,6 +153,7 @@ const testAccVpcV1EIPBasic = `
resource "opentelekomcloud_vpc_eip_v1" "eip_1" {
publicip {
type = "5_bgp"
name = "my_ip"
}
bandwidth {
name = "acc-band"
Expand All @@ -169,6 +172,7 @@ const testAccVpcV1EIPUpdate = `
resource "opentelekomcloud_vpc_eip_v1" "eip_1" {
publicip {
type = "5_bgp"
name = "my_ip"
}
bandwidth {
name = "acc-band-update"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package vpc
import (
"context"
"log"
"regexp"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v1/eips"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common"
Expand All @@ -30,6 +32,11 @@ func DataSourceVPCEipV1() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"name_regex": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsValidRegExp,
},
"status": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -38,6 +45,10 @@ func DataSourceVPCEipV1() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"private_ip_address": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -100,6 +111,17 @@ func dataSourceVPCEipV1Read(_ context.Context, d *schema.ResourceData, meta inte
return fmterr.Errorf("unable to retrieve EIPs: %w", err)
}

var filteredEips []eips.PublicIp
if nameRegex, ok := d.GetOk("name_regex"); ok {
r := regexp.MustCompile(nameRegex.(string))
for _, ip := range refinedEIPs {
if r.MatchString(ip.Name) {
filteredEips = append(filteredEips, ip)
}
}
refinedEIPs = filteredEips
}

tagRaw := d.Get("tags").(map[string]interface{})
var refinedByTags []eips.PublicIp
networkingV2Client, err := config.NetworkingV2Client(config.GetRegion(d))
Expand Down Expand Up @@ -158,6 +180,7 @@ func dataSourceVPCEipV1Read(_ context.Context, d *schema.ResourceData, meta inte
d.Set("public_ip_address", elasticIP.PublicAddress),
d.Set("tenant_id", elasticIP.TenantID),
d.Set("region", config.GetRegion(d)),
d.Set("name", elasticIP.Name),
)

// save tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func ResourceVpcEIPV1() *schema.Resource {
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
},
},
Expand Down Expand Up @@ -177,6 +183,7 @@ func resourceVpcEIPV1Read(ctx context.Context, d *schema.ResourceData, meta inte
"type": eip.Type,
"ip_address": eip.PublicAddress,
"port_id": eip.PortID,
"name": eip.Name,
},
}
if err := d.Set("publicip", publicIP); err != nil {
Expand Down Expand Up @@ -314,6 +321,7 @@ func resourcePublicIP(d *schema.ResourceData) eips.PublicIpOpts {
publicIPRaw := d.Get("publicip").([]interface{})[0].(map[string]interface{})

publicIpOpts := eips.PublicIpOpts{
Name: publicIPRaw["name"].(string),
Type: publicIPRaw["type"].(string),
Address: publicIPRaw["ip_address"].(string),
}
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/eip-name-76300fff53cc851c.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
enhancements:
- |
**[VPC]** Add ``name`` attribute to ``resource/opentelekomcloud_vpc_eip_v1`` (`#2345 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2345>`_)
- |
**[VPC]** Search by `name_regex` in ``data-source/opentelekomcloud_vpc_eip_v1*`` (`#2345 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2345>`_)

0 comments on commit 29f47f4

Please sign in to comment.