-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_interface_logical_info.go
160 lines (150 loc) · 4.31 KB
/
data_source_interface_logical_info.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
package junos
import (
"context"
"encoding/xml"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type interfaceLogicalInfo struct {
adminStatus string
name string
operStatus string
familyInet []map[string]interface{}
familyInet6 []map[string]interface{}
}
func dataSourceInterfaceLogicalInfo() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceInterfaceLogicalInfoRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if strings.Count(value, ".") != 1 {
errors = append(errors, fmt.Errorf(
"%q in %q need to have 1 dot", value, k))
}
return
},
},
"admin_status": {
Type: schema.TypeString,
Computed: true,
},
"oper_status": {
Type: schema.TypeString,
Computed: true,
},
"family_inet": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address_cidr": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"family_inet6": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address_cidr": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
}
}
func dataSourceInterfaceLogicalInfoRead(ctx context.Context, d *schema.ResourceData, m interface{},
) diag.Diagnostics {
clt := m.(*Client)
junSess, err := clt.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer clt.closeSession(junSess)
mutex.Lock()
ifaceInfo, err := readInterfaceLogicalInfo(d, clt, junSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
fillInterfaceLogicalInfo(d, ifaceInfo)
d.SetId(ifaceInfo.name)
return nil
}
func readInterfaceLogicalInfo(d *schema.ResourceData, clt *Client, junSess *junosSession,
) (interfaceLogicalInfo, error) {
var result interfaceLogicalInfo
replyData, err := clt.commandXML(fmt.Sprintf(rpcGetInterfaceInformationTerse, d.Get("name").(string)), junSess)
if err != nil {
return result, err
}
var iface getLogicalInterfaceTerseReply
err = xml.Unmarshal([]byte(replyData), &iface.InterfaceInfo)
if err != nil {
return result, fmt.Errorf("failed to xml unmarshal reply data '%s': %w", replyData, err)
}
if len(iface.InterfaceInfo.LogicalInterface) == 0 {
return result, fmt.Errorf("logical-interface not found in xml: %v", replyData)
}
ifaceInfo := iface.InterfaceInfo.LogicalInterface[0]
result.name = strings.TrimSpace(ifaceInfo.Name)
result.adminStatus = strings.TrimSpace(ifaceInfo.AdminStatus)
result.operStatus = strings.TrimSpace(ifaceInfo.OperStatus)
for _, family := range ifaceInfo.AddressFamily {
switch strings.TrimSpace(family.Name) {
case inetW:
if len(result.familyInet) == 0 {
result.familyInet = append(result.familyInet, map[string]interface{}{
"address_cidr": make([]string, 0, len(family.Address)),
})
}
for _, address := range family.Address {
result.familyInet[0]["address_cidr"] = append(
result.familyInet[0]["address_cidr"].([]string),
strings.TrimSpace(address.Local),
)
}
case inet6W:
if len(result.familyInet6) == 0 {
result.familyInet6 = append(result.familyInet6, map[string]interface{}{
"address_cidr": make([]string, 0, len(family.Address)),
})
}
for _, address := range family.Address {
result.familyInet6[0]["address_cidr"] = append(
result.familyInet6[0]["address_cidr"].([]string),
strings.TrimSpace(address.Local),
)
}
}
}
return result, nil
}
func fillInterfaceLogicalInfo(d *schema.ResourceData, ifaceInfo interfaceLogicalInfo) {
if tfErr := d.Set("admin_status", ifaceInfo.adminStatus); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("oper_status", ifaceInfo.operStatus); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("family_inet", ifaceInfo.familyInet); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("family_inet6", ifaceInfo.familyInet6); tfErr != nil {
panic(tfErr)
}
}