/
data_source_aviatrix_device_interfaces.go
67 lines (57 loc) · 1.84 KB
/
data_source_aviatrix_device_interfaces.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
package aviatrix
import (
"fmt"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v3/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceAviatrixDeviceInterfaces() *schema.Resource {
return &schema.Resource{
Read: dataSourceAviatrixDeviceInterfaceConfigRead,
Schema: map[string]*schema.Schema{
"device_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of device.",
},
"wan_interfaces": {
Type: schema.TypeList,
Computed: true,
Description: "List of WAN primary interface and WAN primary interface public IP.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"wan_primary_interface": {
Type: schema.TypeString,
Computed: true,
Description: "WAN primary interface of the device.",
},
"wan_primary_interface_public_ip": {
Type: schema.TypeString,
Computed: true,
Description: "WAN primary interface public IP address.",
},
},
},
},
},
}
}
func dataSourceAviatrixDeviceInterfaceConfigRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
deviceName := d.Get("device_name").(string)
deviceWanInterfaces, err := client.GetDeviceInterfaces(deviceName)
if err != nil {
return fmt.Errorf("couldn't get device wan interfaces: %s", err)
}
var wanInterfaces []map[string]interface{}
for _, wanInterface := range *deviceWanInterfaces {
wI := make(map[string]interface{})
wI["wan_primary_interface"] = wanInterface.Interface
wI["wan_primary_interface_public_ip"] = wanInterface.IP
wanInterfaces = append(wanInterfaces, wI)
}
if err = d.Set("wan_interfaces", wanInterfaces); err != nil {
return fmt.Errorf("couldn't set wan_interfaces: %s", err)
}
d.SetId(deviceName)
return nil
}