/
data_source_shell_script.go
105 lines (95 loc) · 2.57 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
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
package shell
import (
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/rs/xid"
)
func dataSourceShellScript() *schema.Resource {
return &schema.Resource{
Read: dataSourceShellScriptRead,
Schema: map[string]*schema.Schema{
"lifecycle_commands": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"read": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"environment": {
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeString,
},
"sensitive_environment": {
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeString,
Sensitive: true,
},
"interpreter": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"working_directory": {
Type: schema.TypeString,
Optional: true,
Default: ".",
},
"output": {
Type: schema.TypeMap,
Computed: true,
Elem: schema.TypeString,
},
},
}
}
func dataSourceShellScriptRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading shell script data resource...")
l := d.Get("lifecycle_commands").([]interface{})
c := l[0].(map[string]interface{})
value := c["read"]
command := value.(string)
client := meta.(*Client)
envVariables := getEnvironmentVariables(client, d)
environment := formatEnvironmentVariables(envVariables)
sensitiveEnvVariables := getSensitiveEnvironmentVariables(client, d)
sensitiveEnvironment := formatEnvironmentVariables(sensitiveEnvVariables)
interpreter := getInterpreter(client, d)
workingDirectory := d.Get("working_directory").(string)
enableParallelism := client.config.EnableParallelism
//we don't care about previous output for data sources
previousOutput := make(map[string]string)
commandConfig := &CommandConfig{
Command: command,
Environment: environment,
SensitiveEnvironment: sensitiveEnvironment,
WorkingDirectory: workingDirectory,
Interpreter: interpreter,
Action: ActionRead,
PreviousOutput: previousOutput,
EnableParallelism: enableParallelism,
}
output, err := runCommand(commandConfig)
if err != nil {
return err
}
if output == nil {
log.Printf("[DEBUG] Output from read operation was nil. Marking resource for deletion.")
d.SetId("")
return nil
}
d.Set("output", output)
//create random uuid for the id
id := xid.New().String()
d.SetId(id)
return nil
}