From 884980da1ab8d39196239f009a6c4efdc55d9337 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 13 May 2016 11:27:23 -0700 Subject: [PATCH] providers/aws: instance, nat, internet gateway --- .../providers/aws/import_aws_instance_test.go | 29 +++++++++++++++++++ .../aws/import_aws_internet_gateway_test.go | 28 ++++++++++++++++++ .../aws/import_aws_nat_gateway_test.go | 28 ++++++++++++++++++ .../providers/aws/resource_aws_instance.go | 3 ++ .../aws/resource_aws_internet_gateway.go | 3 ++ .../providers/aws/resource_aws_nat_gateway.go | 3 ++ helper/resource/testing.go | 7 ++++- helper/resource/testing_import_state.go | 18 +++++++++++- 8 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 builtin/providers/aws/import_aws_instance_test.go create mode 100644 builtin/providers/aws/import_aws_internet_gateway_test.go create mode 100644 builtin/providers/aws/import_aws_nat_gateway_test.go diff --git a/builtin/providers/aws/import_aws_instance_test.go b/builtin/providers/aws/import_aws_instance_test.go new file mode 100644 index 0000000000..cf1eea5245 --- /dev/null +++ b/builtin/providers/aws/import_aws_instance_test.go @@ -0,0 +1,29 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSInstance_importBasic(t *testing.T) { + resourceName := "aws_instance.foo" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccInstanceConfigVPC, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"associate_public_ip_address", "user_data"}, + }, + }, + }) +} diff --git a/builtin/providers/aws/import_aws_internet_gateway_test.go b/builtin/providers/aws/import_aws_internet_gateway_test.go new file mode 100644 index 0000000000..561a730b26 --- /dev/null +++ b/builtin/providers/aws/import_aws_internet_gateway_test.go @@ -0,0 +1,28 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSInternetGateway_importBasic(t *testing.T) { + resourceName := "aws_internet_gateway.foo" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckInternetGatewayDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccInternetGatewayConfig, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/aws/import_aws_nat_gateway_test.go b/builtin/providers/aws/import_aws_nat_gateway_test.go new file mode 100644 index 0000000000..1c1ef196e4 --- /dev/null +++ b/builtin/providers/aws/import_aws_nat_gateway_test.go @@ -0,0 +1,28 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSNatGateway_importBasic(t *testing.T) { + resourceName := "aws_nat_gateway.gateway" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNatGatewayDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccNatGatewayConfig, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index e62df1cf34..cc44e73287 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -24,6 +24,9 @@ func resourceAwsInstance() *schema.Resource { Read: resourceAwsInstanceRead, Update: resourceAwsInstanceUpdate, Delete: resourceAwsInstanceDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, SchemaVersion: 1, MigrateState: resourceAwsInstanceMigrateState, diff --git a/builtin/providers/aws/resource_aws_internet_gateway.go b/builtin/providers/aws/resource_aws_internet_gateway.go index dacb02a56a..338ab08331 100644 --- a/builtin/providers/aws/resource_aws_internet_gateway.go +++ b/builtin/providers/aws/resource_aws_internet_gateway.go @@ -18,6 +18,9 @@ func resourceAwsInternetGateway() *schema.Resource { Read: resourceAwsInternetGatewayRead, Update: resourceAwsInternetGatewayUpdate, Delete: resourceAwsInternetGatewayDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "vpc_id": &schema.Schema{ diff --git a/builtin/providers/aws/resource_aws_nat_gateway.go b/builtin/providers/aws/resource_aws_nat_gateway.go index c57fb9f649..4ad23ad8f5 100644 --- a/builtin/providers/aws/resource_aws_nat_gateway.go +++ b/builtin/providers/aws/resource_aws_nat_gateway.go @@ -18,6 +18,9 @@ func resourceAwsNatGateway() *schema.Resource { Create: resourceAwsNatGatewayCreate, Read: resourceAwsNatGatewayRead, Delete: resourceAwsNatGatewayDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "allocation_id": &schema.Schema{ diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 17358f93dc..f3de5fee17 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -153,7 +153,12 @@ type TestStep struct { // ImportStateVerify, if true, will also check that the state values // that are finally put into the state after import match for all the // IDs returned by the Import. - ImportStateVerify bool + // + // ImportStateVerifyIgnore are fields that should not be verified to + // be equal. These can be set to ephemeral fields or fields that can't + // be refreshed and don't matter. + ImportStateVerify bool + ImportStateVerifyIgnore []string } // Test performs an acceptance test on a resource. diff --git a/helper/resource/testing_import_state.go b/helper/resource/testing_import_state.go index c110ec950c..f16f17130e 100644 --- a/helper/resource/testing_import_state.go +++ b/helper/resource/testing_import_state.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "reflect" + "strings" "github.com/davecgh/go-spew/spew" "github.com/hashicorp/terraform/terraform" @@ -91,6 +92,21 @@ func testStepImportState( // Compare their attributes actual := r.Primary.Attributes expected := oldR.Primary.Attributes + + // Remove fields we're ignoring + for _, v := range step.ImportStateVerifyIgnore { + for k, _ := range actual { + if strings.HasPrefix(k, v) { + delete(actual, k) + } + } + for k, _ := range expected { + if strings.HasPrefix(k, v) { + delete(expected, k) + } + } + } + if !reflect.DeepEqual(actual, expected) { // Determine only the different attributes for k, v := range expected { @@ -103,7 +119,7 @@ func testStepImportState( spewConf := spew.NewDefaultConfig() spewConf.SortKeys = true return state, fmt.Errorf( - "Attributes not equivalent. Difference is shown below. Top is actual, bottom is expected."+ + "ImportStateVerify attributes not equivalent. Difference is shown below. Top is actual, bottom is expected."+ "\n\n%s\n\n%s", spewConf.Sdump(actual), spewConf.Sdump(expected)) }