/
file.go
126 lines (106 loc) · 2.93 KB
/
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
package linux
import (
"crypto/md5"
"fmt"
"github.com/google/uuid"
"github.com/hashicorp/terraform/communicator/remote"
"github.com/hashicorp/terraform/helper/schema"
"github.com/sam-myers/terraform-provider-linux/linux/fileutil"
"github.com/sam-myers/terraform-provider-linux/linux/manager"
"io"
"strings"
)
func linuxFile() *schema.Resource {
return &schema.Resource{
Read: linuxFileRead,
Create: linuxFileCreate,
Delete: linuxFileDelete,
Schema: map[string]*schema.Schema{
"connection_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
Sensitive: true,
},
"content": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The content to copy to the destination",
},
"destination": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The destination path. It must be specified as an absolute path",
},
// Computed
"hash_md5": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
Description: "MD5 hash",
},
},
}
}
func linuxFileCreate(d *schema.ResourceData, meta interface{}) error {
destination := d.Get("destination").(string)
content := d.Get("content").(string)
contentReader := strings.NewReader(content)
comm, err := manager.GetCommunicator(d.Get("connection_id").(string))
if err != nil {
return err
}
err = comm.Upload(destination, contentReader)
if err != nil {
return fmt.Errorf("uploading file: %s", err)
}
d.SetId(uuid.New().String())
hash := md5.New()
_, _ = io.WriteString(hash, content)
setOrPanic(d, "hash_md5", fmt.Sprintf("%x", hash.Sum(nil)))
return nil
}
func linuxFileDelete(d *schema.ResourceData, meta interface{}) error {
comm, err := manager.GetCommunicator(d.Get("connection_id").(string))
destination := d.Get("destination").(string)
rmCmd := fmt.Sprintf(`rm -f "%s"`, destination)
command := remote.Cmd{
Command: rmCmd,
}
err = comm.Start(&command)
if err != nil {
return fmt.Errorf("deleting file: %s", err)
}
d.SetId("")
return nil
}
func linuxFileRead(d *schema.ResourceData, meta interface{}) error {
comm, err := manager.GetCommunicator(d.Get("connection_id").(string))
if err != nil {
// Don't change state if read fails
return nil
}
destination := d.Get("destination").(string)
exists, err := fileutil.Exists(comm, destination)
if err != nil {
return err
}
// File is deleted, so destroy the resource
if !exists {
d.SetId("")
}
oldHash := d.Get("hash_md5").(string)
newHash, err := fileutil.HashMD5(comm, destination)
if err != nil {
return fmt.Errorf("getting md5: %s", err)
}
changedOnRemoteMessage := "changed on remote"
if oldHash != newHash && d.Get("content").(string) == changedOnRemoteMessage {
setOrPanic(d, "content", changedOnRemoteMessage+" :) nice try")
} else if oldHash != newHash {
setOrPanic(d, "content", changedOnRemoteMessage)
}
return nil
}