remote: simplify client

This commit is contained in:
Armon Dadgar 2014-10-03 11:14:12 -07:00 committed by Mitchell Hashimoto
parent b83b4a923f
commit d077a82db2
2 changed files with 8 additions and 17 deletions

View File

@ -17,8 +17,8 @@ import (
// RemoteStatePayload is used to return the remote state
// along with associated meta data when we do a remote fetch.
type RemoteStatePayload struct {
MD5 []byte
R io.Reader
MD5 []byte
State []byte
}
// GetState is used to read the remote state
@ -72,7 +72,7 @@ func GetState(conf *terraform.RemoteState) (*RemoteStatePayload, error) {
// Create the payload
payload := &RemoteStatePayload{
R: buf,
State: buf.Bytes(),
}
// Check if this is Consul
@ -88,11 +88,7 @@ func GetState(conf *terraform.RemoteState) (*RemoteStatePayload, error) {
}
// Setup the reader to pull the value from Consul
payload.R = bytes.NewReader(values[0].Value)
// Generate the MD5
hash := md5.Sum(values[0].Value)
payload.MD5 = hash[:md5.Size]
payload.State = values[0].Value
}
}
@ -104,9 +100,9 @@ func GetState(conf *terraform.RemoteState) (*RemoteStatePayload, error) {
}
payload.MD5 = md5
} else if _, ok := payload.R.(*bytes.Buffer); ok {
} else {
// Generate the MD5
hash := md5.Sum(buf.Bytes())
hash := md5.Sum(payload.State)
payload.MD5 = hash[:md5.Size]
}

View File

@ -3,7 +3,6 @@ package remote
import (
"bytes"
"crypto/md5"
"io"
"net/http"
"net/http/httptest"
"strings"
@ -63,9 +62,7 @@ REQ:
}
// Check the body
var buf bytes.Buffer
io.Copy(&buf, payload.R)
if string(buf.Bytes()) != "testing" {
if string(payload.State) != "testing" {
t.Fatalf("Bad body")
}
@ -151,9 +148,7 @@ func TestGetState(t *testing.T) {
}
if tc.Body != nil {
buf := bytes.NewBuffer(nil)
io.Copy(buf, payload.R)
if !bytes.Equal(buf.Bytes(), tc.Body) {
if !bytes.Equal(payload.State, tc.Body) {
t.Fatalf("bad: %#v", payload)
}
}