From 78eabe13c2f09e047ff1602cc87ef6132405046e Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Mon, 19 Oct 2020 19:55:52 +0800 Subject: [PATCH 1/2] add vpc bandwidth data source and docs --- docs/data-sources/vpc_bandwidth.md | 49 ++++++++ go.mod | 2 +- go.sum | 2 + .../data_source_huaweicloud_vpc_bandwidth.go | 110 ++++++++++++++++++ ...a_source_huaweicloud_vpc_bandwidth_test.go | 65 +++++++++++ huaweicloud/provider.go | 3 +- .../networking/v1/bandwidths/requests.go | 32 +++++ .../networking/v1/bandwidths/results.go | 11 ++ .../networking/v1/bandwidths/urls.go | 4 + vendor/modules.txt | 2 +- 10 files changed, 277 insertions(+), 3 deletions(-) create mode 100644 docs/data-sources/vpc_bandwidth.md create mode 100644 huaweicloud/data_source_huaweicloud_vpc_bandwidth.go create mode 100644 huaweicloud/data_source_huaweicloud_vpc_bandwidth_test.go diff --git a/docs/data-sources/vpc_bandwidth.md b/docs/data-sources/vpc_bandwidth.md new file mode 100644 index 0000000000..7d91e88cb0 --- /dev/null +++ b/docs/data-sources/vpc_bandwidth.md @@ -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. diff --git a/go.mod b/go.mod index 07e99700a8..e5d165d634 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 0369668fff..b7ea9ca138 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/huaweicloud/data_source_huaweicloud_vpc_bandwidth.go b/huaweicloud/data_source_huaweicloud_vpc_bandwidth.go new file mode 100644 index 0000000000..4bba7500cf --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_vpc_bandwidth.go @@ -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 +} diff --git a/huaweicloud/data_source_huaweicloud_vpc_bandwidth_test.go b/huaweicloud/data_source_huaweicloud_vpc_bandwidth_test.go new file mode 100644 index 0000000000..26d43eedc1 --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_vpc_bandwidth_test.go @@ -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) +} diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index cacc7d2572..63fcc54395 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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(), diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/requests.go b/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/requests.go index cea6ea7dc2..4627374aa1 100644 --- a/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/requests.go +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/requests.go @@ -38,3 +38,35 @@ func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) }) return } + +// ListOptsBuilder allows extensions to add additional parameters to the +// List request. +type ListOptsBuilder interface { + ToBWListQuery() (string, error) +} + +// ListOpts allows extensions to add additional parameters to the API. +type ListOpts struct { + ShareType string `q:"share_type"` + EnterpriseProjectID string `q:"enterprise_project_id"` +} + +// ToBWListQuery formats a ListOpts into a query string. +func (opts ListOpts) ToBWListQuery() (string, error) { + q, err := golangsdk.BuildQueryString(opts) + return q.String(), err +} + +// List is a method by which can get the detailed information of all bandwidths +func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) (r ListResult) { + url := listURL(client) + query, err := opts.ToBWListQuery() + if err != nil { + r.Err = err + return + } + url += query + + _, r.Err = client.Get(url, &r.Body, nil) + return +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/results.go b/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/results.go index 5dac6847ba..c38b51a7b3 100644 --- a/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/results.go +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/results.go @@ -68,3 +68,14 @@ func (r UpdateResult) Extract() (BandWidth, error) { err := r.Result.ExtractIntoStructPtr(&bw, "bandwidth") return bw, err } + +//ListResult is a struct which contains the result of list method +type ListResult struct { + golangsdk.Result +} + +func (r ListResult) Extract() ([]BandWidth, error) { + var s []BandWidth + err := r.ExtractIntoSlicePtr(&s, "bandwidths") + return s, err +} diff --git a/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/urls.go b/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/urls.go index 5f47750429..290ac302cd 100644 --- a/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/urls.go +++ b/vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/urls.go @@ -7,3 +7,7 @@ const resourcePath = "bandwidths" func resourceURL(client *golangsdk.ServiceClient, id string) string { return client.ServiceURL(client.ProjectID, resourcePath, id) } + +func listURL(client *golangsdk.ServiceClient) string { + return client.ServiceURL(client.ProjectID, resourcePath) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 8a25c368f3..5c9587895d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -186,7 +186,7 @@ github.com/hashicorp/terraform-svchost/auth github.com/hashicorp/terraform-svchost/disco # github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d github.com/hashicorp/yamux -# github.com/huaweicloud/golangsdk v0.0.0-20201013113019-d00b298c3cdd +# github.com/huaweicloud/golangsdk v0.0.0-20201019092827-0254ff170b2e ## explicit github.com/huaweicloud/golangsdk github.com/huaweicloud/golangsdk/internal From 0cbdd1fea44860ee00c74942c4be59337131863c Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Mon, 19 Oct 2020 20:11:45 +0800 Subject: [PATCH 2/2] Add some attributes for bandwidth resource --- docs/resources/vpc_bandwidth.md | 8 ++++++ .../resource_huaweicloud_vpc_bandwidth.go | 26 ++++++++++++++++--- ...resource_huaweicloud_vpc_bandwidth_test.go | 2 ++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/docs/resources/vpc_bandwidth.md b/docs/resources/vpc_bandwidth.md index 405af36e57..3cff5a3ccc 100644 --- a/docs/resources/vpc_bandwidth.md +++ b/docs/resources/vpc_bandwidth.md @@ -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. diff --git a/huaweicloud/resource_huaweicloud_vpc_bandwidth.go b/huaweicloud/resource_huaweicloud_vpc_bandwidth.go index 1c85b9b669..261fe5f9c2 100644 --- a/huaweicloud/resource_huaweicloud_vpc_bandwidth.go +++ b/huaweicloud/resource_huaweicloud_vpc_bandwidth.go @@ -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, + }, }, } } @@ -67,7 +84,6 @@ func resourceVpcBandWidthV2Create(d *schema.ResourceData, meta interface{}) erro } epsID := GetEnterpriseProjectID(d, config) - if epsID != "" { createOpts.EnterpriseProjectId = epsID } @@ -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", @@ -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 } @@ -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), @@ -176,7 +195,6 @@ func resourceVpcBandWidthV2Delete(d *schema.ResourceData, meta interface{}) erro } d.SetId("") - return nil } diff --git a/huaweicloud/resource_huaweicloud_vpc_bandwidth_test.go b/huaweicloud/resource_huaweicloud_vpc_bandwidth_test.go index e0d5a2a6d5..c8ec75660e 100644 --- a/huaweicloud/resource_huaweicloud_vpc_bandwidth_test.go +++ b/huaweicloud/resource_huaweicloud_vpc_bandwidth_test.go @@ -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"), ), }, {