-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1805 from slaunay/feature/data-iam-user
data/aws_iam_user: Add a data source for IAM user
- Loading branch information
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/iam" | ||
"github.com/hashicorp/errwrap" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsIAMUser() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsIAMUserRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"path": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"user_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"user_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsIAMUserRead(d *schema.ResourceData, meta interface{}) error { | ||
iamconn := meta.(*AWSClient).iamconn | ||
userName := d.Get("user_name").(string) | ||
req := &iam.GetUserInput{ | ||
UserName: aws.String(userName), | ||
} | ||
|
||
resp, err := iamconn.GetUser(req) | ||
if err != nil { | ||
return errwrap.Wrapf("error getting user: {{err}}", err) | ||
} | ||
|
||
user := resp.User | ||
d.SetId(*user.UserId) | ||
d.Set("arn", user.Arn) | ||
d.Set("path", user.Path) | ||
d.Set("user_id", user.UserId) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package aws | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSDataSourceIAMUser_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAwsDataSourceIAMUserConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.aws_iam_user.test", "user_id"), | ||
resource.TestCheckResourceAttr("data.aws_iam_user.test", "path", "/"), | ||
resource.TestCheckResourceAttr("data.aws_iam_user.test", "user_name", "test-datasource-user"), | ||
resource.TestMatchResourceAttr("data.aws_iam_user.test", "arn", regexp.MustCompile("^arn:aws:iam::[0-9]{12}:user/test-datasource-user")), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccAwsDataSourceIAMUserConfig = ` | ||
resource "aws_iam_user" "user" { | ||
name = "test-datasource-user" | ||
path = "/" | ||
} | ||
data "aws_iam_user" "test" { | ||
user_name = "${aws_iam_user.user.name}" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_iam_user" | ||
sidebar_current: "docs-aws-datasource-iam-user" | ||
description: |- | ||
Get information on a Amazon IAM user | ||
--- | ||
|
||
# aws_iam_user | ||
|
||
This data source can be used to fetch information about a specific | ||
IAM user. By using this data source, you can reference IAM user | ||
properties without having to hard code ARNs or unique IDs as input. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "aws_iam_user" "example" { | ||
user_name = "an_example_user_name" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `user_name` - (Required) The friendly IAM user name to match. | ||
|
||
## Attributes Reference | ||
|
||
* `arn` - The Amazon Resource Name (ARN) assigned by AWS for this user. | ||
|
||
* `path` - Path in which this user was created. | ||
|
||
* `user_id` - The unique ID assigned by AWS for this user. |