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

Fix describing vpcs with name_regex no results error #582

Merged
merged 1 commit into from
Aug 8, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ IMPROVEMENTS:

BUG FIXES:

- Fix describing vpcs with name_regex no results error ([#582](https://github.com/alibaba/terraform-provider/pull/582))
- Fix creating slb listener in international region failed error ([#578](https://github.com/alibaba/terraform-provider/pull/578))

## 1.10.1 (August 3, 2018)
Expand Down
8 changes: 8 additions & 0 deletions alicloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,11 @@ func buildClientToken(prefix string) string {
}
return token
}

func getNextpageNumber(number requests.Integer) (requests.Integer, error) {
page, err := strconv.Atoi(string(number))
if err != nil {
return "", err
}
return requests.NewInteger(page + 1), nil
}
34 changes: 16 additions & 18 deletions alicloud/data_source_alicloud_vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func dataSourceAlicloudVpcsRead(d *schema.ResourceData, meta interface{}) error
args := vpc.CreateDescribeVpcsRequest()
args.RegionId = string(getRegion(d, meta))
args.PageSize = requests.NewInteger(PageSizeLarge)
args.PageNumber = requests.NewInteger(1)

var allVpcs []vpc.Vpc

Expand All @@ -128,13 +129,24 @@ func dataSourceAlicloudVpcsRead(d *schema.ResourceData, meta interface{}) error
break
}

args.PageNumber = args.PageNumber + requests.NewInteger(1)
if page, err := getNextpageNumber(args.PageNumber); err != nil {
return err
} else {
args.PageNumber = page
}
}

var filteredVpcsTemp []vpc.Vpc
var filteredVpcs []vpc.Vpc
var route_tables []string
var r *regexp.Regexp
if nameRegex, ok := d.GetOk("name_regex"); ok && nameRegex.(string) != "" {
r = regexp.MustCompile(nameRegex.(string))
}

for _, v := range allVpcs {
if r != nil && !r.MatchString(v.VpcName) {
continue
}
if cidrBlock, ok := d.GetOk("cidr_block"); ok && v.CidrBlock != cidrBlock.(string) {
continue
}
Expand Down Expand Up @@ -165,21 +177,7 @@ func dataSourceAlicloudVpcsRead(d *schema.ResourceData, meta interface{}) error
route_tables = append(route_tables, "")
}

filteredVpcsTemp = append(filteredVpcsTemp, v)
}

var filteredVpcs []vpc.Vpc

if nameRegex, ok := d.GetOk("name_regex"); ok {
if r, err := regexp.Compile(nameRegex.(string)); err == nil {
for _, vpc := range filteredVpcsTemp {
if r.MatchString(vpc.VpcName) {
filteredVpcs = append(filteredVpcs, vpc)
}
}
}
} else {
filteredVpcs = filteredVpcsTemp[:]
filteredVpcs = append(filteredVpcs, v)
}

if len(filteredVpcs) < 1 {
Expand All @@ -188,7 +186,7 @@ func dataSourceAlicloudVpcsRead(d *schema.ResourceData, meta interface{}) error

log.Printf("[DEBUG] alicloud_vpc - VPCs found: %#v", allVpcs)

return vpcsDecriptionAttributes(d, filteredVpcsTemp, route_tables, meta)
return vpcsDecriptionAttributes(d, filteredVpcs, route_tables, meta)
}
func vpcVswitchIdListContains(vswitchIdList []string, vswitchId string) bool {
for _, idListItem := range vswitchIdList {
Expand Down
8 changes: 7 additions & 1 deletion alicloud/data_source_alicloud_vpcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ func TestAccAlicloudVpcsDataSource_cidr_block(t *testing.T) {
Config: testAccCheckAlicloudVpcsDataSourceCidrBlockConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAlicloudDataSourceID("data.alicloud_vpcs.vpc"),
resource.TestCheckResourceAttr("data.alicloud_vpcs.vpc", "vpcs.0.region_id", "cn-beijing"),
resource.TestCheckResourceAttr("data.alicloud_vpcs.vpc", "vpcs.#", "1"),
resource.TestCheckResourceAttr("data.alicloud_vpcs.vpc", "vpcs.0.status", "Available"),
resource.TestCheckResourceAttr("data.alicloud_vpcs.vpc", "vpcs.0.is_default", "false"),
resource.TestCheckResourceAttr("data.alicloud_vpcs.vpc", "vpcs.0.vpc_name", "testAccVpcsdatasourceNameRegex"),
),
},
},
})
}

const testAccCheckAlicloudVpcsDataSourceCidrBlockConfig = `
variable "name" {
default = "testAccVpcsdatasourceNameRegex"
}
resource "alicloud_vpc" "foo" {
name = "${var.name}"
cidr_block = "172.16.0.0/12"
}
data "alicloud_vpcs" "vpc" {
name_regex = "testAccVpcsdatasource*"
cidr_block = "${alicloud_vpc.foo.cidr_block}"
}
`