-
Notifications
You must be signed in to change notification settings - Fork 649
/
resource_ibm_kms_key_rings.go
145 lines (128 loc) · 4.2 KB
/
resource_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
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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package kms
import (
"context"
"fmt"
"strings"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
kp "github.com/IBM/keyprotect-go-client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceIBMKmskeyRings() *schema.Resource {
return &schema.Resource{
Create: resourceIBMKmsKeyRingCreate,
Delete: resourceIBMKmsKeyRingDelete,
Read: resourceIBMKmsKeyRingRead,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: "Key protect Instance GUID",
ForceNew: true,
DiffSuppressFunc: suppressKMSInstanceIDDiff,
},
"key_ring_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "User defined unique ID for the key ring",
ValidateFunc: validate.InvokeValidator("ibm_kms_key_rings", "key_ring_id"),
},
"endpoint_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private"}),
Description: "public or private",
ForceNew: true,
},
},
}
}
func ResourceIBMKeyRingValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "key_ring_id",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `^[a-zA-Z0-9-]*$`,
MinValueLength: 2,
MaxValueLength: 100})
ibmKeyRingResourceValidator := validate.ResourceValidator{ResourceName: "ibm_kms_key_rings", Schema: validateSchema}
return &ibmKeyRingResourceValidator
}
func resourceIBMKmsKeyRingCreate(d *schema.ResourceData, meta interface{}) error {
instanceID := getInstanceIDFromCRN(d.Get("instance_id").(string))
keyRingID := d.Get("key_ring_id").(string)
kpAPI, instanceCRN, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
err = kpAPI.CreateKeyRing(context.Background(), keyRingID)
if err != nil {
return fmt.Errorf("[ERROR] Error while creating key ring : %s", err)
}
var keyRing string
keyRings, err := kpAPI.GetKeyRings(context.Background())
if err != nil {
return fmt.Errorf("[ERROR] Error while fetching key ring : %s", err)
}
for _, v := range keyRings.KeyRings {
if v.ID == keyRingID {
keyRing = v.ID
break
}
}
d.SetId(fmt.Sprintf("%s:keyRing:%s", keyRing, *instanceCRN))
return resourceIBMKmsKeyRingRead(d, meta)
}
func resourceIBMKmsKeyRingRead(d *schema.ResourceData, meta interface{}) error {
id := strings.Split(d.Id(), ":keyRing:")
if len(id) < 2 {
return fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of keyRingID:keyRing:InstanceCRN", d.Id())
}
instanceID := getInstanceIDFromCRN(id[1])
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
_, err = kpAPI.GetKeyRings(context.Background())
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Get Key Rings failed with error: %s", err)
}
d.Set("instance_id", instanceID)
if strings.Contains((kpAPI.URL).String(), "private") || strings.Contains(kpAPI.Config.BaseURL, "private") {
d.Set("endpoint_type", "private")
} else {
d.Set("endpoint_type", "public")
}
d.Set("key_ring_id", id[0])
return nil
}
func resourceIBMKmsKeyRingDelete(d *schema.ResourceData, meta interface{}) error {
id := strings.Split(d.Id(), ":keyRing:")
instanceID := getInstanceIDFromCRN(id[1])
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
err = kpAPI.DeleteKeyRing(context.Background(), id[0])
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
return nil
} else {
return fmt.Errorf(" failed to Destroy key ring with error: %s", err)
}
}
return nil
}