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
```
This commit is contained in:
Paul Stack 2016-12-01 09:01:15 +02:00 committed by GitHub
parent c918c7b1c3
commit 83abf5b16a
4 changed files with 58 additions and 17 deletions

View File

@ -18,24 +18,27 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
Read: resourceAwsIamInstanceProfileRead, Read: resourceAwsIamInstanceProfileRead,
Update: resourceAwsIamInstanceProfileUpdate, Update: resourceAwsIamInstanceProfileUpdate,
Delete: resourceAwsIamInstanceProfileDelete, Delete: resourceAwsIamInstanceProfileDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"arn": &schema.Schema{ "arn": {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"create_date": &schema.Schema{ "create_date": {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"unique_id": &schema.Schema{ "unique_id": {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"name": &schema.Schema{ "name": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Computed: true, Computed: true,
@ -56,7 +59,7 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
}, },
}, },
"name_prefix": &schema.Schema{ "name_prefix": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
@ -75,14 +78,14 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
}, },
}, },
"path": &schema.Schema{ "path": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Default: "/", Default: "/",
ForceNew: true, ForceNew: true,
}, },
"roles": &schema.Schema{ "roles": {
Type: schema.TypeSet, Type: schema.TypeSet,
Required: true, Required: true,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},

View File

@ -8,17 +8,42 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "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) { func TestAccAWSIAMInstanceProfile_basic(t *testing.T) {
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
Steps: []resource.TestStep{ 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) { func TestAccAWSIAMInstanceProfile_namePrefix(t *testing.T) {
var conf iam.GetInstanceProfileOutput var conf iam.GetInstanceProfileOutput
rName := acctest.RandString(5)
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
@ -34,8 +60,8 @@ func TestAccAWSIAMInstanceProfile_namePrefix(t *testing.T) {
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSInstanceProfileDestroy, CheckDestroy: testAccCheckAWSInstanceProfileDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: testAccAWSInstanceProfilePrefixNameConfig, Config: testAccAWSInstanceProfilePrefixNameConfig(rName),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSInstanceProfileExists("aws_iam_instance_profile.test", &conf), testAccCheckAWSInstanceProfileExists("aws_iam_instance_profile.test", &conf),
testAccCheckAWSInstanceProfileGeneratedNamePrefix( 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" { 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\"]}]}" assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
} }
resource "aws_iam_instance_profile" "test" { resource "aws_iam_instance_profile" "test" {
name = "test" name = "test"
roles = ["${aws_iam_role.test.name}"] roles = ["${aws_iam_role.test.name}"]
}`, rName)
} }
`
const testAccAWSInstanceProfilePrefixNameConfig = ` func testAccAWSInstanceProfilePrefixNameConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" { 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\"]}]}" assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
} }
resource "aws_iam_instance_profile" "test" { resource "aws_iam_instance_profile" "test" {
name_prefix = "test-" name_prefix = "test-"
roles = ["${aws_iam_role.test.name}"] roles = ["${aws_iam_role.test.name}"]
}`, rName)
} }
`

View File

@ -57,6 +57,7 @@ To make a resource importable, please see the
* aws_glacier_vault * aws_glacier_vault
* aws_iam_account_password_policy * aws_iam_account_password_policy
* aws_iam_group * aws_iam_group
* aws_iam_instance_profile
* aws_iam_saml_provider * aws_iam_saml_provider
* aws_iam_user * aws_iam_user
* aws_instance * aws_instance

View File

@ -59,3 +59,12 @@ The following arguments are supported:
* `unique_id` - The [unique ID][1] assigned by AWS. * `unique_id` - The [unique ID][1] assigned by AWS.
[1]: https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_Identifiers.html#GUIDs [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
```