/
data_source_openstack_networking_router_v2.go
158 lines (136 loc) · 3.79 KB
/
data_source_openstack_networking_router_v2.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package openstack
import (
"fmt"
"log"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceNetworkingRouterV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkingRouterV2Read,
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"router_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"admin_state_up": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"distributed": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"tenant_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"external_network_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"enable_snat": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
"availability_zone_hints": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"external_fixed_ip": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnet_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"ip_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
}
func dataSourceNetworkingRouterV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
listOpts := routers.ListOpts{}
if v, ok := d.GetOk("router_id"); ok {
listOpts.ID = v.(string)
}
if v, ok := d.GetOk("name"); ok {
listOpts.Name = v.(string)
}
if v, ok := d.GetOkExists("admin_state_up"); ok {
asu := v.(bool)
listOpts.AdminStateUp = &asu
}
if v, ok := d.GetOkExists("distributed"); ok {
dist := v.(bool)
listOpts.Distributed = &dist
}
if v, ok := d.GetOk("status"); ok {
listOpts.Status = v.(string)
}
if v, ok := d.GetOk("tenant_id"); ok {
listOpts.TenantID = v.(string)
}
pages, err := routers.List(networkingClient, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to list Routers: %s", err)
}
allRouters, err := routers.ExtractRouters(pages)
if err != nil {
return fmt.Errorf("Unable to retrieve Routers: %s", err)
}
if len(allRouters) < 1 {
return fmt.Errorf("No Router found")
}
if len(allRouters) > 1 {
return fmt.Errorf("More than one Router found")
}
router := allRouters[0]
log.Printf("[DEBUG] Retrieved Router %s: %+v", router.ID, router)
d.SetId(router.ID)
d.Set("name", router.Name)
d.Set("admin_state_up", router.AdminStateUp)
d.Set("distributed", router.Distributed)
d.Set("status", router.Status)
d.Set("tenant_id", router.TenantID)
d.Set("external_network_id", router.GatewayInfo.NetworkID)
d.Set("enable_snat", router.GatewayInfo.EnableSNAT)
d.Set("region", GetRegion(d, config))
if err := d.Set("availability_zone_hints", router.AvailabilityZoneHints); err != nil {
log.Printf("[DEBUG] Unable to set availability_zone_hints: %s", err)
}
var externalFixedIPs []map[string]string
for _, v := range router.GatewayInfo.ExternalFixedIPs {
externalFixedIPs = append(externalFixedIPs, map[string]string{
"subnet_id": v.SubnetID,
"ip_address": v.IPAddress,
})
}
if err = d.Set("external_fixed_ip", externalFixedIPs); err != nil {
log.Printf("[DEBUG] Unable to set external_fixed_ip: %s", err)
}
return nil
}