This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
data_source_openstack_networking_network_v2.go
226 lines (189 loc) · 5.54 KB
/
data_source_openstack_networking_network_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package openstack
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/external"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/vlantransparent"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
)
func dataSourceNetworkingNetworkV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkingNetworkV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"network_id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"matching_subnet_cidr": {
Type: schema.TypeString,
Optional: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
Description: descriptions["tenant_id"],
},
"admin_state_up": {
Type: schema.TypeString,
Computed: true,
},
"shared": {
Type: schema.TypeString,
Computed: true,
},
"external": {
Type: schema.TypeBool,
Optional: true,
},
"availability_zone_hints": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"transparent_vlan": {
Type: schema.TypeBool,
Optional: true,
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"all_tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceNetworkingNetworkV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
// Prepare basic listOpts.
var listOpts networks.ListOptsBuilder
var status string
if v, ok := d.GetOk("status"); ok {
status = v.(string)
}
listOpts = networks.ListOpts{
ID: d.Get("network_id").(string),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
TenantID: d.Get("tenant_id").(string),
Status: status,
}
// Add the external attribute if specified.
if v, ok := d.GetOkExists("external"); ok {
isExternal := v.(bool)
listOpts = external.ListOptsExt{
ListOptsBuilder: listOpts,
External: &isExternal,
}
}
// Add the transparent VLAN attribute if specified.
if v, ok := d.GetOkExists("transparent_vlan"); ok {
isVLANTransparent := v.(bool)
listOpts = vlantransparent.ListOptsExt{
ListOptsBuilder: listOpts,
VLANTransparent: &isVLANTransparent,
}
}
tags := networkV2AttributesTags(d)
if len(tags) > 0 {
listOpts = networks.ListOpts{Tags: strings.Join(tags, ",")}
}
pages, err := networks.List(networkingClient, listOpts).AllPages()
if err != nil {
return err
}
// First extract into a normal networks.Network in order to see if
// there were any results at all.
tmpAllNetworks, err := networks.ExtractNetworks(pages)
if err != nil {
return err
}
if len(tmpAllNetworks) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
type networkWithExternalExt struct {
networks.Network
external.NetworkExternalExt
vlantransparent.TransparentExt
}
var allNetworks []networkWithExternalExt
err = networks.ExtractNetworksInto(pages, &allNetworks)
if err != nil {
return fmt.Errorf("Unable to retrieve openstack_networking_networks_v2: %s", err)
}
var refinedNetworks []networkWithExternalExt
if cidr := d.Get("matching_subnet_cidr").(string); cidr != "" {
for _, n := range allNetworks {
for _, s := range n.Subnets {
subnet, err := subnets.Get(networkingClient, s).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
continue
}
return fmt.Errorf("Unable to retrieve openstack_networking_network_v2 subnet: %s", err)
}
if cidr == subnet.CIDR {
refinedNetworks = append(refinedNetworks, n)
}
}
}
} else {
refinedNetworks = allNetworks
}
if len(refinedNetworks) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(refinedNetworks) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
network := refinedNetworks[0]
if err = d.Set("availability_zone_hints", network.AvailabilityZoneHints); err != nil {
log.Printf("[DEBUG] Unable to set availability_zone_hints for openstack_networking_network_v2 %s: %s", network.ID, err)
}
log.Printf("[DEBUG] Retrieved openstack_networking_network_v2 %s: %+v", network.ID, network)
d.SetId(network.ID)
d.Set("name", network.Name)
d.Set("description", network.Description)
d.Set("admin_state_up", strconv.FormatBool(network.AdminStateUp))
d.Set("shared", strconv.FormatBool(network.Shared))
d.Set("external", network.External)
d.Set("tenant_id", network.TenantID)
d.Set("transparent_vlan", network.VLANTransparent)
d.Set("all_tags", network.Tags)
d.Set("region", GetRegion(d, config))
return nil
}