Skip to content

Commit 6ca4426

Browse files
fix: add check for 422 response on ionoscloud_s3_key key not found (#746)
1 parent b909c31 commit 6ca4426

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
### Fixes
33
- Fix [#735](https://github.com/ionos-cloud/terraform-provider-ionoscloud/issues/735) by reading all values for `api_subnet_allow_list`, not only non-nill values.
44
- Fix [#748](https://github.com/ionos-cloud/terraform-provider-ionoscloud/issues/748) by removing unecessary error check
5+
- S3 key creation fails with 422 if s3 key not found. Add function to check for that specific response from the API.
56
### Features
67
- Add new read-only attribute: `ipv4_cidr_block` to `ionoscloud_lan` resource and data source.
78
- Make `volume` optional for `ionoscloud_server` resource.

ionoscloud/resource_s3_key.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ionoscloud
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"log"
78
"strings"
@@ -102,7 +103,7 @@ func resourceS3KeyRead(ctx context.Context, d *schema.ResourceData, meta interfa
102103
logApiRequestTime(apiResponse)
103104

104105
if err != nil {
105-
if httpNotFound(apiResponse) {
106+
if httpNotFound(apiResponse) || isS3KeyNotFound(err) {
106107
d.SetId("")
107108
return nil
108109
}
@@ -141,7 +142,7 @@ func resourceS3KeyUpdate(ctx context.Context, d *schema.ResourceData, meta inter
141142
logApiRequestTime(apiResponse)
142143

143144
if err != nil {
144-
if httpNotFound(apiResponse) {
145+
if httpNotFound(apiResponse) || isS3KeyNotFound(err) {
145146
d.SetId("")
146147
return nil
147148
}
@@ -164,7 +165,7 @@ func resourceS3KeyDelete(ctx context.Context, d *schema.ResourceData, meta inter
164165
logApiRequestTime(apiResponse)
165166

166167
if err != nil {
167-
if httpNotFound(apiResponse) {
168+
if httpNotFound(apiResponse) || isS3KeyNotFound(err) {
168169
d.SetId("")
169170
return nil
170171
}
@@ -178,6 +179,10 @@ func resourceS3KeyDelete(ctx context.Context, d *schema.ResourceData, meta inter
178179
s3KeyDeleted, dsErr := s3KeyDeleted(ctx, client, d)
179180

180181
if dsErr != nil {
182+
if isS3KeyNotFound(dsErr) {
183+
log.Printf("[INFO] Successfully deleted Object Storage key: %s", d.Id())
184+
return nil
185+
}
181186
diags := diag.FromErr(fmt.Errorf("error while checking deletion status of Object Storage key %s: %w", d.Id(), dsErr))
182187
return diags
183188
}
@@ -200,6 +205,15 @@ func resourceS3KeyDelete(ctx context.Context, d *schema.ResourceData, meta inter
200205
return nil
201206
}
202207

208+
// isS3KeyNotFound needed because api returns 422 instead of 404 on key being not found. will be removed once API issue is fixed
209+
func isS3KeyNotFound(err error) bool {
210+
var genericOpenAPIError ionoscloud.GenericOpenAPIError
211+
if !errors.As(err, &genericOpenAPIError) {
212+
return false
213+
}
214+
return genericOpenAPIError.StatusCode() == 422 && strings.Contains(genericOpenAPIError.Error(), "[VDC-21-2] The access key cannot be found, please double-check the key id and try again.")
215+
}
216+
203217
func s3KeyDeleted(ctx context.Context, client *ionoscloud.APIClient, d *schema.ResourceData) (bool, error) {
204218
userId := d.Get("user_id").(string)
205219
_, apiResponse, err := client.UserS3KeysApi.UmUsersS3keysFindByKeyId(ctx, userId, d.Id()).Execute()
@@ -242,7 +256,7 @@ func resourceS3KeyImport(ctx context.Context, d *schema.ResourceData, meta inter
242256
logApiRequestTime(apiResponse)
243257

244258
if err != nil {
245-
if httpNotFound(apiResponse) {
259+
if httpNotFound(apiResponse) || isS3KeyNotFound(err) {
246260
d.SetId("")
247261
return nil, fmt.Errorf("unable to find Object Storage key %q", keyId)
248262
}

0 commit comments

Comments
 (0)