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

[VPC] Add vpc_bandwidth_associate_v2 resource #1549

Merged
merged 4 commits into from Nov 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/resources/vpc_bandwidth_associate_v2.md
@@ -0,0 +1,59 @@
---
subcategory: "Virtual Private Cloud (VPC)"
---

# opentelekomcloud_vpc_bandwidth_associate_v2

Provides a resource to associate floating IP with a shared bandwidth within Open Telekom Cloud.

## Example Usage

```hcl
resource "opentelekomcloud_networking_floatingip_v2" "ip1" {}
resource "opentelekomcloud_networking_floatingip_v2" "ip2" {}

resource "opentelekomcloud_vpc_bandwidth_v2" "band20m" {
name = "bandwidth-20MBit"
size = 20
}

resource "opentelekomcloud_vpc_bandwidth_associate_v2" "associate" {
bandwidth = opentelekomcloud_vpc_bandwidth_v2.band20m.id
floating_ips = [
opentelekomcloud_networking_floatingip_v2.ip1.id,
opentelekomcloud_networking_floatingip_v2.ip2.id,
]
}
```

## Argument Reference

The following arguments are supported:

* `bandwidth` - (Required) Specifies ID of the bandwidth to be assigned.

* `floating_ips` - (Required) Specifies IDs of floating IPs to be added to the bandwidth.

->
After an EIP is removed from a shared bandwidth, a dedicated bandwidth will be allocated to the EIP, and you will be
billed for the dedicated bandwidth.

* `backup_charge_mode` - (Optional) Specifies whether the dedicated bandwidth used by the EIP that has been removed from
a shared bandwidth is billed by traffic or by bandwidth.

The value can be `bandwidth` or `traffic`.

Default value is `bandwidth`.

* `backup_size` - (Optional) Specifies the size (Mbit/s) of the dedicated bandwidth used by the EIP that has been
removed from a shared bandwidth.

Default value is `1`.

## Import

VPC bandwidth association can be imported using the bandwidth `id`, e.g.

```sh
terraform import opentelekomcloud_vpc_bandwidth_associate_v2.associate eb187fc8-e482-43eb-a18a-9da947ef89f6
```
2 changes: 1 addition & 1 deletion docs/resources/vpc_bandwidth_v2.md
Expand Up @@ -3,7 +3,7 @@ subcategory: "Virtual Private Cloud (VPC)"
---
# opentelekomcloud_vpc_bandwidth_v2

Provides a resource to create a shared bandwidth within OpenTelekomCloud.
Provides a resource to create a shared bandwidth within Open Telekom Cloud.

## Example Usage

Expand Down
@@ -0,0 +1,149 @@
package acceptance

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/bandwidths"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common/quotas"
)

const resourceBandwidthAssociateName = "opentelekomcloud_vpc_bandwidth_associate_v2.associate"

func TestBandwidthAssociateV2_basic(t *testing.T) {
var b bandwidths.Bandwidth

t.Parallel()
qts := quotas.MultipleQuotas{
{Q: quotas.SharedBandwidth, Count: 1},
{Q: quotas.FloatingIP, Count: 2},
}
quotas.BookMany(t, qts)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testCheckBandwidthV2Destroy,
Steps: []resource.TestStep{
{
Config: testBandwidthAssociateV2Basic,
Check: resource.ComposeTestCheckFunc(
testCheckBandwidthExists(resourceBandwidthAssociateName, &b),
resource.TestCheckResourceAttr(resourceBandwidthAssociateName, "floating_ips.#", "1"),
),
},
{
Config: testBandwidthAssociateV2Updated,
Check: resource.ComposeTestCheckFunc(
testCheckBandwidthExists(resourceBandwidthAssociateName, &b),
resource.TestCheckResourceAttr(resourceBandwidthAssociateName, "floating_ips.#", "1"),
),
},
},
})
}

func TestBandwidthAssociateV2_EIPv1(t *testing.T) {
var b bandwidths.Bandwidth

t.Parallel()
qts := quotas.MultipleQuotas{
{Q: quotas.SharedBandwidth, Count: 1},
{Q: quotas.FloatingIP, Count: 1},
}
quotas.BookMany(t, qts)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testCheckBandwidthV2Destroy,
Steps: []resource.TestStep{
{
Config: testBandwidthAssociateV2EipV1,
Check: resource.ComposeTestCheckFunc(
testCheckBandwidthExists(resourceBandwidthAssociateName, &b),
resource.TestCheckResourceAttr(resourceBandwidthAssociateName, "floating_ips.#", "1"),
),
ExpectNonEmptyPlan: true, // opentelekomcloud_vpc_eip_v1 bandwidth is updated
},
},
})
}

func TestBandwidthAssociateV2_import(t *testing.T) {
t.Parallel()
qts := quotas.MultipleQuotas{
{Q: quotas.SharedBandwidth, Count: 1},
{Q: quotas.FloatingIP, Count: 1},
}
quotas.BookMany(t, qts)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testCheckBandwidthV2Destroy,
Steps: []resource.TestStep{
{
Config: testBandwidthAssociateV2Basic,
},
{
ResourceName: resourceBandwidthAssociateName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"backup_charge_mode", "backup_size"},
},
},
})
}

const testBandwidthAssociateV2Basic = `
resource "opentelekomcloud_networking_floatingip_v2" "ip" {}

resource "opentelekomcloud_vpc_bandwidth_v2" "band_test" {
name = "shared-test-associate"
size = 20
}

resource "opentelekomcloud_vpc_bandwidth_associate_v2" "associate" {
bandwidth = opentelekomcloud_vpc_bandwidth_v2.band_test.id
floating_ips = [opentelekomcloud_networking_floatingip_v2.ip.id]
}
`

const testBandwidthAssociateV2Updated = `
resource "opentelekomcloud_networking_floatingip_v2" "ip2" {}

resource "opentelekomcloud_vpc_bandwidth_v2" "band_test" {
name = "shared-test-associate"
size = 20
}

resource "opentelekomcloud_vpc_bandwidth_associate_v2" "associate" {
bandwidth = opentelekomcloud_vpc_bandwidth_v2.band_test.id
floating_ips = [opentelekomcloud_networking_floatingip_v2.ip2.id]
}
`

const testBandwidthAssociateV2EipV1 = `
resource "opentelekomcloud_vpc_eip_v1" "eip" {
bandwidth {
name = "tmp-band"
share_type = "PER"
size = 10
}
publicip {
type = "5_bgp"
}
}

resource "opentelekomcloud_vpc_bandwidth_v2" "band_test" {
name = "shared-test-associate"
size = 20
}

resource "opentelekomcloud_vpc_bandwidth_associate_v2" "associate" {
bandwidth = opentelekomcloud_vpc_bandwidth_v2.band_test.id
floating_ips = [opentelekomcloud_vpc_eip_v1.eip.id]
}
`
8 changes: 8 additions & 0 deletions opentelekomcloud/common/utils.go
Expand Up @@ -209,3 +209,11 @@ var (
DataSourceTooFewDiag = diag.Errorf("your query returned no results. Please change your search criteria and try again.")
DataSourceTooManyDiag = diag.Errorf("your query returned more than one result. Please change your search criteria and try again.")
)

// GetSetChanges returns a pair of sets describing removed and added items
func GetSetChanges(d *schema.ResourceData, key string) (removed, added *schema.Set) {
oldOne, newOne := d.GetChange(key)
oldSet := oldOne.(*schema.Set)
newSet := newOne.(*schema.Set)
return oldSet.Difference(newSet), newSet.Difference(oldSet)
}
1 change: 1 addition & 0 deletions opentelekomcloud/provider.go
Expand Up @@ -424,6 +424,7 @@ func Provider() *schema.Provider {
"opentelekomcloud_swr_organization_permissions_v2": swr.ResourceSwrOrganizationPermissionsV2(),
"opentelekomcloud_swr_organization_v2": swr.ResourceSwrOrganizationV2(),
"opentelekomcloud_swr_repository_v2": swr.ResourceSwrRepositoryV2(),
"opentelekomcloud_vpc_bandwidth_associate_v2": vpc.ResourceBandwidthAssociateV2(),
"opentelekomcloud_vpc_bandwidth_v2": vpc.ResourceBandwidthV2(),
"opentelekomcloud_vpc_eip_v1": vpc.ResourceVpcEIPV1(),
"opentelekomcloud_vpc_v1": vpc.ResourceVirtualPrivateCloudV1(),
Expand Down