-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource_switch_options.go
241 lines (205 loc) · 6.6 KB
/
resource_switch_options.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package junos
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type switchOptionsOptions struct {
vtepSourceIf string
}
func resourceSwitchOptions() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSwitchOptionsCreate,
ReadContext: resourceSwitchOptionsRead,
UpdateContext: resourceSwitchOptionsUpdate,
DeleteContext: resourceSwitchOptionsDelete,
Importer: &schema.ResourceImporter{
State: resourceSwitchOptionsImport,
},
Schema: map[string]*schema.Schema{
"clean_on_destroy": {
Type: schema.TypeBool,
Optional: true,
},
"vtep_source_interface": {
Type: schema.TypeString,
Optional: 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
},
},
},
}
}
func resourceSwitchOptionsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
if sess.junosFakeCreateSetFile != "" {
if err := setSwitchOptions(d, m, nil); err != nil {
return diag.FromErr(err)
}
d.SetId("switch_options")
return nil
}
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
if err := setSwitchOptions(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("create resource junos_switch_options", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
d.SetId("switch_options")
return append(diagWarns, resourceSwitchOptionsReadWJnprSess(d, m, jnprSess)...)
}
func resourceSwitchOptionsRead(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)
return resourceSwitchOptionsReadWJnprSess(d, m, jnprSess)
}
func resourceSwitchOptionsReadWJnprSess(
d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) diag.Diagnostics {
mutex.Lock()
switchOptionsOptions, err := readSwitchOptions(m, jnprSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
fillSwitchOptions(d, switchOptionsOptions)
return nil
}
func resourceSwitchOptionsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
d.Partial(true)
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
if err := delSwitchOptions(m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if err := setSwitchOptions(d, m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("update resource junos_switch_options", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
d.Partial(false)
return append(diagWarns, resourceSwitchOptionsReadWJnprSess(d, m, jnprSess)...)
}
func resourceSwitchOptionsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
if d.Get("clean_on_destroy").(bool) {
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
sess.configLock(jnprSess)
var diagWarns diag.Diagnostics
if err := delSwitchOptions(m, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("delete resource junos_switch_options", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
}
return nil
}
func resourceSwitchOptionsImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
sess := m.(*Session)
jnprSess, err := sess.startNewSession()
if err != nil {
return nil, err
}
defer sess.closeSession(jnprSess)
result := make([]*schema.ResourceData, 1)
switchOptionsOptions, err := readSwitchOptions(m, jnprSess)
if err != nil {
return nil, err
}
fillSwitchOptions(d, switchOptionsOptions)
d.SetId("switch_options")
result[0] = d
return result, nil
}
func setSwitchOptions(d *schema.ResourceData, m interface{}, jnprSess *NetconfObject) error {
sess := m.(*Session)
setPrefix := "set switch-options "
configSet := make([]string, 0)
if v := d.Get("vtep_source_interface").(string); v != "" {
configSet = append(configSet, setPrefix+"vtep-source-interface "+v)
}
return sess.configSet(configSet, jnprSess)
}
func delSwitchOptions(m interface{}, jnprSess *NetconfObject) error {
listLinesToDelete := []string{"vtep-source-interface"}
sess := m.(*Session)
configSet := make([]string, 0)
delPrefix := "delete switch-options "
for _, line := range listLinesToDelete {
configSet = append(configSet,
delPrefix+line)
}
return sess.configSet(configSet, jnprSess)
}
func readSwitchOptions(m interface{}, jnprSess *NetconfObject) (switchOptionsOptions, error) {
sess := m.(*Session)
var confRead switchOptionsOptions
switchOptionsConfig, err := sess.command("show configuration switch-options"+
" | display set relative", jnprSess)
if err != nil {
return confRead, err
}
if switchOptionsConfig != emptyWord {
for _, item := range strings.Split(switchOptionsConfig, "\n") {
if strings.Contains(item, "<configuration-output>") {
continue
}
if strings.Contains(item, "</configuration-output>") {
break
}
itemTrim := strings.TrimPrefix(item, setLineStart)
if strings.HasPrefix(itemTrim, "vtep-source-interface ") {
confRead.vtepSourceIf = strings.TrimPrefix(itemTrim, "vtep-source-interface ")
}
}
}
return confRead, nil
}
func fillSwitchOptions(d *schema.ResourceData, switchOptionsOptions switchOptionsOptions) {
if tfErr := d.Set("vtep_source_interface", switchOptionsOptions.vtepSourceIf); tfErr != nil {
panic(tfErr)
}
}