Skip to content

Commit

Permalink
Merge pull request #1805 from slaunay/feature/data-iam-user
Browse files Browse the repository at this point in the history
data/aws_iam_user: Add a data source for IAM user
  • Loading branch information
radeksimko authored Oct 9, 2017
2 parents 3a68991 + ee29e75 commit d7be4e7
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
54 changes: 54 additions & 0 deletions aws/data_source_aws_iam_user.go
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
}
37 changes: 37 additions & 0 deletions aws/data_source_aws_iam_user_test.go
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}"
}
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func Provider() terraform.ResourceProvider {
"aws_iam_policy_document": dataSourceAwsIamPolicyDocument(),
"aws_iam_role": dataSourceAwsIAMRole(),
"aws_iam_server_certificate": dataSourceAwsIAMServerCertificate(),
"aws_iam_user": dataSourceAwsIAMUser(),
"aws_internet_gateway": dataSourceAwsInternetGateway(),
"aws_instance": dataSourceAwsInstance(),
"aws_ip_ranges": dataSourceAwsIPRanges(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
<li<%= sidebar_current("docs-aws-iam-server-certificate") %>>
<a href="/docs/providers/aws/d/iam_server_certificate.html">aws_iam_server_certificate</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-iam-user") %>>
<a href="/docs/providers/aws/d/iam_user.html">aws_iam_user</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-instance") %>>
<a href="/docs/providers/aws/d/instance.html">aws_instance</a>
</li>
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/iam_user.html.markdown
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.

0 comments on commit d7be4e7

Please sign in to comment.