Skip to content

Commit

Permalink
fix: add check for 422 response on ionoscloud_s3_key key not found (#746
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cristiGuranIonos authored Jan 29, 2025
1 parent b909c31 commit 6ca4426
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
### Fixes
- 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.
- Fix [#748](https://github.com/ionos-cloud/terraform-provider-ionoscloud/issues/748) by removing unecessary error check
- S3 key creation fails with 422 if s3 key not found. Add function to check for that specific response from the API.
### Features
- Add new read-only attribute: `ipv4_cidr_block` to `ionoscloud_lan` resource and data source.
- Make `volume` optional for `ionoscloud_server` resource.
Expand Down
22 changes: 18 additions & 4 deletions ionoscloud/resource_s3_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ionoscloud

import (
"context"
"errors"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -102,7 +103,7 @@ func resourceS3KeyRead(ctx context.Context, d *schema.ResourceData, meta interfa
logApiRequestTime(apiResponse)

if err != nil {
if httpNotFound(apiResponse) {
if httpNotFound(apiResponse) || isS3KeyNotFound(err) {
d.SetId("")
return nil
}
Expand Down Expand Up @@ -141,7 +142,7 @@ func resourceS3KeyUpdate(ctx context.Context, d *schema.ResourceData, meta inter
logApiRequestTime(apiResponse)

if err != nil {
if httpNotFound(apiResponse) {
if httpNotFound(apiResponse) || isS3KeyNotFound(err) {
d.SetId("")
return nil
}
Expand All @@ -164,7 +165,7 @@ func resourceS3KeyDelete(ctx context.Context, d *schema.ResourceData, meta inter
logApiRequestTime(apiResponse)

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

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

// isS3KeyNotFound needed because api returns 422 instead of 404 on key being not found. will be removed once API issue is fixed
func isS3KeyNotFound(err error) bool {
var genericOpenAPIError ionoscloud.GenericOpenAPIError
if !errors.As(err, &genericOpenAPIError) {
return false
}
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.")
}

func s3KeyDeleted(ctx context.Context, client *ionoscloud.APIClient, d *schema.ResourceData) (bool, error) {
userId := d.Get("user_id").(string)
_, apiResponse, err := client.UserS3KeysApi.UmUsersS3keysFindByKeyId(ctx, userId, d.Id()).Execute()
Expand Down Expand Up @@ -242,7 +256,7 @@ func resourceS3KeyImport(ctx context.Context, d *schema.ResourceData, meta inter
logApiRequestTime(apiResponse)

if err != nil {
if httpNotFound(apiResponse) {
if httpNotFound(apiResponse) || isS3KeyNotFound(err) {
d.SetId("")
return nil, fmt.Errorf("unable to find Object Storage key %q", keyId)
}
Expand Down

0 comments on commit 6ca4426

Please sign in to comment.