Skip to content

Commit 8f425a8

Browse files
committed
feat(kms): waiter for versions
1 parent 1ff737f commit 8f425a8

File tree

2 files changed

+295
-48
lines changed

2 files changed

+295
-48
lines changed

services/kms/wait/wait.go

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@ import (
1212
)
1313

1414
const (
15-
StatusKeyActive = "active"
16-
StatusKeyVersionNotReady = "version_not_ready"
17-
StatusKeyDeleted = "deleted"
15+
StatusKeyActive = "active"
16+
StatusKeyNotReady = "version_not_ready"
17+
StatusKeyDeleted = "deleted"
18+
)
19+
20+
const (
21+
StatusKeyVersionActive = "active"
22+
StatusKeyVersionKeyMaterialNotReady = "key_material_not_ready"
23+
StatusKeyVersionKeyMaterialInvalid = "key_material_invalid"
24+
StatusKeyVersionDisabled = "disabled"
25+
StatusKeyVersionDestroyed = "destroyed"
1826
)
1927

2028
type ApiKmsClient interface {
2129
GetKeyExecute(ctx context.Context, projectId string, regionId string, keyRingId string, keyId string) (*kms.Key, error)
30+
GetVersionExecute(ctx context.Context, projectId string, regionId string, keyRingId string, keyId string, versionNumber int64) (*kms.Version, error)
2231
}
2332

2433
func CreateOrUpdateKeyWaitHandler(ctx context.Context, client ApiKmsClient, projectId, region, keyRingId, keyId string) *wait.AsyncActionHandler[kms.Key] {
@@ -30,7 +39,7 @@ func CreateOrUpdateKeyWaitHandler(ctx context.Context, client ApiKmsClient, proj
3039

3140
if response.State != nil {
3241
switch *response.State {
33-
case StatusKeyVersionNotReady:
42+
case StatusKeyNotReady:
3443
return false, nil, nil
3544
default:
3645
return true, response, nil
@@ -43,9 +52,55 @@ func CreateOrUpdateKeyWaitHandler(ctx context.Context, client ApiKmsClient, proj
4352
return handler
4453
}
4554

46-
func DeleteKeyWaitHandler(ctx context.Context, client ApiKmsClient, projectId, region, keyRingId,keyId string) *wait.AsyncActionHandler[kms.Key] {
55+
func DeleteKeyWaitHandler(ctx context.Context, client ApiKmsClient, projectId, region, keyRingId, keyId string) *wait.AsyncActionHandler[kms.Key] {
4756
handler := wait.New(func() (bool, *kms.Key, error) {
48-
_, err := client.GetKeyExecute(ctx,projectId,region,keyRingId, keyId)
57+
_, err := client.GetKeyExecute(ctx, projectId, region, keyRingId, keyId)
58+
if err != nil {
59+
var apiErr *oapierror.GenericOpenAPIError
60+
if errors.As(err, &apiErr) {
61+
if statusCode := apiErr.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone {
62+
return true, nil, nil
63+
}
64+
}
65+
return true, nil, err
66+
}
67+
return false, nil, nil
68+
})
69+
handler.SetTimeout(10 * time.Minute)
70+
return handler
71+
}
72+
73+
func EnableKeyVersionWaitHandler(ctx context.Context, client ApiKmsClient, projectId, region, keyRingId, keyId string, version int64) *wait.AsyncActionHandler[kms.Version] {
74+
handler := wait.New(func() (bool, *kms.Version, error) {
75+
response, err := client.GetVersionExecute(ctx, projectId, region, keyRingId, keyId, version)
76+
if err != nil {
77+
return false, nil, err
78+
}
79+
80+
if response.State != nil {
81+
switch *response.State {
82+
case StatusKeyVersionDestroyed:
83+
return true, response, nil
84+
case StatusKeyVersionKeyMaterialInvalid:
85+
return true, response, nil
86+
case StatusKeyVersionDisabled:
87+
return true, response, nil
88+
case StatusKeyVersionKeyMaterialNotReady:
89+
return false, nil, nil
90+
default:
91+
return true, response, nil
92+
}
93+
}
94+
95+
return false, nil, nil
96+
})
97+
handler.SetTimeout(10 * time.Minute)
98+
return handler
99+
}
100+
101+
func DisableKeyVersionWaitHandler(ctx context.Context, client ApiKmsClient, projectId, region, keyRingId, keyId string, version int64) *wait.AsyncActionHandler[kms.Version] {
102+
handler := wait.New(func() (bool, *kms.Version, error) {
103+
_, err := client.GetVersionExecute(ctx, projectId, region, keyRingId, keyId, version)
49104
if err != nil {
50105
var apiErr *oapierror.GenericOpenAPIError
51106
if errors.As(err, &apiErr) {

0 commit comments

Comments
 (0)