Skip to content

Commit 788e57a

Browse files
committed
Implement inline declaration of Terraform variables via command-line.
1 parent d746e0f commit 788e57a

File tree

8 files changed

+61
-9
lines changed

8 files changed

+61
-9
lines changed

CHANGES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## v0.2
44

5+
New features:
6+
7+
* You can now choose to specify Terraform variables on the command line using `--terraform-variable` (if reading them from a file using `--terraform-variables-from` is undesirable)
8+
9+
Breaking changes:
10+
11+
* The `--terraform-variables` command-line argument has been renamed to `--terraform-variables-from`.
12+
513
Bug fixes:
614

715
* Fetching source from a directory now works correctly (#1)

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Well now you do. If you can express it as a Terraform configuration, you can use
1010
The driver accepts the following arguments:
1111

1212
* `--terraform-config` (Required) - The path (or URL) of the Terraform configuration to use
13-
* `--terraform-additional-variables` (Optional) - An optional file containing the JSON that represents additional variables for the Terraform configuration
13+
* `--terraform-variable` (Optional) - One or more items of the form "name=value" representing additional variables for the Terraform configuration
14+
For example: `--terraform-variable variable1=foo --terraform-variable variable2=bar`
15+
* `--terraform-variables-from` (Optional) - An optional file containing the JSON that represents additional variables for the Terraform configuration
1416
* `--terraform-refresh` (Optional) - A flag which, if specified, will cause the driver to refresh the configuration after applying it
1517

1618
### Terraform configuration

config_variables.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os"
1111
"path"
12+
"strings"
1213

1314
"github.com/docker/machine/libmachine/log"
1415
)
@@ -49,8 +50,38 @@ func (driver *Driver) readVariables() error {
4950
return nil
5051
}
5152

52-
// Read Terraform variables from the additional variables file passed in on the command-line.
53+
// Read all additional variables (from the command line, or from a file)
5354
func (driver *Driver) readAdditionalVariables() error {
55+
err := driver.readAdditionalVariablesInline()
56+
if err != nil {
57+
return err
58+
}
59+
60+
return driver.readAdditionalVariablesFile()
61+
}
62+
63+
// Read additional variables passed in on the command-line (--terraform-variable a=b --terraform-variable c=d)
64+
func (driver *Driver) readAdditionalVariablesInline() error {
65+
for _, additionalVariable := range driver.AdditionalVariablesInline {
66+
variableNameAndValue := strings.SplitN(additionalVariable, "=", 2)
67+
if len(variableNameAndValue) != 2 {
68+
return fmt.Errorf("Invalid format for additional variable '%s", additionalVariable)
69+
}
70+
71+
// Don't overwrite existing entries.
72+
_, variableExists := driver.ConfigVariables[variableNameAndValue[0]]
73+
if variableExists {
74+
continue
75+
}
76+
77+
driver.ConfigVariables[variableNameAndValue[0]] = variableNameAndValue[1]
78+
}
79+
80+
return nil
81+
}
82+
83+
// Read Terraform variables from the file passed in on the command-line.
84+
func (driver *Driver) readAdditionalVariablesFile() error {
5485
if driver.AdditionalVariablesFile == "" {
5586
return nil // Nothing to do
5687
}

driver.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type Driver struct {
3636
// An optional file containing the JSON that represents additional variables for the Terraform configuration
3737
AdditionalVariablesFile string
3838

39+
// Optional "name=value" items that represent additional variables for the Terraform configuration
40+
AdditionalVariablesInline []string
41+
3942
// Refresh the configuration after applying it
4043
RefreshAfterApply bool
4144

@@ -60,9 +63,14 @@ func (driver *Driver) GetCreateFlags() []mcnflag.Flag {
6063
Usage: "The path (or URL) of the Terraform configuration",
6164
Value: "",
6265
},
66+
mcnflag.StringSliceFlag{
67+
Name: "terraform-variable",
68+
Usage: "Additional variable(s) for the Terraform configuration (in the form name=value)",
69+
Value: []string{},
70+
},
6371
mcnflag.StringFlag{
64-
Name: "terraform-additional-variables",
65-
Usage: "An optional file containing the JSON that represents additional variables for the Terraform configuration",
72+
Name: "terraform-variables-from",
73+
Usage: "The name of a file containing the JSON that represents additional variables for the Terraform configuration",
6674
Value: "",
6775
},
6876
mcnflag.BoolFlag{
@@ -106,7 +114,10 @@ func (driver *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
106114

107115
driver.ConfigSource = flags.String("terraform-config")
108116
driver.ConfigVariables = make(map[string]interface{})
109-
driver.AdditionalVariablesFile = flags.String("terraform-additional-variables")
117+
118+
driver.AdditionalVariablesInline = flags.StringSlice("terraform-variable")
119+
driver.AdditionalVariablesFile = flags.String("terraform-variables")
120+
110121
driver.RefreshAfterApply = flags.Bool("terraform-refresh")
111122

112123
driver.SSHPort = flags.Int("terraform-ssh-port")

examples/aws/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export AWS_ACCESS_KEY_ID=my_access_key_id
2525
export AWS_SECRET_KEY=my_secret_key
2626
docker-machine create --driver terraform \
2727
--terraform-config $PWD/main.tf \
28-
--terraform-additional-variables $PWD/additional-variables.json \
28+
--terraform-additional-variables-file $PWD/additional-variables.json \
2929
hello-aws
3030
```
3131

examples/ddcloud/private_ip/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export MCP_USERNAME=my_username
3030
export MCP_PASSWORD=my_password
3131
docker-machine create --driver terraform \
3232
--terraform-config $PWD/main.tf \
33-
--terraform-additional-variables $PWD/additional-variables.json \
33+
--terraform-variables-from $PWD/additional-variables.json \
3434
hello-ddcloud
3535
```
3636

examples/ddcloud/public_ip/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export MCP_USERNAME=my_username
3232
export MCP_PASSWORD=my_password
3333
docker-machine create --driver terraform \
3434
--terraform-config $PWD/main.tf \
35-
--terraform-additional-variables $PWD/additional-variables.json \
35+
--terraform-variables-from $PWD/additional-variables.json \
3636
hello-ddcloud
3737
```
3838

examples/digital_ocean/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export DIGITALOCEAN_TOKEN=my_token
2121
cd examples/digital_ocean
2222
docker-machine create --driver terraform \
2323
--terraform-config $PWD/main.tf \
24-
--terraform-additional-variables $PWD/additional-variables.json \
24+
--terraform-variables-from $PWD/additional-variables.json \
2525
hello-digital-ocean
2626
```
2727

0 commit comments

Comments
 (0)