From bafc666e89e149232750692e06d48bd9195c50ca Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Mon, 25 Nov 2019 09:25:51 +0000 Subject: [PATCH] Add VPC ids data source --- .../data_source_huaweicloud_vpc_ids_v1.go | 50 ++++++++++++++++ ...data_source_huaweicloud_vpc_ids_v1_test.go | 58 +++++++++++++++++++ huaweicloud/provider.go | 1 + 3 files changed, 109 insertions(+) create mode 100644 huaweicloud/data_source_huaweicloud_vpc_ids_v1.go create mode 100644 huaweicloud/data_source_huaweicloud_vpc_ids_v1_test.go diff --git a/huaweicloud/data_source_huaweicloud_vpc_ids_v1.go b/huaweicloud/data_source_huaweicloud_vpc_ids_v1.go new file mode 100644 index 0000000000..c11e95f6a2 --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_vpc_ids_v1.go @@ -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 +} diff --git a/huaweicloud/data_source_huaweicloud_vpc_ids_v1_test.go b/huaweicloud/data_source_huaweicloud_vpc_ids_v1_test.go new file mode 100644 index 0000000000..c216959119 --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_vpc_ids_v1_test.go @@ -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) +} diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index e21112b77f..027eedc565 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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(),