-
Notifications
You must be signed in to change notification settings - Fork 650
/
data_source_ibm_kms_instance_policies.go
269 lines (255 loc) · 9.71 KB
/
data_source_ibm_kms_instance_policies.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package kms
import (
"context"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
kp "github.com/IBM/keyprotect-go-client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMKmsInstancePolicies() *schema.Resource {
return &schema.Resource{
ReadContext: resourceIBMKmsInstancePolicyRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Key protect or hpcs instance GUID or CRN",
DiffSuppressFunc: suppressKMSInstanceIDDiff,
},
"policy_type": {
Type: schema.TypeString,
Optional: true,
Description: "Type of Policy to be Retrieved",
ValidateFunc: validate.ValidateAllowedPolicyType([]string{"dualAuthDelete", "keyCreateImportAccess", "metrics", "rotation"}),
},
"dual_auth_delete": {
Type: schema.TypeList,
Computed: true,
Description: "Data associated with the dual auth delete policy for instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Data associated with enable/disable dual authorization policy for the instance.",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource that created the policy.",
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
Description: "The date the policy was created. The date format follows RFC 3339.",
},
"updated_by": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource that updated the policy.",
},
"last_updated": {
Type: schema.TypeString,
Computed: true,
Description: "Updates when the policy is replaced or modified. The date format follows RFC 3339.",
},
},
},
},
"rotation": {
Type: schema.TypeList,
Computed: true,
Description: "Data associated with the rotation policy for instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Data associated with enable/disable of rotation policy for the instance",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource that created the policy.",
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
Description: "The date the policy was created. The date format follows RFC 3339.",
},
"updated_by": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource that updated the policy.",
},
"last_updated": {
Type: schema.TypeString,
Computed: true,
Description: "Updates when the policy is replaced or modified. The date format follows RFC 3339.",
},
"interval_month": {
Type: schema.TypeInt,
Computed: true,
Description: "Specifies the rotation time interval in months for the instance",
},
},
},
},
"key_create_import_access": {
Type: schema.TypeList,
Computed: true,
Description: "Data associated with the key create import access policy for the instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Data associated with enable/disable KCIA policy for the instance.",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource that created the policy.",
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
Description: "The date the policy was created. The date format follows RFC 3339.",
},
"updated_by": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource that updated the policy.",
},
"last_updated": {
Type: schema.TypeString,
Computed: true,
Description: "Updates when the policy is replaced or modified. The date format follows RFC 3339.",
},
"create_root_key": {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, Key Protect allows you or any authorized users to create root keys in the instance.",
},
"create_standard_key": {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, Key Protect allows you or any authorized users to create standard keys in the instance.",
},
"import_root_key": {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, Key Protect allows you or any authorized users to import root keys into the instance.",
},
"import_standard_key": {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, Key Protect allows you or any authorized users to import standard keys into the instance.",
},
"enforce_token": {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, the service prevents you or any authorized users from importing key material into the specified service instance without using an import token.",
},
},
},
},
"metrics": {
Type: schema.TypeList,
Computed: true,
Description: "Data associated with the metric policy for the instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Data associated with enable/disable metrics policy on the instance.",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource that created the policy.",
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
Description: "The date the policy was created. The date format follows RFC 3339.",
},
"updated_by": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for the resource that updated the policy.",
},
"last_updated": {
Type: schema.TypeString,
Computed: true,
Description: "Updates when the policy is replaced or modified. The date format follows RFC 3339.",
},
},
},
},
},
}
}
func resourceIBMKmsInstancePolicyRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
instanceID := getInstanceIDFromCRN(d.Get("instance_id").(string))
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return diag.FromErr(err)
}
policyType := d.Get("policy_type").(string)
if policyType != "" {
switch policyType {
case "dualAuthDelete":
var dualAuthInstancePolicy []kp.InstancePolicy
instancePolicies, err := kpAPI.GetDualAuthInstancePolicy(context)
if err != nil {
return diag.Errorf("[ERROR] Error retrieving instance policies: %s", err)
}
dualAuthInstancePolicy = append(dualAuthInstancePolicy, *instancePolicies)
d.Set("dual_auth_delete", flex.FlattenInstancePolicy("dual_auth_delete", dualAuthInstancePolicy))
case "keyCreateImportAccess":
var createImportAccessPolicy []kp.InstancePolicy
instancePolicies, err := kpAPI.GetKeyCreateImportAccessInstancePolicy(context)
if err != nil {
return diag.Errorf("[ERROR] Error retrieving instance policies: %s", err)
}
createImportAccessPolicy = append(createImportAccessPolicy, *instancePolicies)
d.Set("key_create_import_access", flex.FlattenInstancePolicy("key_create_import_access", createImportAccessPolicy))
case "metrics":
var metricsPolicy []kp.InstancePolicy
instancePolicies, err := kpAPI.GetMetricsInstancePolicy(context)
if err != nil {
return diag.Errorf("[ERROR] Error retrieving instance policies: %s", err)
}
metricsPolicy = append(metricsPolicy, *instancePolicies)
d.Set("metrics", flex.FlattenInstancePolicy("metrics", metricsPolicy))
case "rotation":
var rotationPolicy []kp.InstancePolicy
instancePolicies, err := kpAPI.GetRotationInstancePolicy(context)
if err != nil {
return diag.Errorf("[ERROR] Error retrieving instance policies: %s", err)
}
rotationPolicy = append(rotationPolicy, *instancePolicies)
d.Set("rotation", flex.FlattenInstancePolicy("rotation", rotationPolicy))
default:
return diag.Errorf("Invalid Policy Type")
}
} else {
instancePolicies, err := kpAPI.GetInstancePolicies(context)
if err != nil {
return diag.Errorf("[ERROR] Error retrieving instance policies: %s", err)
}
d.Set("key_create_import_access", flex.FlattenInstancePolicy("key_create_import_access", instancePolicies))
d.Set("metrics", flex.FlattenInstancePolicy("metrics", instancePolicies))
d.Set("rotation", flex.FlattenInstancePolicy("rotation", instancePolicies))
d.Set("dual_auth_delete", flex.FlattenInstancePolicy("dual_auth_delete", instancePolicies))
}
d.Set("instance_id", instanceID)
d.SetId(instanceID)
return nil
}