-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_interfaces_physical_present.go
143 lines (134 loc) · 3.81 KB
/
data_source_interfaces_physical_present.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
package junos
import (
"context"
"encoding/xml"
"fmt"
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type interfacesPresentOpts struct {
interfaceNames []string
interfaceStatuses []map[string]interface{}
}
func dataSourceInterfacesPhysicalPresent() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceInterfacesPhysicalPresentRead,
Schema: map[string]*schema.Schema{
"match_name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if _, err := regexp.Compile(value); err != nil {
errors = append(errors, fmt.Errorf(
"%q for %q is not valid regexp", value, k))
}
return
},
},
"match_admin_up": {
Type: schema.TypeBool,
Optional: true,
},
"match_oper_up": {
Type: schema.TypeBool,
Optional: true,
},
"interface_names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"interface_statuses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"admin_status": {
Type: schema.TypeString,
Computed: true,
},
"oper_status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceInterfacesPhysicalPresentRead(ctx context.Context,
d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
mutex.Lock()
iPresent, err := searchInterfacesPhysicalPresent(d, m, jnprSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
if tfErr := d.Set("interface_names", iPresent.interfaceNames); tfErr != nil {
panic(tfErr)
}
if tfErr := d.Set("interface_statuses", iPresent.interfaceStatuses); tfErr != nil {
panic(tfErr)
}
idString := "match=" + d.Get("match_name").(string)
if d.Get("match_admin_up").(bool) {
idString += idSeparator + "admin_up=true"
}
if d.Get("match_oper_up").(bool) {
idString += idSeparator + "oper_up=true"
}
d.SetId(idString)
return nil
}
func searchInterfacesPhysicalPresent(
d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) (interfacesPresentOpts, error) {
sess := m.(*Session)
var result interfacesPresentOpts
replyData, err := sess.commandXML(rpcGetInterfaceInformationTerse, jnprSess)
if err != nil {
return result, err
}
var iface getInterfaceTerseReply
err = xml.Unmarshal([]byte(replyData), &iface.InterfaceInfo)
if err != nil {
return result, fmt.Errorf("failed to xml unmarshal reply data %s : %w", replyData, err)
}
for _, iFace := range iface.InterfaceInfo.PhysicalInterface {
if mName := d.Get("match_name").(string); mName != "" {
matched, err := regexp.MatchString(mName, strings.Trim(iFace.Name, " \n\t"))
if err != nil {
return result, fmt.Errorf("failed to regexp with %s : %w", mName, err)
}
if !matched {
continue
}
}
if d.Get("match_admin_up").(bool) && strings.Trim(iFace.AdminStatus, " \n\t") != "up" {
continue
}
if d.Get("match_oper_up").(bool) && strings.Trim(iFace.OperStatus, " \n\t") != "up" {
continue
}
result.interfaceNames = append(result.interfaceNames, strings.Trim(iFace.Name, " \n\t"))
result.interfaceStatuses = append(result.interfaceStatuses, map[string]interface{}{
"name": strings.Trim(iFace.Name, " \n\t"),
"admin_status": strings.Trim(iFace.AdminStatus, " \n\t"),
"oper_status": strings.Trim(iFace.OperStatus, " \n\t"),
})
}
return result, nil
}