Skip to content

Commit 12845fc

Browse files
fix: validate resource_type with warning instead of error
1 parent 744af35 commit 12845fc

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

internal/services/audittrail/event_data_source.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strconv"
66

77
"github.com/google/uuid"
8+
"github.com/hashicorp/go-cty/cty"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
audittrailSDK "github.com/scaleway/scaleway-sdk-go/api/audit_trail/v1alpha1"
@@ -32,10 +33,19 @@ func DataSourceEvent() *schema.Resource {
3233
ValidateDiagFunc: verify.IsUUID(),
3334
},
3435
"resource_type": {
35-
Type: schema.TypeString,
36-
Description: "Type of the scaleway resources associated with the listed events",
37-
Optional: true,
38-
ValidateDiagFunc: verify.ValidateEnum[audittrailSDK.ResourceType](),
36+
Type: schema.TypeString,
37+
Description: "Type of the scaleway resources associated with the listed events",
38+
Optional: true,
39+
ValidateDiagFunc: func(i any, p cty.Path) diag.Diagnostics {
40+
resourceTypeValues := audittrailSDK.ResourceType("").Values()
41+
42+
resourceTypeStringValues := make([]string, 0, len(resourceTypeValues))
43+
for _, resourceTypeValue := range resourceTypeValues {
44+
resourceTypeStringValues = append(resourceTypeStringValues, resourceTypeValue.String())
45+
}
46+
47+
return verify.ValidateStringInSliceWithWarning(resourceTypeStringValues, "resourceType")(i, p)
48+
},
3949
},
4050
"resource_id": {
4151
Type: schema.TypeString,

internal/services/audittrail/event_data_source_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package audittrail_test
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/google/uuid"
@@ -139,3 +140,37 @@ func TestAccDataSourceEvent_Basic(t *testing.T) {
139140
},
140141
})
141142
}
143+
144+
func TestAccDataSourceEvent_Warning(t *testing.T) {
145+
// Test that a resource_type that is not yet supported on the
146+
// provider only raises a warning before calling the API
147+
// anyway (it could exist on API side)
148+
149+
// NOTE: Currently, we cannot programmatically assert that a warning was emitted
150+
// during the test step. This needs support from the testing framework:
151+
// https://github.com/hashicorp/terraform-plugin-testing/issues/69
152+
// Once implemented, we should add a check like:
153+
// ExpectWarning: regexp.MustCompile(`expected resourceType to be one of [\"unknown_type\" \"secm_secret\" \"secm_secret_version\" \"kube_cluster\" \"kube_pool\" \"kube_node\" \"kube_acl\" \"keym_key\" \"iam_user\" \"iam_application\" \"iam_group\" \"iam_policy\" \"iam_api_key\" \"iam_ssh_key\" \"iam_rule\" \"iam_saml\" \"iam_saml_certificate\" \"secret_manager_secret\" \"secret_manager_version\" \"key_manager_key\" \"account_user\" \"account_organization\" \"account_project\" \"instance_server\" \"instance_placement_group\" \"instance_security_group\" \"instance_volume\" \"instance_snapshot\" \"instance_image\" \"apple_silicon_server\" \"baremetal_server\" \"baremetal_setting\" \"ipam_ip\" \"sbs_volume\" \"sbs_snapshot\" \"load_balancer_lb\" \"load_balancer_ip\" \"load_balancer_frontend\" \"load_balancer_backend\" \"load_balancer_route\" \"load_balancer_acl\" \"load_balancer_certificate\" \"sfs_filesystem\" \"vpc_private_network\"], got a_new_resource_type`)
154+
tt := acctest.NewTestTools(t)
155+
defer tt.Cleanup()
156+
157+
resource.ParallelTest(t, resource.TestCase{
158+
ProtoV6ProviderFactories: tt.ProviderFactories,
159+
CheckDestroy: testAccCheckSecretDestroy(tt),
160+
Steps: []resource.TestStep{
161+
{
162+
Config: `
163+
data "scaleway_audit_trail_event" "unknown_resource_type" {
164+
resource_type = "a_new_resource_type"
165+
}
166+
`,
167+
Check: resource.ComposeTestCheckFunc(
168+
resource.TestCheckResourceAttr("data.scaleway_audit_trail_event.unknown_resource_type", "events.#", "0"),
169+
),
170+
// In this test, we still expect a 400 from the API since `a_new_resource_type`
171+
// does not actually exist on API side.
172+
ExpectError: regexp.MustCompile(`400 Bad Request`),
173+
},
174+
},
175+
})
176+
}

internal/services/audittrail/helpers_test.go

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

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"testing"
78
"time"
@@ -55,9 +56,8 @@ func waitForAuditTrailEvents(t *testing.T, ctx context.Context, api *audit_trail
5556
}
5657

5758
// Not found yet
58-
return retry.RetryableError(fmt.Errorf("audit event not found yet for resource, retrying..."))
59+
return retry.RetryableError(errors.New("audit event not found yet for resource, retrying..."))
5960
})
60-
6161
if err != nil {
6262
t.Fatalf("timed out waiting for audit trail event: %v", err)
6363
}

0 commit comments

Comments
 (0)