/
data_source_shell_script.go
62 lines (54 loc) · 1.3 KB
/
data_source_shell_script.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
package sys
import (
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceShellScript() *schema.Resource {
return &schema.Resource{
Read: dataSourceShellScriptRead,
Schema: map[string]*schema.Schema{
"working_directory": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "",
},
"shell": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "/bin/sh",
},
"read": {
Type: schema.TypeString,
Description: "Shell script to read the value",
Required: true,
ForceNew: true,
},
"content": {
Type: schema.TypeString,
Computed: true,
},
"content_base64": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceShellScriptRead(d *schema.ResourceData, _ interface{}) error {
script := d.Get("read")
cwd := d.Get("working_directory")
shell := d.Get("shell").(string)
content, err := resourceShellScriptRun(cwd, shell, script.(string), true)
if err != nil {
return err
}
d.Set("content", string(content))
d.Set("content_base64", base64.StdEncoding.EncodeToString([]byte(content)))
checksum := sha1.Sum([]byte(content))
d.SetId(hex.EncodeToString(checksum[:]))
return nil
}