-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new datasource cen_bandwidth_limits
- Loading branch information
Showing
6 changed files
with
303 additions
and
2 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
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,157 @@ | ||
package alicloud | ||
|
||
import ( | ||
"fmt" | ||
|
||
"log" | ||
|
||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" | ||
"github.com/aliyun/alibaba-cloud-sdk-go/services/cbn" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAlicloudCenBandwidthLimits() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAlicloudCenBandwidthLimitsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"instance_ids": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
ForceNew: true, | ||
}, | ||
"output_file": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
|
||
// Computed values | ||
"limits": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"local_region_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"opposite_region_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"bandwidth_limit": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAlicloudCenBandwidthLimitsRead(d *schema.ResourceData, meta interface{}) error { | ||
var allCenBwLimits []cbn.CenInterRegionBandwidthLimit | ||
|
||
instanceIds := make([]string, 0) | ||
if v, ok := d.GetOk("instance_ids"); ok { | ||
for _, vv := range v.([]interface{}) { | ||
instanceIds = append(instanceIds, Trim(vv.(string))) | ||
} | ||
} else { | ||
instanceIds = append(instanceIds, "") | ||
} | ||
|
||
for _, instanceId := range instanceIds { | ||
tmpAllCenBwLimits, err := getCenBandwidthLimits(instanceId, meta) | ||
if err != nil { | ||
return err | ||
} else { | ||
allCenBwLimits = append(allCenBwLimits, tmpAllCenBwLimits...) | ||
} | ||
} | ||
|
||
if len(allCenBwLimits) < 1 { | ||
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") | ||
} | ||
|
||
log.Printf("[DEBUG] alicloud_cen_bandwidth_limits - CensBwLimits found: %#v", allCenBwLimits) | ||
return cenInterRegionBandwidthLimitsAttributes(d, allCenBwLimits) | ||
} | ||
|
||
func getCenBandwidthLimits(instanceId string, meta interface{}) ([]cbn.CenInterRegionBandwidthLimit, error) { | ||
conn := meta.(*AliyunClient).cenconn | ||
|
||
args := cbn.CreateDescribeCenInterRegionBandwidthLimitsRequest() | ||
args.PageSize = requests.NewInteger(PageSizeLarge) | ||
args.PageNumber = requests.NewInteger(1) | ||
if instanceId != "" { | ||
args.CenId = instanceId | ||
} | ||
|
||
var allCenBwLimits []cbn.CenInterRegionBandwidthLimit | ||
|
||
for { | ||
resp, err := conn.DescribeCenInterRegionBandwidthLimits(args) | ||
if err != nil { | ||
return allCenBwLimits, err | ||
} | ||
|
||
if resp == nil || len(resp.CenInterRegionBandwidthLimits.CenInterRegionBandwidthLimit) < 1 { | ||
break | ||
} | ||
allCenBwLimits = append(allCenBwLimits, resp.CenInterRegionBandwidthLimits.CenInterRegionBandwidthLimit...) | ||
|
||
if len(resp.CenInterRegionBandwidthLimits.CenInterRegionBandwidthLimit) < PageSizeLarge { | ||
break | ||
} | ||
|
||
if page, err := getNextpageNumber(args.PageNumber); err != nil { | ||
return allCenBwLimits, err | ||
} else { | ||
args.PageNumber = page | ||
} | ||
} | ||
|
||
return allCenBwLimits, nil | ||
} | ||
|
||
func cenInterRegionBandwidthLimitsAttributes(d *schema.ResourceData, allCenBwLimits []cbn.CenInterRegionBandwidthLimit) error { | ||
var ids []string | ||
var s []map[string]interface{} | ||
|
||
for _, cenBwLimit := range allCenBwLimits { | ||
mapping := map[string]interface{}{ | ||
"instance_id": cenBwLimit.CenId, | ||
"local_region_id": cenBwLimit.LocalRegionId, | ||
"opposite_region_id": cenBwLimit.OppositeRegionId, | ||
"status": cenBwLimit.Status, | ||
"bandwidth_limit": cenBwLimit.BandwidthLimit, | ||
} | ||
|
||
id := cenBwLimit.CenId + COLON_SEPARATED + cenBwLimit.LocalRegionId + COLON_SEPARATED + cenBwLimit.OppositeRegionId | ||
ids = append(ids, id) | ||
s = append(s, mapping) | ||
} | ||
|
||
d.SetId(dataResourceIdHash(ids)) | ||
if err := d.Set("limits", s); err != nil { | ||
return err | ||
} | ||
|
||
// create a json file in current directory and write data source to it. | ||
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { | ||
writeToFile(output.(string), s) | ||
} | ||
|
||
return nil | ||
} |
98 changes: 98 additions & 0 deletions
98
alicloud/data_source_alicloud_cen_bandwidth_limits_test.go
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,98 @@ | ||
package alicloud | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAlicloudCenBandwidthLimitsDataSource_instance_id(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckAlicloudCenInterRegionBandwidthLimitsDataSourceCenIdConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAlicloudDataSourceID("data.alicloud_cen_bandwidth_limits.limit"), | ||
resource.TestCheckResourceAttr("data.alicloud_cen_bandwidth_limits.limit", "limits.0.local_region_id", "cn-beijing"), | ||
resource.TestCheckResourceAttr("data.alicloud_cen_bandwidth_limits.limit", "limits.0.opposite_region_id", "us-west-1"), | ||
resource.TestCheckResourceAttr("data.alicloud_cen_bandwidth_limits.limit", "limits.0.status", "Active"), | ||
resource.TestCheckResourceAttr("data.alicloud_cen_bandwidth_limits.limit", "limits.0.bandwidth_limit", "15"), | ||
resource.TestCheckResourceAttrSet("data.alicloud_cen_bandwidth_limits.limit", "limits.0.instance_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccCheckAlicloudCenInterRegionBandwidthLimitsDataSourceCenIdConfig = ` | ||
provider "alicloud" { | ||
alias = "bj" | ||
region = "cn-beijing" | ||
} | ||
provider "alicloud" { | ||
alias = "us" | ||
region = "us-west-1" | ||
} | ||
resource "alicloud_vpc" "vpc1" { | ||
provider = "alicloud.bj" | ||
name = "tf-testAccTerraform-01" | ||
cidr_block = "192.168.0.0/16" | ||
} | ||
resource "alicloud_vpc" "vpc2" { | ||
provider = "alicloud.us" | ||
name = "tf-testAccTerraform-02" | ||
cidr_block = "172.16.0.0/12" | ||
} | ||
resource "alicloud_cen_instance" "cen" { | ||
name = "tf-testAccTerraform-01" | ||
description = "tf-testAccTerraform01" | ||
} | ||
resource "alicloud_cen_bandwidth_package" "bwp" { | ||
name = "tf-testAccCenBandwidthPackage" | ||
bandwidth = 20 | ||
geographic_region_ids = [ | ||
"China", | ||
"North-America"] | ||
} | ||
resource "alicloud_cen_bandwidth_package_attachment" "bwp_attach" { | ||
instance_id = "${alicloud_cen_instance.cen.id}" | ||
bandwidth_package_id = "${alicloud_cen_bandwidth_package.bwp.id}" | ||
} | ||
resource "alicloud_cen_instance_attachment" "vpc_attach_1" { | ||
instance_id = "${alicloud_cen_instance.cen.id}" | ||
child_instance_id = "${alicloud_vpc.vpc1.id}" | ||
child_instance_region_id = "cn-beijing" | ||
} | ||
resource "alicloud_cen_instance_attachment" "vpc_attach_2" { | ||
instance_id = "${alicloud_cen_instance.cen.id}" | ||
child_instance_id = "${alicloud_vpc.vpc2.id}" | ||
child_instance_region_id = "us-west-1" | ||
} | ||
resource "alicloud_cen_bandwidth_limit" "foo" { | ||
instance_id = "${alicloud_cen_instance.cen.id}" | ||
region_ids = ["cn-beijing", | ||
"us-west-1"] | ||
bandwidth_limit = 15 | ||
depends_on = [ | ||
"alicloud_cen_bandwidth_package_attachment.bwp_attach", | ||
"alicloud_cen_instance_attachment.vpc_attach_1", | ||
"alicloud_cen_instance_attachment.vpc_attach_2"] | ||
} | ||
data "alicloud_cen_bandwidth_limits" "limit" { | ||
instance_ids = ["${alicloud_cen_bandwidth_limit.foo.instance_id}"] | ||
} | ||
` |
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,41 @@ | ||
--- | ||
layout: "alicloud" | ||
page_title: "Alicloud: alicloud_cen_bandwidth_limits" | ||
sidebar_current: "docs-alicloud-datasource-cen-bandwidth-limits" | ||
description: |- | ||
Provides a list of CEN Bandwidth Limits owned by an Alibaba Cloud account. | ||
--- | ||
|
||
# alicloud\_cen\_bandwidth\_limits | ||
|
||
This data source provides CEN Bandwidth Limits available to the user. | ||
|
||
## Example Usage | ||
|
||
``` | ||
data "alicloud_cen_bandwidth_limits" "bwl"{ | ||
instance_ids = ["cen-id1"] | ||
} | ||
output "first_cen_bandwidth_limits_local_region_id" { | ||
value = "${data.alicloud_cen_bandwidth_packages.bwl.bandwidth_limits.0.local_region_id}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `instance_ids` - (Optional) A list of CEN instances IDs. | ||
* `output_file` - (Optional) File name where to save data source results (after running `terraform plan`). | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported in addition to the arguments listed above: | ||
|
||
* `limits` - A list of CEN Bandwidth Limits. Each element contains the following attributes: | ||
* `instance_id` - ID of the CEN instance. | ||
* `local_region_id` - ID of local region. | ||
* `opposite_region_id` - ID of opposite region. | ||
* `status` - Status of the CEN Bandwidth Limit, including "Active" and "Modifying". | ||
* `bandwidth_limit` - The bandwidth limit configured for the interconnected regions communication. |