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

add vpc bandwidth data source and docs #595

Merged
merged 2 commits into from
Oct 20, 2020
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
49 changes: 49 additions & 0 deletions docs/data-sources/vpc_bandwidth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
subcategory: "Elastic IP (EIP)"
---

# huaweicloud\_vpc\_bandwidth

Provides details about a specific bandwidth.

## Example Usage

```hcl
variable "bandwidth_name" {}

data "huaweicloud_vpc_bandwidth" "bandwidth_1" {
name = var.bandwidth_name
}
```

## Argument Reference

The arguments of this data source act as filters for querying the available
bandwidth in the current tenant. The following arguments are supported:

* `name` - (Required) The name of the Shared Bandwidth to retrieve.

* `size` - (Optional) The size of the Shared Bandwidth to retrieve. The value ranges from 5 to 2000 G.

* `enterprise_project_id` - (Optional) The enterprise project id of the Shared Bandwidth to retrieve.


## Attributes Reference

The following attributes are exported:

* `id` - ID of the Shared Bandwidth.

* `name` - See Argument Reference above.

* `size` - See Argument Reference above.

* `enterprise_project_id` - See Argument Reference above.

* `share_type` - Indicates whether the bandwidth is shared or dedicated.

* `bandwidth_type` - Indicates the bandwidth type.

* `charge_mode` - Indicates whether the billing is based on traffic, bandwidth, or 95th percentile bandwidth (enhanced).

* `status` - Indicates the bandwidth status.
8 changes: 8 additions & 0 deletions docs/resources/vpc_bandwidth.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ The following attributes are exported:

* `enterprise_project_id` - See Argument Reference above.

* `share_type` - Indicates whether the bandwidth is shared or dedicated.

* `bandwidth_type` - Indicates the bandwidth type.

* `charge_mode` - Indicates whether the billing is based on traffic, bandwidth, or 95th percentile bandwidth (enhanced).

* `status` - Indicates the bandwidth status.

## Import

Shared Bandwidths can be imported using the `id`, e.g.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/terraform-plugin-sdk v1.13.0
github.com/huaweicloud/golangsdk v0.0.0-20201013113019-d00b298c3cdd
github.com/huaweicloud/golangsdk v0.0.0-20201019092827-0254ff170b2e
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a
github.com/mitchellh/go-homedir v1.1.0
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ github.com/huaweicloud/golangsdk v0.0.0-20201010022156-88a65d817494 h1:3nOQxuXHU
github.com/huaweicloud/golangsdk v0.0.0-20201010022156-88a65d817494/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20201013113019-d00b298c3cdd h1:98+a/BpLn8AJBvFEKZIxAt4TnyWcgfOF02PQQoj9+Js=
github.com/huaweicloud/golangsdk v0.0.0-20201013113019-d00b298c3cdd/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20201019092827-0254ff170b2e h1:3/OcFZHyBNfPlLxv8BiCP3b6rkzhfIQWU2TmwBEmb7g=
github.com/huaweicloud/golangsdk v0.0.0-20201019092827-0254ff170b2e/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a h1:FyS/ubzBR5xJlnJGRTwe7GUHpJOR4ukYK3y+LFNffuA=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a/go.mod h1:uoIMjNxUfXi48Ci40IXkPRbghZ1vbti6v9LCbNqRgHY=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
110 changes: 110 additions & 0 deletions huaweicloud/data_source_huaweicloud_vpc_bandwidth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package huaweicloud

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths"
)

func dataSourceBandWidth() *schema.Resource {
return &schema.Resource{
Read: dataSourceBandWidthRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"size": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validateIntegerInRange(5, 2000),
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
},
"share_type": {
Type: schema.TypeString,
Computed: true,
},
"bandwidth_type": {
Type: schema.TypeString,
Computed: true,
},
"charge_mode": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

listOpts := bandwidths.ListOpts{
ShareType: "WHOLE",
}
if v, ok := d.GetOk("enterprise_project_id"); ok {
listOpts.EnterpriseProjectID = v.(string)
}

allBWs, err := bandwidths.List(vpcClient, listOpts).Extract()
if err != nil {
return fmt.Errorf("Unable to list huaweicloud bandwidths: %s", err)
}
if len(allBWs) == 0 {
return fmt.Errorf("No huaweicloud bandwidth was found")
}

// Filter bandwidths by "name"
var bandList []bandwidths.BandWidth
name := d.Get("name").(string)
for _, band := range allBWs {
if name == band.Name {
bandList = append(bandList, band)
}
}
if len(bandList) == 0 {
return fmt.Errorf("No huaweicloud bandwidth was found by name: %s", name)
}

// Filter bandwidths by "size"
result := bandList[0]
if v, ok := d.GetOk("size"); ok {
var found bool
for _, band := range bandList {
if v.(int) == band.Size {
found = true
result = band
break
}
}
if !found {
return fmt.Errorf("No huaweicloud bandwidth was found by size: %d", v.(int))
}
}

log.Printf("[DEBUG] Retrieved huaweicloud bandwidth %s: %+v", result.ID, result)
d.SetId(result.ID)
d.Set("name", result.Name)
d.Set("size", result.Size)
d.Set("enterprise_project_id", result.EnterpriseProjectID)

d.Set("share_type", result.ShareType)
d.Set("bandwidth_type", result.BandwidthType)
d.Set("charge_mode", result.ChargeMode)
d.Set("status", result.Status)
return nil
}
65 changes: 65 additions & 0 deletions huaweicloud/data_source_huaweicloud_vpc_bandwidth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package huaweicloud

import (
"fmt"
"testing"

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

func TestAccBandWidthDataSource_basic(t *testing.T) {
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
dataName := "data.huaweicloud_vpc_bandwidth.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccBandWidthDataSource_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckBandWidthDataSourceExists(dataName),
resource.TestCheckResourceAttr(dataName, "name", rName),
resource.TestCheckResourceAttr(dataName, "size", "10"),
),
},
},
})
}

func testAccCheckBandWidthDataSourceExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("root module has no resource called %s", n)
}

bandwidthRs, ok := s.RootModule().Resources["huaweicloud_vpc_bandwidth.test"]
if !ok {
return fmt.Errorf("can't find huaweicloud_vpc_bandwidth.test in state")
}

attr := rs.Primary.Attributes
if attr["id"] != bandwidthRs.Primary.Attributes["id"] {
return fmt.Errorf("Attribute 'id' expected %s; got %s",
bandwidthRs.Primary.Attributes["id"], attr["id"])
}

return nil
}
}

func testAccBandWidthDataSource_basic(rName string) string {
return fmt.Sprintf(`
resource "huaweicloud_vpc_bandwidth" "test" {
name = "%s"
size = 10
}

data "huaweicloud_vpc_bandwidth" "test" {
name = huaweicloud_vpc_bandwidth.test.name
}
`, rName)
}
3 changes: 2 additions & 1 deletion huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_obs_bucket_object": dataSourceObsBucketObject(),
"huaweicloud_rds_flavors": dataSourceRdsFlavorV3(),
"huaweicloud_sfs_file_system": dataSourceSFSFileSystemV2(),
"huaweicloud_vpc": DataSourceVirtualPrivateCloudVpcV1(),
"huaweicloud_vbs_backup_policy": dataSourceVBSBackupPolicyV2(),
"huaweicloud_vbs_backup": dataSourceVBSBackupV2(),
"huaweicloud_vpc": DataSourceVirtualPrivateCloudVpcV1(),
"huaweicloud_vpc_bandwidth": dataSourceBandWidth(),
"huaweicloud_vpc_ids": dataSourceVirtualPrivateCloudVpcIdsV1(),
"huaweicloud_vpc_peering_connection": dataSourceVpcPeeringConnectionV2(),
"huaweicloud_vpc_route": DataSourceVPCRouteV2(),
Expand Down
26 changes: 22 additions & 4 deletions huaweicloud/resource_huaweicloud_vpc_bandwidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ func resourceVpcBandWidthV2() *schema.Resource {
ForceNew: true,
Computed: true,
},

"share_type": {
Type: schema.TypeString,
Computed: true,
},
"bandwidth_type": {
Type: schema.TypeString,
Computed: true,
},
"charge_mode": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand All @@ -67,7 +84,6 @@ func resourceVpcBandWidthV2Create(d *schema.ResourceData, meta interface{}) erro
}

epsID := GetEnterpriseProjectID(d, config)

if epsID != "" {
createOpts.EnterpriseProjectId = epsID
}
Expand All @@ -89,7 +105,6 @@ func resourceVpcBandWidthV2Create(d *schema.ResourceData, meta interface{}) erro
}

_, err = stateConf.WaitForState()

if err != nil {
return fmt.Errorf(
"Error waiting for Bandwidth (%s) to become ACTIVE for creation: %s",
Expand Down Expand Up @@ -145,6 +160,10 @@ func resourceVpcBandWidthV2Read(d *schema.ResourceData, meta interface{}) error
d.Set("size", b.Size)
d.Set("enterprise_project_id", b.EnterpriseProjectID)

d.Set("share_type", b.ShareType)
d.Set("bandwidth_type", b.BandwidthType)
d.Set("charge_mode", b.ChargeMode)
d.Set("status", b.Status)
return nil
}

Expand All @@ -162,7 +181,7 @@ func resourceVpcBandWidthV2Delete(d *schema.ResourceData, meta interface{}) erro
}

stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE"},
Pending: []string{"NORMAL"},
Target: []string{"DELETED"},
Refresh: waitForBandwidth(NetworkingV1Client, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Expand All @@ -176,7 +195,6 @@ func resourceVpcBandWidthV2Delete(d *schema.ResourceData, meta interface{}) erro
}

d.SetId("")

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions huaweicloud/resource_huaweicloud_vpc_bandwidth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestAccVpcBandWidthV2_basic(t *testing.T) {
testAccCheckVpcBandWidthV2Exists(resourceName, &bandwidth),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "size", "5"),
resource.TestCheckResourceAttr(resourceName, "share_type", "WHOLE"),
resource.TestCheckResourceAttr(resourceName, "status", "NORMAL"),
),
},
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading