remote: Refactoring to use a common client

This commit is contained in:
Armon Dadgar 2014-10-06 17:34:36 -07:00 committed by Mitchell Hashimoto
parent d16793659c
commit f748b6ae71
3 changed files with 30 additions and 10 deletions

View File

@ -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())

View File

@ -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()

View File

@ -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)