-
Notifications
You must be signed in to change notification settings - Fork 649
/
resource_ibm_kms_key_alias.go
140 lines (130 loc) · 3.92 KB
/
resource_ibm_kms_key_alias.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
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 ResourceIBMKmskeyAlias() *schema.Resource {
return &schema.Resource{
Create: resourceIBMKmsKeyAliasCreate,
Delete: resourceIBMKmsKeyAliasDelete,
Read: resourceIBMKmsKeyAliasRead,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: "Key ID",
ForceNew: true,
DiffSuppressFunc: suppressKMSInstanceIDDiff,
},
"alias": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Key protect or hpcs key alias name",
},
"key_id": {
Type: schema.TypeString,
Optional: true,
Description: "Key ID",
ForceNew: true,
ExactlyOneOf: []string{"key_id", "existing_alias"},
},
"existing_alias": {
Type: schema.TypeString,
Optional: true,
Description: "Existing Alias of the Key",
ForceNew: true,
ExactlyOneOf: []string{"key_id", "existing_alias"},
},
"endpoint_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private"}),
Description: "public or private",
ForceNew: true,
},
},
}
}
func resourceIBMKmsKeyAliasCreate(d *schema.ResourceData, meta interface{}) error {
instanceID := getInstanceIDFromCRN(d.Get("instance_id").(string))
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
aliasName := d.Get("alias").(string)
var id string
if v, ok := d.GetOk("key_id"); ok {
id = v.(string)
d.Set("key_id", id)
}
if v, ok := d.GetOk("existing_alias"); ok {
id = v.(string)
}
stkey, err := kpAPI.CreateKeyAlias(context.Background(), aliasName, id)
if err != nil {
return fmt.Errorf("[ERROR] Error while creating alias name for the key: %s", err)
}
key, err := kpAPI.GetKey(context.Background(), stkey.KeyID)
if err != nil {
return fmt.Errorf("[ERROR] Get Key failed with error: %s", err)
}
d.SetId(fmt.Sprintf("%s:alias:%s", stkey.Alias, key.CRN))
return resourceIBMKmsKeyAliasRead(d, meta)
}
func resourceIBMKmsKeyAliasRead(d *schema.ResourceData, meta interface{}) error {
id := strings.Split(d.Id(), ":alias:")
if len(id) < 2 {
return fmt.Errorf("[ERROR] Incorrect ID %s: Id should be a combination of keyAlias:alias:keyCRN", d.Id())
}
_, instanceID, keyid := getInstanceAndKeyDataFromCRN(id[1])
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
key, err := kpAPI.GetKey(context.Background(), keyid)
if err != nil {
kpError := err.(*kp.Error)
if kpError.StatusCode == 404 || kpError.StatusCode == 409 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Get Key failed with error while reading policies: %s", err)
} else if key.State == 5 { //Refers to Deleted state of the Key
d.SetId("")
return nil
}
d.Set("alias", id[0])
d.Set("key_id", key.ID)
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")
}
return nil
}
func resourceIBMKmsKeyAliasDelete(d *schema.ResourceData, meta interface{}) error {
id := strings.Split(d.Id(), ":alias:")
_, instanceID, keyid := getInstanceAndKeyDataFromCRN(id[1])
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
err1 := kpAPI.DeleteKeyAlias(context.Background(), id[0], keyid)
if err1 != nil {
kpError := err1.(*kp.Error)
if kpError.StatusCode == 404 {
return nil
} else {
return fmt.Errorf(" failed to Destroy alias with error: %s", err1)
}
}
return nil
}