5
5
"github.com/hashicorp/terraform/helper/schema"
6
6
7
7
"context"
8
+ "fmt"
8
9
"io/ioutil"
9
10
"os"
10
11
"path/filepath"
@@ -32,6 +33,24 @@ func resourceVagrantVM() *schema.Resource {
32
33
ValidateFunc : resourceVagrantVMPathToVagrantfileValidate ,
33
34
},
34
35
36
+ "env" : {
37
+ Description : "Environment variables to pass to the Vagrantfile." ,
38
+ Type : schema .TypeMap ,
39
+ Optional : true ,
40
+ Elem : & schema.Schema {
41
+ Type : schema .TypeString ,
42
+ },
43
+ },
44
+
45
+ "machine_names" : {
46
+ Description : "Names of the vagrant machines from the Vagrantfile. Names are in the same order as ssh_config." ,
47
+ Type : schema .TypeList ,
48
+ Computed : true ,
49
+ Elem : & schema.Schema {
50
+ Type : schema .TypeString ,
51
+ },
52
+ },
53
+
35
54
"ssh_config" : {
36
55
Description : "SSH connection information." ,
37
56
Type : schema .TypeList ,
@@ -91,6 +110,8 @@ func resourceVagrantVMCreate(d *schema.ResourceData, m interface{}) error {
91
110
92
111
cmd := client .Up ()
93
112
cmd .Context = ctx
113
+ cmd .Env = buildEnvironment (d .Get ("env" ).(map [string ]string ))
114
+ cmd .Parallel = true
94
115
if err := cmd .Run (); err != nil {
95
116
return err
96
117
}
@@ -123,6 +144,7 @@ func resourceVagrantVMUpdate(d *schema.ResourceData, m interface{}) error {
123
144
124
145
cmd := client .Reload ()
125
146
cmd .Context = ctx
147
+ cmd .Env = buildEnvironment (d .Get ("env" ).(map [string ]string ))
126
148
if err := cmd .Run (); err != nil {
127
149
return nil
128
150
}
@@ -141,6 +163,7 @@ func resourceVagrantVMDelete(d *schema.ResourceData, m interface{}) error {
141
163
142
164
cmd := client .Destroy ()
143
165
cmd .Context = ctx
166
+ cmd .Env = buildEnvironment (d .Get ("env" ).(map [string ]string ))
144
167
return cmd .Run ()
145
168
}
146
169
@@ -155,6 +178,7 @@ func resourceVagrantVMExists(d *schema.ResourceData, m interface{}) (bool, error
155
178
156
179
cmd := client .Status ()
157
180
cmd .Context = ctx
181
+ cmd .Env = buildEnvironment (d .Get ("env" ).(map [string ]string ))
158
182
if err := cmd .Run (); err != nil {
159
183
return false , err
160
184
}
@@ -207,13 +231,15 @@ func buildId(info map[string]*vagrant.VMInfo) string {
207
231
func readVagrantInfo (ctx context.Context , client * vagrant.VagrantClient , d * schema.ResourceData ) error {
208
232
cmd := client .SSHConfig ()
209
233
cmd .Context = ctx
234
+ cmd .Env = buildEnvironment (d .Get ("env" ).(map [string ]string ))
210
235
if err := cmd .Run (); err != nil {
211
236
return err
212
237
}
213
238
214
239
sshConfigs := make ([]map [string ]string , len (cmd .Configs ))
240
+ keys := make ([]string , len (cmd .Configs ))
215
241
i := 0
216
- for _ , config := range cmd .Configs {
242
+ for key , config := range cmd .Configs {
217
243
sshConfig := make (map [string ]string , 6 )
218
244
sshConfig ["type" ] = "ssh"
219
245
sshConfig ["user" ] = config .User
@@ -224,13 +250,30 @@ func readVagrantInfo(ctx context.Context, client *vagrant.VagrantClient, d *sche
224
250
}
225
251
sshConfig ["agent" ] = "false"
226
252
sshConfigs [i ] = sshConfig
253
+ keys [i ] = key
227
254
i ++
228
255
}
256
+
229
257
d .Set ("ssh_config" , sshConfigs )
258
+ d .Set ("machine_names" , keys )
230
259
231
260
if len (sshConfigs ) == 1 {
232
261
d .SetConnInfo (sshConfigs [0 ])
233
262
}
234
263
235
264
return nil
236
265
}
266
+
267
+ func buildEnvironment (env map [string ]string ) []string {
268
+ if len (env ) == 0 {
269
+ return nil
270
+ }
271
+
272
+ envArray := make ([]string , len (env ))
273
+ i := 0
274
+ for key , value := range env {
275
+ envArray [i ] = fmt .Sprintf ("%v=%v" , key , value )
276
+ i ++
277
+ }
278
+ return envArray
279
+ }
0 commit comments