Skip to content

Commit 721581b

Browse files
kms cancel deletion functionality and test (#369)
[KMS] Canceling the Scheduled Deletion of a CMK What this PR does / why we need it Add ability to canceling the Scheduled Deletion of a CMK Which issue this PR fixes Resolves: #368 Tests: === RUN TestKmsKeysLifecycle --- PASS: TestKmsKeysLifecycle(68.05s) PASS Reviewed-by: Artem Lifshits <None> Reviewed-by: Rodion Gyrbu <fpsoff@outlook.com> Reviewed-by: Vladimir Vshivkov <None>
1 parent a530b64 commit 721581b

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package v1
2+
3+
import (
4+
"testing"
5+
6+
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
7+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/kms/v1/keys"
8+
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
9+
)
10+
11+
func TestKmsKeysLifecycle(t *testing.T) {
12+
client, err := clients.NewKMSV1Client()
13+
th.AssertNoErr(t, err)
14+
15+
kmsID := clients.EnvOS.GetEnv("KMS_ID")
16+
if kmsID == "" {
17+
t.Skip("OS_KMS_ID env var is missing but KMSv1 grant test requires")
18+
}
19+
20+
createOpts := keys.CreateOpts{
21+
KeyAlias: kmsID,
22+
KeyDescription: "some description",
23+
}
24+
createKey, err := keys.Create(client, createOpts).ExtractKeyInfo()
25+
th.AssertNoErr(t, err)
26+
27+
defer func() {
28+
deleteOpts := keys.DeleteOpts{
29+
KeyID: createKey.KeyID,
30+
PendingDays: "7",
31+
}
32+
_, err := keys.Delete(client, deleteOpts).Extract()
33+
th.AssertNoErr(t, err)
34+
}()
35+
36+
keyGet, err := keys.Get(client, createKey.KeyID).ExtractKeyInfo()
37+
th.AssertNoErr(t, err)
38+
th.AssertEquals(t, createOpts.KeyAlias, keyGet.KeyAlias)
39+
th.AssertEquals(t, keyGet.KeyState, "2")
40+
41+
deleteOpts := keys.DeleteOpts{
42+
KeyID: createKey.KeyID,
43+
PendingDays: "7",
44+
}
45+
_, err = keys.Delete(client, deleteOpts).Extract()
46+
th.AssertNoErr(t, err)
47+
48+
keyGetDeleted, err := keys.Get(client, createKey.KeyID).ExtractKeyInfo()
49+
th.AssertNoErr(t, err)
50+
th.AssertEquals(t, keyGetDeleted.KeyState, "4")
51+
52+
cancelDeleteOpts := keys.CancelDeleteOpts{
53+
KeyID: createKey.KeyID,
54+
}
55+
_, err = keys.CancelDelete(client, cancelDeleteOpts).Extract()
56+
th.AssertNoErr(t, err)
57+
58+
_, err = keys.EnableKey(client, createKey.KeyID).Extract()
59+
th.AssertNoErr(t, err)
60+
61+
keyGetEnabled, err := keys.Get(client, createKey.KeyID).ExtractKeyInfo()
62+
th.AssertNoErr(t, err)
63+
th.AssertEquals(t, keyGetEnabled.KeyState, "2")
64+
65+
_, err = keys.DisableKey(client, createKey.KeyID).Extract()
66+
th.AssertNoErr(t, err)
67+
68+
keyGetDisabled, err := keys.Get(client, createKey.KeyID).ExtractKeyInfo()
69+
th.AssertNoErr(t, err)
70+
th.AssertEquals(t, keyGetDisabled.KeyState, "3")
71+
}

openstack/kms/v1/keys/requests.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ type DeleteOpts struct {
2424
PendingDays string `json:"pending_days" required:"true"`
2525
}
2626

27+
type CancelDeleteOpts struct {
28+
// ID of a CMK
29+
KeyID string `json:"key_id" required:"true"`
30+
}
31+
2732
type UpdateAliasOpts struct {
2833
// ID of a CMK
2934
KeyID string `json:"key_id" required:"true"`
@@ -89,6 +94,12 @@ func (opts DeleteOpts) ToKeyDeleteMap() (map[string]interface{}, error) {
8994
return golangsdk.BuildRequestBody(opts, "")
9095
}
9196

97+
// ToKeyCancelDeleteMap assembles a request body based on the contents of a
98+
// CancelDeleteOpts.
99+
func (opts CancelDeleteOpts) ToKeyCancelDeleteMap() (map[string]interface{}, error) {
100+
return golangsdk.BuildRequestBody(opts, "")
101+
}
102+
92103
// ToKeyUpdateAliasMap assembles a request body based on the contents of a
93104
// UpdateAliasOpts.
94105
func (opts UpdateAliasOpts) ToKeyUpdateAliasMap() (map[string]interface{}, error) {
@@ -127,6 +138,10 @@ type DeleteOptsBuilder interface {
127138
ToKeyDeleteMap() (map[string]interface{}, error)
128139
}
129140

141+
type CancelDeleteOptsBuilder interface {
142+
ToKeyCancelDeleteMap() (map[string]interface{}, error)
143+
}
144+
130145
type UpdateAliasOptsBuilder interface {
131146
ToKeyUpdateAliasMap() (map[string]interface{}, error)
132147
}
@@ -339,3 +354,17 @@ func UpdateKeyRotationInterval(client *golangsdk.ServiceClient, opts RotationOpt
339354
})
340355
return
341356
}
357+
358+
// CancelDelete will cancel the scheduled deletion for a CMK only when the CMK's status is Scheduled deletion with the provided ID.
359+
func CancelDelete(client *golangsdk.ServiceClient, opts CancelDeleteOptsBuilder) (r DeleteResult) {
360+
b, err := opts.ToKeyCancelDeleteMap()
361+
if err != nil {
362+
r.Err = err
363+
return
364+
}
365+
_, r.Err = client.Post(cancelDeleteURL(client), b, &r.Body, &golangsdk.RequestOpts{
366+
OkCodes: []int{200},
367+
JSONResponse: &r.Body,
368+
})
369+
return
370+
}

openstack/kms/v1/keys/results.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ type DeleteResult struct {
8484
commonResult
8585
}
8686

87+
// CancelDeleteResult contains the response body and error from a CancelDelete request.
88+
type CancelDeleteResult struct {
89+
commonResult
90+
}
91+
8792
// UpdateAliasResult contains the response body and error from a UpdateAlias request.
8893
type UpdateAliasResult struct {
8994
commonResult

openstack/kms/v1/keys/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ func disableKeyURL(c *golangsdk.ServiceClient) string {
6161
func listURL(c *golangsdk.ServiceClient) string {
6262
return c.ServiceURL(resourcePath, "list-keys")
6363
}
64+
65+
func cancelDeleteURL(c *golangsdk.ServiceClient) string {
66+
return c.ServiceURL(resourcePath, "cancel-key-deletion")
67+
}

0 commit comments

Comments
 (0)