/
data_source_huaweicloud_vpc_ids.go
61 lines (49 loc) · 1.43 KB
/
data_source_huaweicloud_vpc_ids.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package vpc
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/openstack/networking/v1/vpcs"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
// @API VPC GET /v1/{project_id}/vpcs
func DataSourceVpcIdsV1() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceVpcIdsV1Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceVpcIdsV1Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*config.Config)
vpcClient, err := config.NetworkingV1Client(config.GetRegion(d))
if err != nil {
return diag.Errorf("error creating Vpc client: %s", err)
}
listOpts := vpcs.ListOpts{}
refinedVpcs, err := vpcs.List(vpcClient, listOpts)
if err != nil {
return diag.Errorf("unable to retrieve vpcs: %s", err)
}
if len(refinedVpcs) < 1 {
return diag.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
}