From 6cd5c894e85c7f71a24b24ef813e1301a59f892c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 22 Feb 2015 10:29:42 -0800 Subject: [PATCH] terraform: State.IsRemote --- terraform/state.go | 16 ++++++++++++++++ terraform/state_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/terraform/state.go b/terraform/state.go index 2ad11a667..52d4c8ca0 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -125,6 +125,22 @@ func (s *State) ModuleOrphans(path []string, c *config.Config) [][]string { return orphans } +// IsRemote returns true if State represents a state that exists and is +// remote. +func (s *State) IsRemote() bool { + if s == nil { + return false + } + if s.Remote == nil { + return false + } + if s.Remote.Type == "" { + return false + } + + return true +} + // RootModule returns the ModuleState for the root module func (s *State) RootModule() *ModuleState { root := s.ModuleByPath(rootModulePath) diff --git a/terraform/state_test.go b/terraform/state_test.go index 9b4a7d1e3..aa3fe6e0b 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -312,6 +312,34 @@ func TestInstanceStateEqual(t *testing.T) { } } +func TestStateIsRemote(t *testing.T) { + cases := []struct { + In *State + Result bool + }{ + { + nil, + false, + }, + { + &State{}, + false, + }, + { + &State{ + Remote: &RemoteState{Type: "foo"}, + }, + true, + }, + } + + for i, tc := range cases { + if tc.In.IsRemote() != tc.Result { + t.Fatalf("bad %d %#v:\n\n%#v", i, tc.Result, tc.In) + } + } +} + func TestInstanceState_MergeDiff(t *testing.T) { is := InstanceState{ ID: "foo",