From 83abf5b16a7a8076edfd549bdfedc726a194c12e Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Thu, 1 Dec 2016 09:01:15 +0200 Subject: [PATCH] provider/aws: Support import of aws_iam_instance_profile (#10436) Fixes #10341 ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSIAMInstanceProfile_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/30 14:32:59 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSIAMInstanceProfile_ -timeout 120m === RUN TestAccAWSIAMInstanceProfile_importBasic --- PASS: TestAccAWSIAMInstanceProfile_importBasic (20.22s) === RUN TestAccAWSIAMInstanceProfile_basic --- PASS: TestAccAWSIAMInstanceProfile_basic (18.71s) === RUN TestAccAWSIAMInstanceProfile_namePrefix --- PASS: TestAccAWSIAMInstanceProfile_namePrefix (18.58s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws57.535s ``` --- .../aws/resource_aws_iam_instance_profile.go | 17 ++++--- .../resource_aws_iam_instance_profile_test.go | 48 +++++++++++++++---- .../source/docs/import/importability.html.md | 1 + .../aws/r/iam_instance_profile.html.markdown | 9 ++++ 4 files changed, 58 insertions(+), 17 deletions(-) diff --git a/builtin/providers/aws/resource_aws_iam_instance_profile.go b/builtin/providers/aws/resource_aws_iam_instance_profile.go index c84459e00..00f2c3637 100644 --- a/builtin/providers/aws/resource_aws_iam_instance_profile.go +++ b/builtin/providers/aws/resource_aws_iam_instance_profile.go @@ -18,24 +18,27 @@ func resourceAwsIamInstanceProfile() *schema.Resource { Read: resourceAwsIamInstanceProfileRead, Update: resourceAwsIamInstanceProfileUpdate, Delete: resourceAwsIamInstanceProfileDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ - "arn": &schema.Schema{ + "arn": { Type: schema.TypeString, Computed: true, }, - "create_date": &schema.Schema{ + "create_date": { Type: schema.TypeString, Computed: true, }, - "unique_id": &schema.Schema{ + "unique_id": { Type: schema.TypeString, Computed: true, }, - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Optional: true, Computed: true, @@ -56,7 +59,7 @@ func resourceAwsIamInstanceProfile() *schema.Resource { }, }, - "name_prefix": &schema.Schema{ + "name_prefix": { Type: schema.TypeString, Optional: true, ForceNew: true, @@ -75,14 +78,14 @@ func resourceAwsIamInstanceProfile() *schema.Resource { }, }, - "path": &schema.Schema{ + "path": { Type: schema.TypeString, Optional: true, Default: "/", ForceNew: true, }, - "roles": &schema.Schema{ + "roles": { Type: schema.TypeSet, Required: true, Elem: &schema.Schema{Type: schema.TypeString}, diff --git a/builtin/providers/aws/resource_aws_iam_instance_profile_test.go b/builtin/providers/aws/resource_aws_iam_instance_profile_test.go index 93001184b..0bd3d5ffb 100644 --- a/builtin/providers/aws/resource_aws_iam_instance_profile_test.go +++ b/builtin/providers/aws/resource_aws_iam_instance_profile_test.go @@ -8,17 +8,42 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/iam" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) +func TestAccAWSIAMInstanceProfile_importBasic(t *testing.T) { + resourceName := "aws_iam_instance_profile.test" + rName := acctest.RandString(5) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSInstanceProfileDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSInstanceProfilePrefixNameConfig(rName), + }, + + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"name_prefix"}, + }, + }, + }) +} + func TestAccAWSIAMInstanceProfile_basic(t *testing.T) { + rName := acctest.RandString(5) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccAwsIamInstanceProfileConfig, + { + Config: testAccAwsIamInstanceProfileConfig(rName), }, }, }) @@ -26,6 +51,7 @@ func TestAccAWSIAMInstanceProfile_basic(t *testing.T) { func TestAccAWSIAMInstanceProfile_namePrefix(t *testing.T) { var conf iam.GetInstanceProfileOutput + rName := acctest.RandString(5) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -34,8 +60,8 @@ func TestAccAWSIAMInstanceProfile_namePrefix(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccCheckAWSInstanceProfileDestroy, Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccAWSInstanceProfilePrefixNameConfig, + { + Config: testAccAWSInstanceProfilePrefixNameConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSInstanceProfileExists("aws_iam_instance_profile.test", &conf), testAccCheckAWSInstanceProfileGeneratedNamePrefix( @@ -118,26 +144,28 @@ func testAccCheckAWSInstanceProfileExists(n string, res *iam.GetInstanceProfileO } } -const testAccAwsIamInstanceProfileConfig = ` +func testAccAwsIamInstanceProfileConfig(rName string) string { + return fmt.Sprintf(` resource "aws_iam_role" "test" { - name = "test" + name = "test-%s" assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" } resource "aws_iam_instance_profile" "test" { name = "test" roles = ["${aws_iam_role.test.name}"] +}`, rName) } -` -const testAccAWSInstanceProfilePrefixNameConfig = ` +func testAccAWSInstanceProfilePrefixNameConfig(rName string) string { + return fmt.Sprintf(` resource "aws_iam_role" "test" { - name = "test" + name = "test-%s" assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" } resource "aws_iam_instance_profile" "test" { name_prefix = "test-" roles = ["${aws_iam_role.test.name}"] +}`, rName) } -` diff --git a/website/source/docs/import/importability.html.md b/website/source/docs/import/importability.html.md index ee8c36839..b3c56461d 100644 --- a/website/source/docs/import/importability.html.md +++ b/website/source/docs/import/importability.html.md @@ -57,6 +57,7 @@ To make a resource importable, please see the * aws_glacier_vault * aws_iam_account_password_policy * aws_iam_group +* aws_iam_instance_profile * aws_iam_saml_provider * aws_iam_user * aws_instance diff --git a/website/source/docs/providers/aws/r/iam_instance_profile.html.markdown b/website/source/docs/providers/aws/r/iam_instance_profile.html.markdown index c87fcdfbc..ca78294a3 100644 --- a/website/source/docs/providers/aws/r/iam_instance_profile.html.markdown +++ b/website/source/docs/providers/aws/r/iam_instance_profile.html.markdown @@ -59,3 +59,12 @@ The following arguments are supported: * `unique_id` - The [unique ID][1] assigned by AWS. [1]: https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_Identifiers.html#GUIDs + + +## Import + +Instance Profiles can be imported using the `name`, e.g. + +``` +$ terraform import aws_iam_instance_profile.test_profile app-instance-profile-1 +``` \ No newline at end of file