-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_kms_key_rings.go
95 lines (81 loc) · 2.48 KB
/
data_source_ibm_kms_key_rings.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package kms
import (
"context"
"fmt"
//kp "github.com/IBM/keyprotect-go-client"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMKMSkeyRings() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMKMSKeyRingsRead,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: "Key protect or hpcs instance GUID",
DiffSuppressFunc: suppressKMSInstanceIDDiff,
},
"endpoint_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private"}),
Description: "public or private",
Default: "public",
},
"key_rings": {
Type: schema.TypeList,
Computed: true,
Description: "Key Rings for a particualer instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"created_by": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceIBMKMSKeyRingsRead(d *schema.ResourceData, meta interface{}) error {
instanceID := getInstanceIDFromCRN(d.Get("instance_id").(string))
api, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
endpointType := d.Get("endpoint_type").(string)
keys, err := api.GetKeyRings(context.Background())
if err != nil || keys == nil {
return fmt.Errorf("[ERROR] Get Key Rings failed with error: %s", err)
}
if keys == nil || keys.KeyRings == nil || len(keys.KeyRings) == 0 {
return fmt.Errorf("[ERROR] No key Rings in instance %s", instanceID)
}
keyRingMap := make([]map[string]interface{}, 0, len(keys.KeyRings))
for _, keyRing := range keys.KeyRings {
keyInstance := make(map[string]interface{})
keyInstance["id"] = keyRing.ID
keyInstance["created_by"] = keyRing.CreatedBy
if keyRing.CreationDate != nil {
keyInstance["creation_date"] = keyRing.CreationDate.String()
}
keyRingMap = append(keyRingMap, keyInstance)
}
d.SetId(instanceID)
d.Set("key_rings", keyRingMap)
d.Set("instance_id", instanceID)
d.Set("endpoint_type", endpointType)
return nil
}