command/push: prefer Atlas over local, add -set flag
This commit is contained in:
parent
8b0deba35b
commit
dc40f044f0
|
@ -24,6 +24,7 @@ func (c *PushCommand) Run(args []string) int {
|
|||
var atlasAddress, atlasToken string
|
||||
var archiveVCS, moduleUpload bool
|
||||
var name string
|
||||
var set []string
|
||||
args = c.Meta.process(args, true)
|
||||
cmdFlags := c.Meta.flagSet("push")
|
||||
cmdFlags.StringVar(&atlasAddress, "atlas-address", "", "")
|
||||
|
@ -32,11 +33,18 @@ func (c *PushCommand) Run(args []string) int {
|
|||
cmdFlags.BoolVar(&moduleUpload, "upload-modules", true, "")
|
||||
cmdFlags.StringVar(&name, "name", "", "")
|
||||
cmdFlags.BoolVar(&archiveVCS, "vcs", true, "")
|
||||
cmdFlags.Var((*FlagStringSlice)(&set), "set", "")
|
||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
// Make a map of the set values
|
||||
setMap := make(map[string]struct{}, len(set))
|
||||
for _, v := range set {
|
||||
setMap[v] = struct{}{}
|
||||
}
|
||||
|
||||
// The pwd is used for the configuration path if one is not given
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
|
@ -132,10 +140,10 @@ func (c *PushCommand) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
for k, v := range vars {
|
||||
// Local variables override remote ones
|
||||
if _, exists := ctx.Variables()[k]; exists {
|
||||
if _, ok := setMap[k]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
ctx.SetVariable(k, v)
|
||||
}
|
||||
|
||||
|
@ -211,6 +219,10 @@ Options:
|
|||
-token=<token> Access token to use to upload. If blank or unspecified,
|
||||
the ATLAS_TOKEN environmental variable will be used.
|
||||
|
||||
-set=foo Variable keys that should overwrite values in Atlas.
|
||||
Otherwise, variables already set in Atlas will overwrite
|
||||
local values. This flag can be repeated.
|
||||
|
||||
-var 'foo=bar' Set a variable in the Terraform configuration. This
|
||||
flag can be set multiple times.
|
||||
|
||||
|
|
|
@ -179,7 +179,9 @@ func TestPush_inputPartial(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPush_inputTfvars(t *testing.T) {
|
||||
// This tests that the push command will override Atlas variables
|
||||
// if requested.
|
||||
func TestPush_localOverride(t *testing.T) {
|
||||
// Disable test mode so input would be asked and setup the
|
||||
// input reader/writers.
|
||||
test = false
|
||||
|
@ -219,6 +221,154 @@ func TestPush_inputTfvars(t *testing.T) {
|
|||
client: client,
|
||||
}
|
||||
|
||||
path := testFixturePath("push-tfvars")
|
||||
args := []string{
|
||||
"-var-file", path + "/terraform.tfvars",
|
||||
"-vcs=false",
|
||||
"-set=foo",
|
||||
path,
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
actual := testArchiveStr(t, archivePath)
|
||||
expected := []string{
|
||||
".terraform/",
|
||||
".terraform/terraform.tfstate",
|
||||
"main.tf",
|
||||
"terraform.tfvars",
|
||||
}
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
|
||||
if client.UpsertOptions.Name != "foo" {
|
||||
t.Fatalf("bad: %#v", client.UpsertOptions)
|
||||
}
|
||||
|
||||
variables := map[string]string{
|
||||
"foo": "bar",
|
||||
"bar": "foo",
|
||||
}
|
||||
if !reflect.DeepEqual(client.UpsertOptions.Variables, variables) {
|
||||
t.Fatalf("bad: %#v", client.UpsertOptions)
|
||||
}
|
||||
}
|
||||
|
||||
// This tests that the push command prefers Atlas variables over
|
||||
// local ones.
|
||||
func TestPush_preferAtlas(t *testing.T) {
|
||||
// Disable test mode so input would be asked and setup the
|
||||
// input reader/writers.
|
||||
test = false
|
||||
defer func() { test = true }()
|
||||
defaultInputReader = bytes.NewBufferString("nope\n")
|
||||
defaultInputWriter = new(bytes.Buffer)
|
||||
|
||||
tmp, cwd := testCwd(t)
|
||||
defer testFixCwd(t, tmp, cwd)
|
||||
|
||||
// Create remote state file, this should be pulled
|
||||
conf, srv := testRemoteState(t, testState(), 200)
|
||||
defer srv.Close()
|
||||
|
||||
// Persist local remote state
|
||||
s := terraform.NewState()
|
||||
s.Serial = 5
|
||||
s.Remote = conf
|
||||
testStateFileRemote(t, s)
|
||||
|
||||
// Path where the archive will be "uploaded" to
|
||||
archivePath := testTempFile(t)
|
||||
defer os.Remove(archivePath)
|
||||
|
||||
client := &mockPushClient{File: archivePath}
|
||||
// Provided vars should override existing ones
|
||||
client.GetResult = map[string]string{
|
||||
"foo": "old",
|
||||
}
|
||||
ui := new(cli.MockUi)
|
||||
c := &PushCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(testProvider()),
|
||||
Ui: ui,
|
||||
},
|
||||
|
||||
client: client,
|
||||
}
|
||||
|
||||
path := testFixturePath("push-tfvars")
|
||||
args := []string{
|
||||
"-var-file", path + "/terraform.tfvars",
|
||||
"-vcs=false",
|
||||
path,
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
|
||||
actual := testArchiveStr(t, archivePath)
|
||||
expected := []string{
|
||||
".terraform/",
|
||||
".terraform/terraform.tfstate",
|
||||
"main.tf",
|
||||
"terraform.tfvars",
|
||||
}
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
|
||||
if client.UpsertOptions.Name != "foo" {
|
||||
t.Fatalf("bad: %#v", client.UpsertOptions)
|
||||
}
|
||||
|
||||
variables := map[string]string{
|
||||
"foo": "old",
|
||||
"bar": "foo",
|
||||
}
|
||||
if !reflect.DeepEqual(client.UpsertOptions.Variables, variables) {
|
||||
t.Fatalf("bad: %#v", client.UpsertOptions)
|
||||
}
|
||||
}
|
||||
|
||||
// This tests that the push command will send the variables in tfvars
|
||||
func TestPush_tfvars(t *testing.T) {
|
||||
// Disable test mode so input would be asked and setup the
|
||||
// input reader/writers.
|
||||
test = false
|
||||
defer func() { test = true }()
|
||||
defaultInputReader = bytes.NewBufferString("nope\n")
|
||||
defaultInputWriter = new(bytes.Buffer)
|
||||
|
||||
tmp, cwd := testCwd(t)
|
||||
defer testFixCwd(t, tmp, cwd)
|
||||
|
||||
// Create remote state file, this should be pulled
|
||||
conf, srv := testRemoteState(t, testState(), 200)
|
||||
defer srv.Close()
|
||||
|
||||
// Persist local remote state
|
||||
s := terraform.NewState()
|
||||
s.Serial = 5
|
||||
s.Remote = conf
|
||||
testStateFileRemote(t, s)
|
||||
|
||||
// Path where the archive will be "uploaded" to
|
||||
archivePath := testTempFile(t)
|
||||
defer os.Remove(archivePath)
|
||||
|
||||
client := &mockPushClient{File: archivePath}
|
||||
ui := new(cli.MockUi)
|
||||
c := &PushCommand{
|
||||
Meta: Meta{
|
||||
ContextOpts: testCtxConfig(testProvider()),
|
||||
Ui: ui,
|
||||
},
|
||||
|
||||
client: client,
|
||||
}
|
||||
|
||||
path := testFixturePath("push-tfvars")
|
||||
args := []string{
|
||||
"-var-file", path + "/terraform.tfvars",
|
||||
|
|
Loading…
Reference in New Issue