command/apply: read existing state
This commit is contained in:
parent
82af81b606
commit
d4994b5d44
|
@ -19,9 +19,11 @@ type ApplyCommand struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ApplyCommand) Run(args []string) int {
|
func (c *ApplyCommand) Run(args []string) int {
|
||||||
|
var init bool
|
||||||
var statePath, stateOutPath string
|
var statePath, stateOutPath string
|
||||||
|
|
||||||
cmdFlags := flag.NewFlagSet("apply", flag.ContinueOnError)
|
cmdFlags := flag.NewFlagSet("apply", flag.ContinueOnError)
|
||||||
|
cmdFlags.BoolVar(&init, "init", false, "init")
|
||||||
cmdFlags.StringVar(&statePath, "state", "terraform.tfstate", "path")
|
cmdFlags.StringVar(&statePath, "state", "terraform.tfstate", "path")
|
||||||
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
|
cmdFlags.StringVar(&stateOutPath, "state-out", "", "path")
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
|
@ -48,6 +50,23 @@ func (c *ApplyCommand) Run(args []string) int {
|
||||||
stateOutPath = statePath
|
stateOutPath = statePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load up the state
|
||||||
|
var state *terraform.State
|
||||||
|
if !init {
|
||||||
|
f, err := os.Open(statePath)
|
||||||
|
if err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Error loading state: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
state, err = terraform.ReadState(f)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf("Error loading state: %s", err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
b, err := config.Load(args[0])
|
b, err := config.Load(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err))
|
||||||
|
@ -63,13 +82,13 @@ func (c *ApplyCommand) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
diff, err := tf.Diff(nil)
|
diff, err := tf.Diff(state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error running diff: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error running diff: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
state, err := tf.Apply(nil, diff)
|
state, err = tf.Apply(state, diff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error applying diff: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error applying diff: %s", err))
|
||||||
return 1
|
return 1
|
||||||
|
|
|
@ -3,6 +3,7 @@ package command
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
@ -26,6 +27,7 @@ func TestApply(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
|
"-init",
|
||||||
"-state", statePath,
|
"-state", statePath,
|
||||||
testFixturePath("apply"),
|
testFixturePath("apply"),
|
||||||
}
|
}
|
||||||
|
@ -69,6 +71,72 @@ func TestApply_noState(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApply_state(t *testing.T) {
|
||||||
|
// Write out some prior state
|
||||||
|
tf, err := ioutil.TempFile("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
statePath := tf.Name()
|
||||||
|
defer os.Remove(tf.Name())
|
||||||
|
|
||||||
|
originalState := &terraform.State{
|
||||||
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
"test_instance.foo": &terraform.ResourceState{
|
||||||
|
ID: "bar",
|
||||||
|
Type: "test_instance",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err = terraform.WriteState(originalState, tf)
|
||||||
|
tf.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &ApplyCommand{
|
||||||
|
TFConfig: testTFConfig(p),
|
||||||
|
Ui: ui,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run the apply command pointing to our existing state
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
testFixturePath("apply"),
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that the provider was called with the existing state
|
||||||
|
expectedState := originalState.Resources["test_instance.foo"]
|
||||||
|
if !reflect.DeepEqual(p.ApplyState, expectedState) {
|
||||||
|
t.Fatalf("bad: %#v", p.ApplyState)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify a new state exists
|
||||||
|
if _, err := os.Stat(statePath); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(statePath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
state, err := terraform.ReadState(f)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if state == nil {
|
||||||
|
t.Fatal("state should not be nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApply_stateNoExist(t *testing.T) {
|
func TestApply_stateNoExist(t *testing.T) {
|
||||||
p := testProvider()
|
p := testProvider()
|
||||||
ui := new(cli.MockUi)
|
ui := new(cli.MockUi)
|
||||||
|
|
Loading…
Reference in New Issue