Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add check for 422 response on s3 key not found #746

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 6.7.0
### 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 [#735](https://github.com/ionos-cloud/terraform-provider-ionoscloud/issues/735) by reading all values for `api_subnet_allow_list`, not only non-nil values.
- 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
Loading