-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_quantum_file.go
71 lines (60 loc) · 1.55 KB
/
data_source_quantum_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
package main
import (
"crypto/sha1"
"encoding/hex"
"io/ioutil"
"os"
"path"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceQuantumFile() *schema.Resource {
return &schema.Resource{
Read: resourceLocalFileRead,
Schema: map[string]*schema.Schema{
"content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"sensitive_content"},
},
"sensitive_content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
ConflictsWith: []string{"content"},
},
"filename": {
Type: schema.TypeString,
Description: "Path to the output file",
Required: true,
ForceNew: true,
},
},
}
}
func resourceLocalFileRead(d *schema.ResourceData, _ interface{}) error {
content := resourceLocalFileContent(d)
destination := d.Get("filename").(string)
destinationDir := path.Dir(destination)
if _, err := os.Stat(destinationDir); err != nil {
if err := os.MkdirAll(destinationDir, 0777); err != nil {
return err
}
}
if err := ioutil.WriteFile(destination, []byte(content), 0777); err != nil {
return err
}
checksum := sha1.Sum([]byte(content))
d.SetId(hex.EncodeToString(checksum[:]))
return nil
}
func resourceLocalFileContent(d *schema.ResourceData) string {
content := d.Get("content")
sensitiveContent, sensitiveSpecified := d.GetOk("sensitive_content")
useContent := content.(string)
if sensitiveSpecified {
useContent = sensitiveContent.(string)
}
return useContent
}