From f748b6ae71bc5d6d2856c0a760c1dc9166a73741 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 6 Oct 2014 17:34:36 -0700 Subject: [PATCH] remote: Refactoring to use a common client --- remote/client.go | 31 ++++++++++++++++++++++++------- remote/client_test.go | 6 ++++-- remote/remote.go | 3 ++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/remote/client.go b/remote/client.go index 9c7ab2c43..f488988f0 100644 --- a/remote/client.go +++ b/remote/client.go @@ -21,23 +21,40 @@ type RemoteStatePayload struct { State []byte } -// GetState is used to read the remote state -func GetState(conf *terraform.RemoteState) (*RemoteStatePayload, error) { +// remoteStateClient is used to interact with a remote state store +// using the API +type remoteStateClient struct { + conf *terraform.RemoteState +} + +// URL is used to return an appropriate URL to hit for the +// given server and remote name +func (r *remoteStateClient) URL() (*url.URL, error) { // Get the base URL configuration - base, err := url.Parse(conf.Server) + base, err := url.Parse(r.conf.Server) if err != nil { - return nil, fmt.Errorf("Failed to parse remote server '%s': %v", conf.Server, err) + return nil, fmt.Errorf("Failed to parse remote server '%s': %v", r.conf.Server, err) } // Compute the full path by just appending the name - base.Path = path.Join(base.Path, conf.Name) + base.Path = path.Join(base.Path, r.conf.Name) // Add the request token if any - if conf.AuthToken != "" { + if r.conf.AuthToken != "" { values := base.Query() - values.Set("access_token", conf.AuthToken) + values.Set("access_token", r.conf.AuthToken) base.RawQuery = values.Encode() } + return base, nil +} + +// GetState is used to read the remote state +func (r *remoteStateClient) GetState() (*RemoteStatePayload, error) { + // Get the target URL + base, err := r.URL() + if err != nil { + return nil, err + } // Request the url resp, err := http.Get(base.String()) diff --git a/remote/client_test.go b/remote/client_test.go index 9a4d871a1..51c28ebed 100644 --- a/remote/client_test.go +++ b/remote/client_test.go @@ -50,7 +50,8 @@ func TestGetState_Consul(t *testing.T) { Server: "http://demo.consul.io/v1/kv/test/tf/remote", } REQ: - payload, err := GetState(remote) + r := &remoteStateClient{conf: remote} + payload, err := r.GetState() if err != nil { t.Fatalf("err: %v", err) } @@ -132,7 +133,8 @@ func TestGetState(t *testing.T) { Server: s.URL, } - payload, err := GetState(remote) + r := &remoteStateClient{remote} + payload, err := r.GetState() errStr := "" if err != nil { errStr = err.Error() diff --git a/remote/remote.go b/remote/remote.go index c24094d56..259bdab52 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -217,7 +217,8 @@ This is likely a bug, please report it.`) // the local state if necessary. func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) { // Read the state from the server - payload, err := GetState(conf) + client := &remoteStateClient{conf: conf} + payload, err := client.GetState() if err != nil { return StateChangeNoop, fmt.Errorf("Failed to read remote state: %v", err)