Skip to content

Commit ade580e

Browse files
committed
refactor: remove GCP tags validation and improve type safety
Remove custom validation that prevented GCP configurations from using tags filters. Improve type safety in selector conversion by adding proper type checking for region and tags list values instead of unsafe type assertions. - Remove ResourceWithValidateConfig interface and ValidateConfig method - Add type checking for region and tags list conversions - Clean up struct field alignment in tests
1 parent d28efb9 commit ade580e

File tree

2 files changed

+29
-54
lines changed

2 files changed

+29
-54
lines changed

internal/cloud_security_group/cloud_security_group.go

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ import (
2929

3030
// Ensure the implementation satisfies the expected interfaces.
3131
var (
32-
_ resource.Resource = &cloudSecurityGroupResource{}
33-
_ resource.ResourceWithConfigure = &cloudSecurityGroupResource{}
34-
_ resource.ResourceWithImportState = &cloudSecurityGroupResource{}
35-
_ resource.ResourceWithValidateConfig = &cloudSecurityGroupResource{}
32+
_ resource.Resource = &cloudSecurityGroupResource{}
33+
_ resource.ResourceWithConfigure = &cloudSecurityGroupResource{}
34+
_ resource.ResourceWithImportState = &cloudSecurityGroupResource{}
3635
)
3736

3837
// cloudSecurityGroupScopes defines the required API scopes for Cloud Security Groups.
@@ -346,46 +345,6 @@ func (r *cloudSecurityGroupResource) Configure(
346345
r.client = client
347346
}
348347

349-
// ValidateConfig validates the resource configuration.
350-
func (r *cloudSecurityGroupResource) ValidateConfig(
351-
ctx context.Context,
352-
req resource.ValidateConfigRequest,
353-
resp *resource.ValidateConfigResponse,
354-
) {
355-
var config cloudSecurityGroupResourceModel
356-
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
357-
if resp.Diagnostics.HasError() {
358-
return
359-
}
360-
361-
// Validate that GCP configuration does not use tags filter
362-
if !config.GCP.IsNull() && !config.GCP.IsUnknown() {
363-
var gcpConfig cloudProviderConfigModel
364-
resp.Diagnostics.Append(config.GCP.As(ctx, &gcpConfig, basetypes.ObjectAsOptions{})...)
365-
if resp.Diagnostics.HasError() {
366-
return
367-
}
368-
369-
if !gcpConfig.Filters.IsNull() && !gcpConfig.Filters.IsUnknown() {
370-
// Extract filters attributes to check for tags
371-
filterValues := gcpConfig.Filters.Attributes()
372-
373-
// Check if tags field exists and has values
374-
if tagsValue, ok := filterValues["tags"]; ok && !tagsValue.IsNull() {
375-
var tags []string
376-
resp.Diagnostics.Append(tagsValue.(types.List).ElementsAs(ctx, &tags, false)...)
377-
if len(tags) > 0 {
378-
resp.Diagnostics.AddAttributeError(
379-
path.Root("gcp").AtName("filters").AtName("tags"),
380-
"Invalid Configuration",
381-
"GCP cloud resources do not support tag filtering. Remove the tags filter from the GCP configuration.",
382-
)
383-
}
384-
}
385-
}
386-
}
387-
}
388-
389348
// Create creates the resource and sets the initial Terraform state.
390349
func (r *cloudSecurityGroupResource) Create(
391350
ctx context.Context,
@@ -772,15 +731,31 @@ func (r *cloudSecurityGroupResource) convertSelectorsToAPI(
772731
// Extract region if present
773732
if regionValue, ok := filterValues["region"]; ok && !regionValue.IsNull() {
774733
var regions []string
775-
diags.Append(regionValue.(types.List).ElementsAs(ctx, &regions, false)...)
734+
regionList, ok := regionValue.(types.List)
735+
if !ok {
736+
diags.AddError(
737+
"Invalid Type",
738+
"Expected region to be a list type.",
739+
)
740+
return nil, diags
741+
}
742+
diags.Append(regionList.ElementsAs(ctx, &regions, false)...)
776743
apiFilters.Region = regions
777744
}
778745

779746
// Extract tags if present and includeTags is true
780747
if includeTags {
781748
if tagsValue, ok := filterValues["tags"]; ok && !tagsValue.IsNull() {
782749
var tags []string
783-
diags.Append(tagsValue.(types.List).ElementsAs(ctx, &tags, false)...)
750+
tagsList, ok := tagsValue.(types.List)
751+
if !ok {
752+
diags.AddError(
753+
"Invalid Type",
754+
"Expected tags to be a list type.",
755+
)
756+
return nil, diags
757+
}
758+
diags.Append(tagsList.ElementsAs(ctx, &tags, false)...)
784759
apiFilters.Tags = tags
785760
}
786761
}

internal/cloud_security_group/cloud_security_group_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ type groupConfig struct {
2121
Owners []string
2222

2323
// Cloud provider configs
24-
AWSAccountIDs []string
25-
AWSRegions []string
26-
AWSTags []string
27-
AzureAccountIDs []string
28-
AzureRegions []string
29-
AzureTags []string
30-
GCPAccountIDs []string
31-
GCPRegions []string
24+
AWSAccountIDs []string
25+
AWSRegions []string
26+
AWSTags []string
27+
AzureAccountIDs []string
28+
AzureRegions []string
29+
AzureTags []string
30+
GCPAccountIDs []string
31+
GCPRegions []string
3232

3333
// Image selectors
3434
Images []imageSelector

0 commit comments

Comments
 (0)