rpc: implement Apply

This commit is contained in:
Mitchell Hashimoto 2014-06-18 16:49:34 -07:00
parent 8aa99687c3
commit 8c1a69aa0a
2 changed files with 72 additions and 0 deletions

View File

@ -48,6 +48,26 @@ func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
return err
}
func (p *ResourceProvider) Apply(
s *terraform.ResourceState,
d *terraform.ResourceDiff) (*terraform.ResourceState, error) {
var resp ResourceProviderApplyResponse
args := &ResourceProviderApplyArgs{
State: s,
Diff: d,
}
err := p.Client.Call(p.Name+".Apply", args, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
err = resp.Error
}
return resp.State, err
}
func (p *ResourceProvider) Diff(
s *terraform.ResourceState,
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
@ -89,6 +109,16 @@ type ResourceProviderConfigureResponse struct {
Error *BasicError
}
type ResourceProviderApplyArgs struct {
State *terraform.ResourceState
Diff *terraform.ResourceDiff
}
type ResourceProviderApplyResponse struct {
State *terraform.ResourceState
Error *BasicError
}
type ResourceProviderDiffArgs struct {
State *terraform.ResourceState
Config *terraform.ResourceConfig
@ -133,6 +163,17 @@ func (s *ResourceProviderServer) Configure(
return nil
}
func (s *ResourceProviderServer) Apply(
args *ResourceProviderApplyArgs,
result *ResourceProviderApplyResponse) error {
state, err := s.Provider.Apply(args.State, args.Diff)
*result = ResourceProviderApplyResponse{
State: state,
Error: NewBasicError(err),
}
return nil
}
func (s *ResourceProviderServer) Diff(
args *ResourceProviderDiffArgs,
result *ResourceProviderDiffResponse) error {

View File

@ -92,6 +92,37 @@ func TestResourceProvider_configure_warnings(t *testing.T) {
}
}
func TestResourceProvider_apply(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.ApplyReturn = &terraform.ResourceState{
ID: "bob",
}
// Apply
state := &terraform.ResourceState{}
diff := &terraform.ResourceDiff{}
newState, err := provider.Apply(state, diff)
if !p.ApplyCalled {
t.Fatal("apply should be called")
}
if !reflect.DeepEqual(p.ApplyDiff, diff) {
t.Fatalf("bad: %#v", p.ApplyDiff)
}
if err != nil {
t.Fatalf("bad: %#v", err)
}
if !reflect.DeepEqual(p.ApplyReturn, newState) {
t.Fatalf("bad: %#v", newState)
}
}
func TestResourceProvider_diff(t *testing.T) {
p := new(terraform.MockResourceProvider)
client, server := testClientServer(t)