-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add vpc bandwidth data source and docs (#595)
* add vpc bandwidth data source and docs * Add some attributes for bandwidth resource
- Loading branch information
1 parent
c6f0b7a
commit 1c3a5b0
Showing
13 changed files
with
309 additions
and
7 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,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. |
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
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
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
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,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 | ||
} |
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,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) | ||
} |
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
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
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
32 changes: 32 additions & 0 deletions
32
vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/requests.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
vendor/github.com/huaweicloud/golangsdk/openstack/networking/v1/bandwidths/results.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.