forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_aws_iam_instance_profile.go
83 lines (71 loc) · 1.79 KB
/
data_source_aws_iam_instance_profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package aws
import (
"fmt"
"log"
"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 dataSourceAwsIAMInstanceProfile() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsIAMInstanceProfileRead,
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"create_date": {
Type: schema.TypeString,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"role_arn": {
Type: schema.TypeString,
Computed: true,
},
"role_id": {
Type: schema.TypeString,
Computed: true,
},
"role_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsIAMInstanceProfileRead(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
name := d.Get("name").(string)
req := &iam.GetInstanceProfileInput{
InstanceProfileName: aws.String(name),
}
log.Printf("[DEBUG] Reading IAM Instance Profile: %s", req)
resp, err := iamconn.GetInstanceProfile(req)
if err != nil {
return errwrap.Wrapf("Error getting instance profiles: {{err}}", err)
}
if resp == nil {
return fmt.Errorf("no IAM instance profile found")
}
instanceProfile := resp.InstanceProfile
d.SetId(*instanceProfile.InstanceProfileId)
d.Set("arn", instanceProfile.Arn)
d.Set("create_date", fmt.Sprintf("%v", instanceProfile.CreateDate))
d.Set("path", instanceProfile.Path)
if len(instanceProfile.Roles) > 0 {
role := instanceProfile.Roles[0]
d.Set("role_arn", role.Arn)
d.Set("role_id", role.RoleId)
d.Set("role_name", role.RoleName)
}
return nil
}