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

Add VPC ids data source #233

Merged
merged 1 commit into from
Nov 25, 2019
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
50 changes: 50 additions & 0 deletions huaweicloud/data_source_huaweicloud_vpc_ids_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package huaweicloud

import (
"fmt"

"github.com/huaweicloud/golangsdk/openstack/networking/v1/vpcs"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceVirtualPrivateCloudVpcIdsV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceVirtualPrivateCloudIdsV1Read,

Schema: map[string]*schema.Schema{
"ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

func dataSourceVirtualPrivateCloudIdsV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vpcClient, err := config.networkingV1Client(GetRegion(d, config))

listOpts := vpcs.ListOpts{}
refinedVpcs, err := vpcs.List(vpcClient, listOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve vpcs: %s", err)
}

if len(refinedVpcs) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}

listVpcs := make([]string, 0)

for _, vpc := range refinedVpcs {
listVpcs = append(listVpcs, vpc.ID)
}
d.SetId(listVpcs[0])
d.Set("ids", listVpcs)

return nil
}
58 changes: 58 additions & 0 deletions huaweicloud/data_source_huaweicloud_vpc_ids_v1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package huaweicloud

import (
"fmt"
"math/rand"
"testing"

"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccVpcIdsV1DataSource_basic(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
rInt := rand.Intn(50)
cidr := fmt.Sprintf("172.16.%d.0/24", rInt)
name := fmt.Sprintf("terraform-testacc-vpc-data-source-%d", rInt)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVpcIdsV1Config(name, cidr),
Check: resource.ComposeTestCheckFunc(
testAccVpcIdsV2DataSourceID("data.huaweicloud_vpc_ids_v1.vpc_ids"),
),
},
},
})
}

func testAccVpcIdsV2DataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find vpc data source: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Vpc data source ID not set")
}

return nil
}
}

func testAccDataSourceVpcIdsV1Config(name, cidr string) string {
return fmt.Sprintf(`
resource "huaweicloud_vpc_v1" "vpc_1" {
name = "%s"
cidr = "%s"
}

data "huaweicloud_vpc_ids_v1" "vpc_ids" {
}
`, name, cidr)
}
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_rts_stack_resource_v1": dataSourceRTSStackResourcesV1(),
"huaweicloud_iam_role_v3": dataSourceIAMRoleV3(),
"huaweicloud_vpc_v1": dataSourceVirtualPrivateCloudVpcV1(),
"huaweicloud_vpc_ids_v1": dataSourceVirtualPrivateCloudVpcIdsV1(),
"huaweicloud_vpc_peering_connection_v2": dataSourceVpcPeeringConnectionV2(),
"huaweicloud_vpc_route_v2": dataSourceVPCRouteV2(),
"huaweicloud_vpc_route_ids_v2": dataSourceVPCRouteIdsV2(),
Expand Down