-
Notifications
You must be signed in to change notification settings - Fork 7
/
data_source_hcs_vpc.go
120 lines (107 loc) · 2.96 KB
/
data_source_hcs_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package vpc
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/config"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/networking/v1/vpcs"
)
func DataSourceVpcV1() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceVpcV1Read,
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,
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"cidr": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"routes": {
Type: schema.TypeList,
Computed: true,
Deprecated: "use huaweicloud_vpc_route_table data source to get all routes",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"destination": {
Type: schema.TypeString,
Computed: true,
},
"nexthop": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceVpcV1Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
hcsConfig := config.GetHcsConfig(meta)
vpcClient, err := hcsConfig.NetworkingV1Client(hcsConfig.GetRegion(d))
if err != nil {
return diag.Errorf("error creating VPC client: %s", err)
}
listOpts := vpcs.ListOpts{
ID: d.Get("id").(string),
Name: d.Get("name").(string),
Status: d.Get("status").(string),
CIDR: d.Get("cidr").(string),
EnterpriseProjectID: hcsConfig.DataGetEnterpriseProjectID(d),
}
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.")
}
if len(refinedVpcs) > 1 {
return diag.Errorf("your query returned more than one result." +
" Please try a more specific search criteria")
}
Vpc := refinedVpcs[0]
log.Printf("[INFO] Retrieved Vpc using given filter %s: %+v", Vpc.ID, Vpc)
d.SetId(Vpc.ID)
d.Set("region", hcsConfig.GetRegion(d))
d.Set("name", Vpc.Name)
d.Set("enterprise_project_id", Vpc.EnterpriseProjectID)
d.Set("cidr", Vpc.CIDR)
d.Set("status", Vpc.Status)
var s []map[string]interface{}
for _, route := range Vpc.Routes {
mapping := map[string]interface{}{
"destination": route.DestinationCIDR,
"nexthop": route.NextHop,
}
s = append(s, mapping)
}
d.Set("routes", s)
return nil
}