-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_iec_vpc.go
101 lines (85 loc) · 2.26 KB
/
data_source_huaweicloud_iec_vpc.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package iec
import (
"context"
"log"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/openstack/iec/v1/vpcs"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
// @API IEC GET /v1/vpcs
func DataSourceVpc() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceVpcRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"cidr": {
Type: schema.TypeString,
Computed: true,
},
"mode": {
Type: schema.TypeString,
Computed: true,
},
"subnet_num": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceVpcRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
iecClient, err := cfg.IECV1Client(region)
if err != nil {
return diag.Errorf("error creating IEC client: %s", err)
}
listOpts := vpcs.ListOpts{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
}
log.Printf("[DEBUG] Query VPCs using given filter: %+v", listOpts)
allVpcs, err := vpcs.List(iecClient, listOpts).Extract()
if err != nil {
return diag.Errorf("unable to retrieve vpcs: %s", err)
}
total := len(allVpcs.Vpcs)
if total < 1 {
return diag.Errorf("your query returned no results," +
"please change your search criteria and try again.")
}
if total > 1 {
return diag.Errorf("your query returned more than one result," +
" please try a more specific search criteria")
}
vpcInfo := allVpcs.Vpcs[0]
log.Printf("[DEBUG] Retrieved IEC VPC %s: %+v", vpcInfo.ID, vpcInfo)
d.SetId(vpcInfo.ID)
mErr := multierror.Append(nil,
d.Set("name", vpcInfo.Name),
d.Set("cidr", vpcInfo.Cidr),
d.Set("mode", vpcInfo.Mode),
d.Set("subnet_num", vpcInfo.SubnetNum),
d.Set("region", region),
)
if err := mErr.ErrorOrNil(); err != nil {
return diag.Errorf("error saving IEC VPC: %s", err)
}
return nil
}