From 531f609564888001d71bab25131d4fff16973cbd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 26 Apr 2016 10:56:41 -0700 Subject: [PATCH] terraform: add ImportState to the provider interface --- terraform/resource_provider.go | 15 +++++++++++++++ terraform/resource_provider_mock.go | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/terraform/resource_provider.go b/terraform/resource_provider.go index c3f0b969f..94b283b9c 100644 --- a/terraform/resource_provider.go +++ b/terraform/resource_provider.go @@ -82,6 +82,21 @@ type ResourceProvider interface { * Functions related to importing *********************************************************************/ + // ImportState requests that the given resource be imported. + // + // The returned InstanceState only requires ID be set. Importing + // will always call Refresh after the state to complete it. + // + // IMPORTANT: InstanceState doesn't have the resource type attached + // to it. A type must be specified on the state via the Ephemeral + // field on the state. + // + // This function can return multiple states. Normally, an import + // will map 1:1 to a physical resource. However, some resources map + // to multiple. For example, an AWS security group may contain many rules. + // Each rule is represented by a separate resource in Terraform, + // therefore multiple states are returned. + ImportState(*InstanceInfo) ([]*InstanceState, error) } // ResourceProviderCloser is an interface that providers that can close diff --git a/terraform/resource_provider_mock.go b/terraform/resource_provider_mock.go index 75fb6d87e..529b8c4d2 100644 --- a/terraform/resource_provider_mock.go +++ b/terraform/resource_provider_mock.go @@ -55,6 +55,12 @@ type MockResourceProvider struct { ValidateResourceConfig *ResourceConfig ValidateResourceReturnWarns []string ValidateResourceReturnErrors []error + + ImportStateCalled bool + ImportStateInfo *InstanceInfo + ImportStateReturn []*InstanceState + ImportStateReturnError error + ImportStateFn func(*InstanceInfo) ([]*InstanceState, error) } func (p *MockResourceProvider) Close() error { @@ -175,3 +181,16 @@ func (p *MockResourceProvider) Resources() []ResourceType { p.ResourcesCalled = true return p.ResourcesReturn } + +func (p *MockResourceProvider) ImportState(info *InstanceInfo) ([]*InstanceState, error) { + p.Lock() + defer p.Unlock() + + p.ImportStateCalled = true + p.ImportStateInfo = info + if p.ImportStateFn != nil { + return p.ImportStateFn(info) + } + + return p.ImportStateReturn, p.ImportStateReturnError +}