Skip to content

Commit

Permalink
Add new datasource cen_bandwidth_limits
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhu36 committed Oct 12, 2018
1 parent d282604 commit 6e26b56
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ FEATURES:
- **New Resource:** `alicloud_datahub_project` ([#678](https://github.com/alibaba/terraform-provider/pull/678))
- **New Resource:** `alicloud_datahub_topic` ([#678](https://github.com/alibaba/terraform-provider/pull/678))
- **New Resource:** `alicloud_datahub_subscription` ([#678](https://github.com/alibaba/terraform-provider/pull/678))
- **New Data Source:** `alicloud_cen_bandwidth_limits` ([#733](https://github.com/alibaba/terraform-provider/pull/733))

IMPROVEMENTS:

Expand Down
157 changes: 157 additions & 0 deletions alicloud/data_source_alicloud_cen_bandwidth_limits.go
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 alicloud/data_source_alicloud_cen_bandwidth_limits_test.go
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}"]
}
`
1 change: 1 addition & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_kvstore_instances": dataSourceAlicloudKVStoreInstances(),
"alicloud_cen_instances": dataSourceAlicloudCenInstances(),
"alicloud_cen_bandwidth_packages": dataSourceAlicloudCenBandwidthPackages(),
"alicloud_cen_bandwidth_limits": dataSourceAlicloudCenBandwidthLimits(),
"alicloud_mns_queues": dataSourceAlicloudMNSQueues(),
"alicloud_mns_topics": dataSourceAlicloudMNSTopics(),
"alicloud_mns_topic_subscriptions": dataSourceAlicloudMNSTopicSubscriptions(),
Expand Down
7 changes: 5 additions & 2 deletions website/alicloud.erb
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,14 @@
<li<%= sidebar_current("docs-alicloud-datasource-pvtz-zone-records") %>>
<a href="/docs/providers/alicloud/d/pvtz_zone_records.html">alicloud_pvtz_zone_records</a>
</li>
<li<%= sidebar_current("docs-alicloud-datasource-cen-instances") %>>
<a href="/docs/providers/alicloud/d/cen_instances.html">alicloud_cen_instances</a>
</li>
<li<%= sidebar_current("docs-alicloud-datasource-cen-bandwidth-packages") %>>
<a href="/docs/providers/alicloud/d/cen_bandwidth_packages.html">alicloud_cen_bandwidth_packages</a>
</li>
<li<%= sidebar_current("docs-alicloud-datasource-cen-instances") %>>
<a href="/docs/providers/alicloud/d/cen_instances.html">alicloud_cen_instances</a>
<li<%= sidebar_current("docs-alicloud-datasource-cen-bandwidth-limits") %>>
<a href="/docs/providers/alicloud/d/cen_bandwidth_limits.html">alicloud_cen_bandwidth_limits</a>
</li>
<li<%= sidebar_current("docs-alicloud-datasource-mns-queues") %>>
<a href="/docs/providers/alicloud/d/mns_queues.html">alicloud_mns_queues</a>
Expand Down
41 changes: 41 additions & 0 deletions website/docs/d/cen_bandwidth_limits.html.markdown
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.

0 comments on commit 6e26b56

Please sign in to comment.