github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_iam_role.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/service/iam"
     8  	"github.com/hashicorp/errwrap"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourceAwsIAMRole() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsIAMRoleRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"arn": {
    18  				Type:     schema.TypeString,
    19  				Computed: true,
    20  			},
    21  			"assume_role_policy_document": {
    22  				Type:     schema.TypeString,
    23  				Computed: true,
    24  			},
    25  			"path": {
    26  				Type:     schema.TypeString,
    27  				Computed: true,
    28  			},
    29  			"role_id": {
    30  				Type:     schema.TypeString,
    31  				Computed: true,
    32  			},
    33  			"role_name": {
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func dataSourceAwsIAMRoleRead(d *schema.ResourceData, meta interface{}) error {
    42  	iamconn := meta.(*AWSClient).iamconn
    43  
    44  	roleName := d.Get("role_name").(string)
    45  
    46  	req := &iam.GetRoleInput{
    47  		RoleName: aws.String(roleName),
    48  	}
    49  
    50  	resp, err := iamconn.GetRole(req)
    51  	if err != nil {
    52  		return errwrap.Wrapf("Error getting roles: {{err}}", err)
    53  	}
    54  	if resp == nil {
    55  		return fmt.Errorf("no IAM role found")
    56  	}
    57  
    58  	role := resp.Role
    59  
    60  	d.SetId(*role.RoleId)
    61  	d.Set("arn", role.Arn)
    62  	d.Set("assume_role_policy_document", role.AssumeRolePolicyDocument)
    63  	d.Set("path", role.Path)
    64  	d.Set("role_id", role.RoleId)
    65  
    66  	return nil
    67  }