Skip to content

Commit f5a6215

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

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
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: 38 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"
@@ -29,6 +30,7 @@ func TestAccDataSourceEvent_Basic(t *testing.T) {
2930
require.NoError(t, err)
3031

3132
resource.ParallelTest(t, resource.TestCase{
33+
PreCheck: func() { acctest.PreCheck(t) },
3234
ProtoV6ProviderFactories: acctest.FakeSideProjectProviders(ctx, tt, project, iamAPIKey),
3335
CheckDestroy: resource.ComposeAggregateTestCheckFunc(
3436
func(_ *terraform.State) error {
@@ -139,3 +141,39 @@ func TestAccDataSourceEvent_Basic(t *testing.T) {
139141
},
140142
})
141143
}
144+
145+
func TestAccDataSourceEvent_Warning(t *testing.T) {
146+
// Test that a resource_type that is not yet supported on the
147+
// provider only raises a warning before calling the API
148+
// anyway (it could exist on API side)
149+
150+
// NOTE: Currently, we cannot programmatically assert that a warning was emitted
151+
// during the test step. This needs support from the testing framework:
152+
// https://github.com/hashicorp/terraform-plugin-testing/issues/69
153+
// Once implemented, we should add a check like:
154+
// 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`)
155+
156+
tt := acctest.NewTestTools(t)
157+
defer tt.Cleanup()
158+
159+
resource.ParallelTest(t, resource.TestCase{
160+
PreCheck: func() { acctest.PreCheck(t) },
161+
ProtoV6ProviderFactories: tt.ProviderFactories,
162+
CheckDestroy: testAccCheckSecretDestroy(tt),
163+
Steps: []resource.TestStep{
164+
{
165+
Config: `
166+
data "scaleway_audit_trail_event" "unknown_resource_type" {
167+
resource_type = "a_new_resource_type"
168+
}
169+
`,
170+
Check: resource.ComposeTestCheckFunc(
171+
resource.TestCheckResourceAttr("data.scaleway_audit_trail_event.unknown_resource_type", "events.#", "0"),
172+
),
173+
// In this test, we still expect a 400 from the API since `a_new_resource_type`
174+
// does not actually exist on API side.
175+
ExpectError: regexp.MustCompile(`400 Bad Request`),
176+
},
177+
},
178+
})
179+
}

0 commit comments

Comments
 (0)