/
data_source_ibm_iam_user_policy.go
129 lines (117 loc) · 3.34 KB
/
data_source_ibm_iam_user_policy.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package ibm
import (
"fmt"
"github.com/IBM-Cloud/bluemix-go/api/iampap/iampapv1"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
// Data source to find all the policies for a user in a particular account
func dataSourceIBMIAMUserPolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMIAMUserPolicyRead,
Schema: map[string]*schema.Schema{
"ibm_id": {
Description: "The ibm id or email of user",
Type: schema.TypeString,
Required: true,
},
"policies": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"roles": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Role names of the policy definition",
},
"resources": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Computed: true,
Description: "Service name of the policy definition",
},
"resource_instance_id": {
Type: schema.TypeString,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "ID of resource instance of the policy definition",
},
"region": {
Type: schema.TypeString,
Computed: true,
Description: "Region of the policy definition",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "Resource type of the policy definition",
},
"resource": {
Type: schema.TypeString,
Computed: true,
Description: "Resource of the policy definition",
},
"resource_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the resource group.",
},
},
},
},
},
},
},
},
}
}
func dataSourceIBMIAMUserPolicyRead(d *schema.ResourceData, meta interface{}) error {
iampapClient, err := meta.(ClientSession).IAMPAPAPI()
if err != nil {
return err
}
userEmail := d.Get("ibm_id").(string)
userDetails, err := meta.(ClientSession).BluemixUserDetails()
if err != nil {
return err
}
accountID := userDetails.userAccount
ibmUniqueID, err := getIBMUniqueId(accountID, userEmail, meta)
if err != nil {
return err
}
policies, err := iampapClient.V1Policy().List(iampapv1.SearchParams{
AccountID: accountID,
IAMID: ibmUniqueID,
Type: iampapv1.AccessPolicyType,
})
if err != nil {
return err
}
userPolicies := make([]map[string]interface{}, 0, len(policies))
for _, policy := range policies {
roles := make([]string, len(policy.Roles))
for i, role := range policy.Roles {
roles[i] = role.Name
}
resources := flattenPolicyResource(policy.Resources)
p := map[string]interface{}{
"id": fmt.Sprintf("%s/%s", userEmail, policy.ID),
"roles": roles,
"resources": resources,
}
userPolicies = append(userPolicies, p)
}
d.SetId(userEmail)
d.Set("policies", userPolicies)
return nil
}