rpc: add Refresh method

This commit is contained in:
Mitchell Hashimoto 2014-06-20 11:51:07 -07:00
parent bff4b8a58c
commit 5dbb9c821a
2 changed files with 60 additions and 0 deletions

View File

@ -87,6 +87,20 @@ func (p *ResourceProvider) Diff(
return resp.Diff, err
}
func (p *ResourceProvider) Refresh(
s *terraform.ResourceState) (*terraform.ResourceState, error) {
var resp ResourceProviderRefreshResponse
err := p.Client.Call(p.Name+".Refresh", s, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
err = resp.Error
}
return resp.State, err
}
func (p *ResourceProvider) Resources() []terraform.ResourceType {
var result []terraform.ResourceType
@ -129,6 +143,11 @@ type ResourceProviderDiffResponse struct {
Error *BasicError
}
type ResourceProviderRefreshResponse struct {
State *terraform.ResourceState
Error *BasicError
}
type ResourceProviderValidateArgs struct {
Config *terraform.ResourceConfig
}
@ -185,6 +204,17 @@ func (s *ResourceProviderServer) Diff(
return nil
}
func (s *ResourceProviderServer) Refresh(
state *terraform.ResourceState,
result *ResourceProviderRefreshResponse) error {
newState, err := s.Provider.Refresh(state)
*result = ResourceProviderRefreshResponse{
State: newState,
Error: NewBasicError(err),
}
return nil
}
func (s *ResourceProviderServer) Resources(
nothing interface{},
result *[]terraform.ResourceType) error {

View File

@ -192,6 +192,36 @@ func TestResourceProvider_diff_error(t *testing.T) {
}
}
func TestResourceProvider_refresh(t *testing.T) {
p := new(terraform.MockResourceProvider)
client, server := testClientServer(t)
name, err := Register(server, p)
if err != nil {
t.Fatalf("err: %s", err)
}
provider := &ResourceProvider{Client: client, Name: name}
p.RefreshReturn = &terraform.ResourceState{
ID: "bob",
}
// Refresh
state := &terraform.ResourceState{}
newState, err := provider.Refresh(state)
if !p.RefreshCalled {
t.Fatal("refresh should be called")
}
if !reflect.DeepEqual(p.RefreshState, state) {
t.Fatalf("bad: %#v", p.RefreshState)
}
if err != nil {
t.Fatalf("bad: %#v", err)
}
if !reflect.DeepEqual(p.RefreshReturn, newState) {
t.Fatalf("bad: %#v", newState)
}
}
func TestResourceProvider_resources(t *testing.T) {
p := new(terraform.MockResourceProvider)
client, server := testClientServer(t)