-
Notifications
You must be signed in to change notification settings - Fork 647
/
data_source_ibm_iam_policy_template.go
271 lines (254 loc) · 9.19 KB
/
data_source_ibm_iam_policy_template.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package iampolicy
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM/platform-services-go-sdk/iampolicymanagementv1"
)
func DataSourceIBMIAMPolicyTemplate() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPolicyTemplateRead,
Schema: map[string]*schema.Schema{
"policy_templates": {
Type: schema.TypeList,
Computed: true,
Description: "List of policy templates.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "name of template.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "description of template purpose.",
},
"account_id": {
Type: schema.TypeString,
Computed: true,
Description: "account id where this template will be created.",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "Template vesrsion.",
},
"committed": {
Type: schema.TypeBool,
Computed: true,
Description: "Template vesrsion committed status.",
},
"policy": {
Type: schema.TypeList,
Computed: true,
Description: "The core set of properties associated with the template's policy objet.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
Description: "The policy type; either 'access' or 'authorization'.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Allows the customer to use their own words to record the purpose/context related to a policy.",
},
"resource": {
Type: schema.TypeList,
Computed: true,
Description: "The resource attributes to which the policy grants access.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"attributes": {
Type: schema.TypeList,
Computed: true,
Description: "List of resource attributes to which the policy grants access.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
Description: "The name of a resource attribute.",
},
"operator": {
Type: schema.TypeString,
Computed: true,
Description: "The operator of an attribute.",
},
"value": {
Type: schema.TypeString,
Computed: true,
Description: "The value of a rule or resource attribute; can be boolean or string for resource attribute. Can be string or an array of strings (e.g., array of days to permit access) for rule attribute.",
},
},
},
},
"tags": {
Type: schema.TypeList,
Computed: true,
Description: "Optional list of resource tags to which the policy grants access.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
Description: "The name of an access management tag.",
},
"value": {
Type: schema.TypeString,
Computed: true,
Description: "The value of an access management tag.",
},
"operator": {
Type: schema.TypeString,
Computed: true,
Description: "The operator of an access management tag.",
},
},
},
},
},
},
},
"pattern": {
Type: schema.TypeString,
Computed: true,
Description: "Indicates pattern of rule, either 'time-based-conditions:once', 'time-based-conditions:weekly:all-day', or 'time-based-conditions:weekly:custom-hours'.",
},
"rule_conditions": {
Type: schema.TypeSet,
Optional: true,
Description: "Rule conditions enforced by the policy",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Optional: true,
Description: "Key of the condition",
},
"operator": {
Type: schema.TypeString,
Required: true,
Description: "Operator of the condition",
},
"value": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Value of the condition",
},
"conditions": {
Type: schema.TypeList,
Optional: true,
Description: "Additional Rule conditions enforced by the policy",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
Description: "Key of the condition",
},
"operator": {
Type: schema.TypeString,
Required: true,
Description: "Operator of the condition",
},
"value": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Value of the condition",
},
},
},
},
},
},
},
"rule_operator": {
Type: schema.TypeString,
Optional: true,
Description: "Operator that multiple rule conditions are evaluated over",
},
"roles": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Role names of the policy definition",
},
},
},
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The policy template ID.",
},
},
},
},
},
}
}
func dataSourceIBMPolicyTemplateRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
iamPolicyManagementClient, err := meta.(conns.ClientSession).IAMPolicyManagementV1API()
if err != nil {
return diag.FromErr(err)
}
userDetails, err := meta.(conns.ClientSession).BluemixUserDetails()
if err != nil {
return diag.FromErr(fmt.Errorf("Failed to fetch BluemixUserDetails %s", err))
}
accountID := userDetails.UserAccount
listPolicyTemplatesOptions := &iampolicymanagementv1.ListPolicyTemplatesOptions{}
listPolicyTemplatesOptions.SetAccountID(accountID)
policyTemplateCollection, response, err := iamPolicyManagementClient.ListPolicyTemplatesWithContext(context, listPolicyTemplatesOptions)
if err != nil {
log.Printf("[DEBUG] ListPolicyTemplatesWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListPolicyTemplatesWithContext failed %s\n%s", err, response))
}
d.SetId(accountID)
policyTemplates := []map[string]interface{}{}
if policyTemplateCollection.PolicyTemplates != nil {
for _, modelItem := range policyTemplateCollection.PolicyTemplates {
modelMap, err := dataSourceIBMPolicyTemplatePolicyTemplateToMap(&modelItem, iamPolicyManagementClient)
if err != nil {
return diag.FromErr(err)
}
policyTemplates = append(policyTemplates, modelMap)
}
}
if err = d.Set("policy_templates", policyTemplates); err != nil {
return diag.FromErr(fmt.Errorf("Error setting policy_templates %s", err))
}
return nil
}
func dataSourceIBMPolicyTemplatePolicyTemplateToMap(model *iampolicymanagementv1.PolicyTemplate, iamPolicyManagementClient *iampolicymanagementv1.IamPolicyManagementV1) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["name"] = model.Name
if model.Description != nil {
modelMap["description"] = model.Description
}
modelMap["account_id"] = model.AccountID
modelMap["version"] = model.Version
if model.Committed != nil {
modelMap["committed"] = model.Committed
}
policyMap, err := flattenTemplatePolicy(model.Policy, iamPolicyManagementClient)
if err != nil {
return modelMap, err
}
modelMap["policy"] = []map[string]interface{}{policyMap}
if model.ID != nil {
modelMap["id"] = model.ID
}
return modelMap, nil
}