command/refresh

This commit is contained in:
Mitchell Hashimoto 2014-06-27 11:09:01 -07:00
parent 9c7a3d9fea
commit 1819b6fb34
4 changed files with 193 additions and 0 deletions

115
command/refresh.go Normal file
View File

@ -0,0 +1,115 @@
package command
import (
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
// RefreshCommand is a cli.Command implementation that refreshes the state
// file.
type RefreshCommand struct {
TFConfig *terraform.Config
Ui cli.Ui
}
func (c *RefreshCommand) Run(args []string) int {
var outPath string
statePath := "terraform.tfstate"
configPath := "."
cmdFlags := flag.NewFlagSet("refresh", flag.ContinueOnError)
cmdFlags.StringVar(&outPath, "out", "", "output path")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
args = cmdFlags.Args()
if len(args) != 2 {
// TODO(mitchellh): this is temporary until we can assume current
// dir for Terraform config.
c.Ui.Error("TEMPORARY: The refresh command requires two args.")
cmdFlags.Usage()
return 1
}
statePath = args[0]
configPath = args[1]
if outPath == "" {
outPath = statePath
}
// Load up the state
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(configPath)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error loading blueprint: %s", err))
return 1
}
c.TFConfig.Hooks = append(c.TFConfig.Hooks, &UiHook{Ui: c.Ui})
tf, err := terraform.New(c.TFConfig)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err))
return 1
}
state, err = tf.Refresh(b, state)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error refreshing state: %s", err))
return 1
}
log.Printf("[INFO] Writing state output to: %s", outPath)
f, err = os.Create(outPath)
if err == nil {
defer f.Close()
err = terraform.WriteState(state, f)
}
if err != nil {
c.Ui.Error(fmt.Sprintf("Error writing state file: %s", err))
return 1
}
return 0
}
func (c *RefreshCommand) Help() string {
helpText := `
Usage: terraform refresh [options] [terraform.tfstate] [terraform.tf]
Refresh and update the state of your infrastructure. This is read-only
operation that will not modify infrastructure. The read-only property
is dependent on resource providers being implemented correctly.
Options:
-out=path Path to write updated state file. If this is not specified,
the existing state file will be overridden.
`
return strings.TrimSpace(helpText)
}
func (c *RefreshCommand) Synopsis() string {
return "Refresh the state of your infrastructure"
}

68
command/refresh_test.go Normal file
View File

@ -0,0 +1,68 @@
package command
import (
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
func TestRefresh(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())
state := &terraform.State{}
err = terraform.WriteState(state, tf)
tf.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
p := testProvider()
ui := new(cli.MockUi)
c := &RefreshCommand{
TFConfig: testTFConfig(p),
Ui: ui,
}
p.RefreshFn = nil
p.RefreshReturn = &terraform.ResourceState{ID: "yes"}
args := []string{
statePath,
testFixturePath("refresh"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
if !p.RefreshCalled {
t.Fatal("refresh should be called")
}
f, err := os.Open(statePath)
if err != nil {
t.Fatalf("err: %s", err)
}
newState, err := terraform.ReadState(f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := newState.Resources["test_instance.foo"]
expected := p.RefreshReturn
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}

View File

@ -0,0 +1,3 @@
resource "test_instance" "foo" {
ami = "bar"
}

View File

@ -40,6 +40,13 @@ func init() {
}, nil
},
"refresh": func() (cli.Command, error) {
return &command.RefreshCommand{
TFConfig: &TFConfig,
Ui: Ui,
}, nil
},
"version": func() (cli.Command, error) {
return &command.VersionCommand{
Revision: GitCommit,