-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
resource_null_commit_file.go
127 lines (112 loc) · 3.43 KB
/
resource_null_commit_file.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
package junos
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceNullCommitFile() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceNullCommitFileCreate,
ReadWithoutTimeout: resourceNullCommitFileRead,
DeleteWithoutTimeout: resourceNullCommitFileDelete,
Schema: map[string]*schema.Schema{
"filename": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"append_lines": {
Type: schema.TypeList,
ForceNew: true,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"clear_file_after_commit": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"triggers": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: nil,
},
},
}
}
func resourceNullCommitFileCreate(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
fileName := d.Get("filename").(string)
configSet, err := readNullCommitFile(fileName)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
for _, v := range d.Get("append_lines").([]interface{}) {
configSet = append(configSet, v.(string))
}
if err := sess.configSet(configSet, jnprSess); err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
warns, err := sess.commitConf("commit a file with resource junos_null_commit_file", jnprSess)
appendDiagWarns(&diagWarns, warns)
if err != nil {
appendDiagWarns(&diagWarns, sess.configClear(jnprSess))
return append(diagWarns, diag.FromErr(err)...)
}
d.SetId(fileName)
if d.Get("clear_file_after_commit").(bool) {
if err := cleanNullCommitFile(fileName, sess); err != nil {
return append(diagWarns, diag.FromErr(err)...)
}
}
return diagWarns
}
func resourceNullCommitFileRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}
func resourceNullCommitFileDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
d.SetId("")
return nil
}
func readNullCommitFile(filename string) ([]string, error) {
if err := replaceTildeToHomeDir(&filename); err != nil {
return []string{}, err
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
return []string{}, fmt.Errorf("file '%s' doesn't exist", filename)
}
fileReadByte, err := ioutil.ReadFile(filename)
if err != nil {
return []string{}, fmt.Errorf("could not read file '%s': %w", filename, err)
}
return strings.Split(string(fileReadByte), "\n"), nil
}
func cleanNullCommitFile(filename string, sess *Session) error {
if err := replaceTildeToHomeDir(&filename); err != nil {
return err
}
f, err := os.OpenFile(filename, os.O_TRUNC, os.FileMode(sess.junosFilePermission))
if err != nil {
return fmt.Errorf("could not open file '%s' to truncate after commit: %w", filename, err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("could not close file handler for '%s' after truncation: %w", filename, err)
}
return nil
}