remote: Refactoring to use a common client
This commit is contained in:
parent
d16793659c
commit
f748b6ae71
|
@ -21,23 +21,40 @@ type RemoteStatePayload struct {
|
||||||
State []byte
|
State []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetState is used to read the remote state
|
// remoteStateClient is used to interact with a remote state store
|
||||||
func GetState(conf *terraform.RemoteState) (*RemoteStatePayload, error) {
|
// 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
|
// Get the base URL configuration
|
||||||
base, err := url.Parse(conf.Server)
|
base, err := url.Parse(r.conf.Server)
|
||||||
if err != nil {
|
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
|
// 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
|
// Add the request token if any
|
||||||
if conf.AuthToken != "" {
|
if r.conf.AuthToken != "" {
|
||||||
values := base.Query()
|
values := base.Query()
|
||||||
values.Set("access_token", conf.AuthToken)
|
values.Set("access_token", r.conf.AuthToken)
|
||||||
base.RawQuery = values.Encode()
|
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
|
// Request the url
|
||||||
resp, err := http.Get(base.String())
|
resp, err := http.Get(base.String())
|
||||||
|
|
|
@ -50,7 +50,8 @@ func TestGetState_Consul(t *testing.T) {
|
||||||
Server: "http://demo.consul.io/v1/kv/test/tf/remote",
|
Server: "http://demo.consul.io/v1/kv/test/tf/remote",
|
||||||
}
|
}
|
||||||
REQ:
|
REQ:
|
||||||
payload, err := GetState(remote)
|
r := &remoteStateClient{conf: remote}
|
||||||
|
payload, err := r.GetState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -132,7 +133,8 @@ func TestGetState(t *testing.T) {
|
||||||
Server: s.URL,
|
Server: s.URL,
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := GetState(remote)
|
r := &remoteStateClient{remote}
|
||||||
|
payload, err := r.GetState()
|
||||||
errStr := ""
|
errStr := ""
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errStr = err.Error()
|
errStr = err.Error()
|
||||||
|
|
|
@ -217,7 +217,8 @@ This is likely a bug, please report it.`)
|
||||||
// the local state if necessary.
|
// the local state if necessary.
|
||||||
func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) {
|
func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) {
|
||||||
// Read the state from the server
|
// Read the state from the server
|
||||||
payload, err := GetState(conf)
|
client := &remoteStateClient{conf: conf}
|
||||||
|
payload, err := client.GetState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return StateChangeNoop,
|
return StateChangeNoop,
|
||||||
fmt.Errorf("Failed to read remote state: %v", err)
|
fmt.Errorf("Failed to read remote state: %v", err)
|
||||||
|
|
Loading…
Reference in New Issue