-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource_interface_st0_unit.go
185 lines (164 loc) · 5.44 KB
/
resource_interface_st0_unit.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
package junos
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
bchk "github.com/jeremmfr/go-utils/basiccheck"
)
func resourceInterfaceSt0Unit() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceInterfaceSt0UnitCreate,
ReadWithoutTimeout: resourceInterfaceSt0UnitRead,
DeleteWithoutTimeout: resourceInterfaceSt0UnitDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceInterfaceSt0UnitImport,
},
}
}
func resourceInterfaceSt0UnitCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
jnprSess, err := sess.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
if err := sess.configLock(ctx, jnprSess); err != nil {
return diag.FromErr(err)
}
var diagWarns diag.Diagnostics
newSt0, err := searchInterfaceSt0UnitToCreate(m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(fmt.Errorf("error to find new st0 unit interface: %w", err))...)
}
if err := sess.configSet([]string{"set interfaces " + newSt0}, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("create resource junos_interface_st0_unit", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(newSt0, m, jnprSess)
if err != nil {
return append(diagWarns, diag.FromErr(err)...)
}
if ncInt {
return append(diagWarns, diag.FromErr(fmt.Errorf("create new %v always disable after commit "+
"=> check your config", newSt0))...)
}
if emptyInt && !setInt {
return append(diagWarns, diag.FromErr(fmt.Errorf("create new st0 unit interface doesn't works, "+
"can't find the new interface %s after commit", newSt0))...)
}
d.SetId(newSt0)
return diagWarns
}
func resourceInterfaceSt0UnitRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
jnprSess, err := sess.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
mutex.Lock()
ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Id(), m, jnprSess)
mutex.Unlock()
if err != nil {
return diag.FromErr(err)
}
if ncInt || (emptyInt && !setInt) {
d.SetId("")
}
return nil
}
func resourceInterfaceSt0UnitDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sess := m.(*Session)
if sess.junosFakeDeleteAlso {
if err := sess.configSet([]string{"delete interfaces " + d.Id()}, nil); err != nil {
return diag.FromErr(err)
}
return nil
}
jnprSess, err := sess.startNewSession(ctx)
if err != nil {
return diag.FromErr(err)
}
defer sess.closeSession(jnprSess)
if err := sess.configLock(ctx, jnprSess); err != nil {
return diag.FromErr(err)
}
var diagWarns diag.Diagnostics
ncInt, emptyInt, _, err := checkInterfaceLogicalNCEmpty(d.Id(), m, jnprSess)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
if !ncInt && !emptyInt {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(fmt.Errorf("interface %s not empty or disable", d.Id()))...)
}
if err := sess.configSet([]string{"delete interfaces " + d.Id()}, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("delete resource junos_interface_st0_unit", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
return diagWarns
}
func resourceInterfaceSt0UnitImport(ctx context.Context, d *schema.ResourceData, m interface{},
) ([]*schema.ResourceData, error) {
sess := m.(*Session)
if !strings.HasPrefix(d.Id(), "st0.") {
return nil, fmt.Errorf("id must be start with 'st0.'")
}
jnprSess, err := sess.startNewSession(ctx)
if err != nil {
return nil, err
}
defer sess.closeSession(jnprSess)
result := make([]*schema.ResourceData, 1)
ncInt, emptyInt, setInt, err := checkInterfaceLogicalNCEmpty(d.Id(), m, jnprSess)
if err != nil {
return nil, err
}
if ncInt {
return nil, fmt.Errorf("interface '%v' is disabled, import is not possible", d.Id())
}
if emptyInt && !setInt {
return nil, fmt.Errorf("don't find interface with id '%v'"+
" (id must be the name of st0 unit interface <st0.?>)", d.Id())
}
result[0] = d
return result, nil
}
func searchInterfaceSt0UnitToCreate(m interface{}, jnprSess *NetconfObject) (string, error) {
sess := m.(*Session)
st0, err := sess.command("show interfaces st0 terse", jnprSess)
if err != nil {
return "", err
}
st0Line := strings.Split(st0, "\n")
st0int := make([]string, 0)
for _, line := range st0Line {
if strings.HasPrefix(line, "st0.") {
lineSplit := strings.Split(line, " ")
st0int = append(st0int, lineSplit[0])
}
}
for i := 0; i <= 1073741823; i++ {
if !bchk.StringInSlice("st0."+strconv.Itoa(i), st0int) {
return "st0." + strconv.Itoa(i), nil
}
}
return "", fmt.Errorf("error for find st0 unit to create")
}