-
Notifications
You must be signed in to change notification settings - Fork 647
/
data_source_ibm_iam_roles.go
87 lines (73 loc) · 2.18 KB
/
data_source_ibm_iam_roles.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package iampolicy
import (
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/platform-services-go-sdk/iampolicymanagementv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMIAMRole() *schema.Resource {
return &schema.Resource{
Read: datasourceIBMIAMRoleRead,
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Optional: true,
Description: "The Service Name",
ForceNew: true,
},
"roles": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func datasourceIBMIAMRoleRead(d *schema.ResourceData, meta interface{}) error {
iamPolicyManagementClient, err := meta.(conns.ClientSession).IAMPolicyManagementV1API()
if err != nil {
return err
}
userDetails, err := meta.(conns.ClientSession).BluemixUserDetails()
if err != nil {
return err
}
var serviceName string
var customRoles []iampolicymanagementv1.CustomRole
var serviceRoles, systemRoles []iampolicymanagementv1.Role
listRoleOptions := &iampolicymanagementv1.ListRolesOptions{
AccountID: &userDetails.UserAccount,
}
if service, ok := d.GetOk("service"); ok {
serviceName = service.(string)
listRoleOptions.ServiceName = &serviceName
}
roleList, _, err := iamPolicyManagementClient.ListRoles(listRoleOptions)
if err != nil {
return err
}
customRoles = roleList.CustomRoles
serviceRoles = roleList.ServiceRoles
systemRoles = roleList.SystemRoles
d.SetId(userDetails.UserAccount)
roles := append(flex.FlattenRoleData(systemRoles, "platform"), append(flex.FlattenRoleData(serviceRoles, "service"), flex.FlattenCustomRoleData(customRoles, "custom")...)...)
d.Set("roles", roles)
return nil
}