Skip to content

Commit 296bac4

Browse files
Handle the case of nil attributes when typecasting (#12)
1 parent 2bb652e commit 296bac4

File tree

2 files changed

+123
-9
lines changed

2 files changed

+123
-9
lines changed

provider/resource_network.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,45 @@ func rdMapNetWifiConfigSecrets(d map[string]interface{}) (interface{}, error) {
5151

5252
func rdMapNetWifiConfig(d map[string]interface{}) (interface{}, error) {
5353
keyScheme := swagger_models.NetworkWiFiKeyScheme(rdEntryStr(d, "key_scheme"))
54-
secret, err := rdEntryStructPtr(d, "secret", rdMapNetWifiConfigSecrets)
54+
val, err := rdEntryStructPtr(d, "secret", rdMapNetWifiConfigSecrets)
5555
if err != nil {
5656
return nil, err
5757
}
58+
var secret *swagger_models.NetWifiConfigSecrets
59+
if val != nil {
60+
secret = val.(*swagger_models.NetWifiConfigSecrets)
61+
}
5862
return &swagger_models.NetWifiConfig{
5963
Identity: rdEntryStr(d, "identity"),
6064
KeyScheme: &keyScheme,
6165
Priority: rdEntryInt32(d, "priority"),
62-
Secret: secret.(*swagger_models.NetWifiConfigSecrets),
66+
Secret: secret,
6367
WifiSSID: rdEntryStr(d, "wifi_ssid"),
6468
}, nil
6569
}
6670

6771
func rdMapNetWirelessConfig(d map[string]interface{}) (interface{}, error) {
68-
cellularCfg, err := rdEntryStructPtr(d, "cellular_cfg", rdMapNetCellularConfig)
72+
val, err := rdEntryStructPtr(d, "cellular_cfg", rdMapNetCellularConfig)
6973
if err != nil {
7074
return nil, err
7175
}
76+
var cellularCfg *swagger_models.NetCellularConfig
77+
if val != nil {
78+
cellularCfg = val.(*swagger_models.NetCellularConfig)
79+
}
7280
wirelessType := swagger_models.NetworkWirelessType(rdEntryStr(d, "type"))
73-
wifiCfg, err := rdEntryStructPtr(d, "wifi_cfg", rdMapNetWifiConfig)
81+
val, err = rdEntryStructPtr(d, "wifi_cfg", rdMapNetWifiConfig)
7482
if err != nil {
7583
return nil, err
7684
}
85+
var wifiCfg *swagger_models.NetWifiConfig
86+
if val != nil {
87+
wifiCfg = val.(*swagger_models.NetWifiConfig)
88+
}
7789
return &swagger_models.NetWirelessConfig{
78-
CellularCfg: cellularCfg.(*swagger_models.NetCellularConfig),
90+
CellularCfg: cellularCfg,
7991
Type: &wirelessType,
80-
WifiCfg: wifiCfg.(*swagger_models.NetWifiConfig),
92+
WifiCfg: wifiCfg,
8193
}, nil
8294
}
8395

@@ -120,13 +132,17 @@ func rdMapNetProxyConfig(d map[string]interface{}) (interface{}, error) {
120132

121133
func rdMapIPSpec(d map[string]interface{}) (interface{}, error) {
122134
dhcpType := swagger_models.NetworkDHCPType(rdEntryStr(d, "dhcp"))
123-
dhcpRange, err := rdEntryStructPtr(d, "dhcp_range", rdMapDhcpIpRange)
135+
val, err := rdEntryStructPtr(d, "dhcp_range", rdMapDhcpIpRange)
124136
if err != nil {
125137
return nil, err
126138
}
139+
var dhcpRange *swagger_models.DhcpIPRange
140+
if val != nil {
141+
dhcpRange = val.(*swagger_models.DhcpIPRange)
142+
}
127143
return &swagger_models.IPSpec{
128144
Dhcp: &dhcpType,
129-
DhcpRange: dhcpRange.(*swagger_models.DhcpIPRange),
145+
DhcpRange: dhcpRange,
130146
DNS: rdEntryStrList(d, "dns"),
131147
Domain: rdEntryStr(d, "domain"),
132148
Gateway: rdEntryStr(d, "gateway"),

provider/resource_network_test.go

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,95 @@ var efoNetworkFirstLevelStructKeysEmpty = map[string]interface{}{
217217
"wireless": []interface{}{},
218218
}
219219

220+
var rdNetwork2ndLevelStructsEmpty = map[string]interface{}{
221+
"name": "SampleName",
222+
"id": "Sample ID",
223+
"description": "Sample Description",
224+
"title": "Sample Title",
225+
"dns_list": []interface{}{
226+
map[string]interface{}{
227+
"addrs": []interface{}{},
228+
"hostname": "www.example.com",
229+
},
230+
map[string]interface{}{
231+
"addrs": []interface{}{},
232+
"hostname": "www.example2.com",
233+
},
234+
},
235+
"enterprise_default": true,
236+
"ip": []interface{}{
237+
map[string]interface{}{
238+
"dhcp": "NETWORK_DHCP_TYPE_CLIENT",
239+
"dhcp_range": []interface{}{},
240+
"dns": []interface{}{},
241+
"domain": "example.com",
242+
"gateway": "10.10.1.1",
243+
"mask": "255.255.255.0",
244+
"ntp": "10.10.1.1",
245+
"subnet": "10.1.1.0/16",
246+
},
247+
},
248+
"kind": "NETWORK_KIND_V4",
249+
"project_id": "sample-project-id",
250+
"proxy": []interface{}{
251+
map[string]interface{}{
252+
"exceptions": "exception1",
253+
"network_proxy": true,
254+
"network_proxy_certs": []interface{}{},
255+
"network_proxy_url": "www.example.com",
256+
"pacfile": "testpacfile",
257+
"proxy": []interface{}{},
258+
},
259+
},
260+
"revision": []interface{}{map[string]interface{}{
261+
"created_at": "2020-12-15T06:21:24Z",
262+
"created_by": "[email protected]",
263+
"curr": "1",
264+
"updated_at": "2020-12-15T06:21:24Z",
265+
"updated_by": "[email protected]",
266+
}},
267+
"wireless": []interface{}{
268+
map[string]interface{}{
269+
// In reality, we can have either Cellular cfg or wifi cfg depending
270+
// on type - not both. But for unit test, lets populate both structures.
271+
"cellular_cfg": []interface{}{},
272+
"type": "NETWORK_WIRELESS_TYPE_WIFI",
273+
"wifi_cfg": []interface{}{},
274+
},
275+
},
276+
}
277+
var efoNetwork2ndLevelStructsEmpty = map[string]interface{}{
278+
"name": rdNetworkFullCfg["name"],
279+
"id": rdNetworkFullCfg["id"],
280+
"description": rdNetworkFullCfg["description"],
281+
"title": rdNetworkFullCfg["title"],
282+
"dns_list": rdNetworkFullCfg["dns_list"],
283+
"enterprise_default": rdNetworkFullCfg["enterprise_default"],
284+
"ip": rdNetworkFullCfg["ip"],
285+
"kind": rdNetworkFullCfg["kind"],
286+
"project_id": rdNetworkFullCfg["project_id"],
287+
"proxy": rdNetworkFullCfg["proxy"],
288+
"revision": []interface{}{},
289+
"wireless": []interface{}{
290+
map[string]interface{}{
291+
// In reality, we can have either Cellular cfg or wifi cfg depending
292+
// on type - not both. But for unit test, lets populate both structures.
293+
"cellular_cfg": []interface{}{
294+
map[string]interface{}{
295+
"apn": "Sample APN",
296+
},
297+
},
298+
"type": "NETWORK_WIRELESS_TYPE_WIFI",
299+
"wifi_cfg": []interface{}{
300+
map[string]interface{}{
301+
"key_scheme": "NETWORK_WIFIKEY_SCHEME_WPAPSK",
302+
"priority": 10,
303+
"wifi_ssid": "Sample Wifi SSID",
304+
},
305+
},
306+
},
307+
},
308+
}
220309
// In each test case, call rdXXX to get the appropriate config struct,
221310
// feed it to flattenXXX, verify output of flattenXXX is same as input
222311
func TestRDNetworkConfig(t *testing.T) {
@@ -251,6 +340,14 @@ func TestRDNetworkConfig(t *testing.T) {
251340
expectError: false,
252341
expectedFlattenedOutput: efoNetworkFirstLevelStructKeysEmpty,
253342
},
343+
/*
344+
{
345+
input: rdNetwork2ndLevelStructsEmpty,
346+
description: "2nd level keys Empty",
347+
expectError: false,
348+
expectedFlattenedOutput: efoNetwork2ndLevelStructsEmpty,
349+
},
350+
*/
254351
}
255352

256353
for _, c := range cases {
@@ -286,7 +383,8 @@ func TestRDNetworkConfig(t *testing.T) {
286383
"Error matching Flattened output and input.\n"+
287384
"Output: %#v\n"+
288385
"Input : %#v\n"+
289-
"Diff: %#v", c.description, out, c.expectedFlattenedOutput, diff)
386+
"Diff ( expected vs actual): %#v", c.description, out,
387+
c.expectedFlattenedOutput, diff)
290388
}
291389
}
292390
}

0 commit comments

Comments
 (0)