From 0b84d31c9c97ad8920a54d47e09274155c8353ec Mon Sep 17 00:00:00 2001 From: jackofallops <11830746+jackofallops@users.noreply.github.com> Date: Mon, 9 Oct 2023 05:43:09 +0100 Subject: [PATCH] `TypedSDK` - add filtering tag for removed schema items (#23415) --- contributing/topics/guide-new-fields-to-resource.md | 12 ++++++++++-- internal/sdk/resource_encode.go | 6 +++++- internal/services/appservice/helpers/app_stack.go | 10 +++++----- ...omation_software_update_configuration_resource.go | 5 ++--- ...te_recovery_replication_recovery_plan_resource.go | 2 +- ...tinel_log_analytics_workspace_onboard_resource.go | 4 ++-- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/contributing/topics/guide-new-fields-to-resource.md b/contributing/topics/guide-new-fields-to-resource.md index 4bf410977720..1ab28e86820e 100755 --- a/contributing/topics/guide-new-fields-to-resource.md +++ b/contributing/topics/guide-new-fields-to-resource.md @@ -118,8 +118,6 @@ Schema: map[string]*pluginsdk.Schema{ After deprecation the schema might look like the example below. -TODO: how is this done for typed resources? - ```go func resource() *pluginsdk.Resource { resource := &pluginsdk.Resource{ @@ -185,3 +183,13 @@ func read() { ... } ``` + +When deprecating a property in a Typed Resource it is important to ensure that the Go struct representing the schema is correctly tagged to prevent the SDK decoding the removed property when the major version beta / feature flag is in use. In these cases the struct tags must be updated to include `,removedInNextMajorVersion`. + +```go +type ExampleResourceModel struct { + Name string `tfschema:"name"` + EnablePublicNetworkAccess bool `tfschema:"enable_public_network_access,removedInNextMajorVersion"` + PublicNetworkAccessEnabled bool `tfschema:"public_network_access_enabled"` +} +``` \ No newline at end of file diff --git a/internal/sdk/resource_encode.go b/internal/sdk/resource_encode.go index eca58016fd9a..9a93345f22d1 100644 --- a/internal/sdk/resource_encode.go +++ b/internal/sdk/resource_encode.go @@ -6,6 +6,9 @@ package sdk import ( "fmt" "reflect" + "strings" + + "github.com/hashicorp/terraform-provider-azurerm/internal/features" ) // Encode will encode the specified object into the Terraform State @@ -51,7 +54,8 @@ func recurse(objType reflect.Type, objVal reflect.Value, fieldName string, debug for i := 0; i < objType.NumField(); i++ { field := objType.Field(i) fieldVal := objVal.Field(i) - if tfschemaTag, exists := field.Tag.Lookup("tfschema"); exists { + if tfschemaTag, exists := field.Tag.Lookup("tfschema"); exists && !(strings.Contains(tfschemaTag, "removedInNextMajorVersion") && features.FourPointOh()) { + tfschemaTag = strings.TrimSuffix(tfschemaTag, ",removedInNextMajorVersion") switch field.Type.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: iv := fieldVal.Int() diff --git a/internal/services/appservice/helpers/app_stack.go b/internal/services/appservice/helpers/app_stack.go index 2a7044429c39..ff9c74aafdb6 100644 --- a/internal/services/appservice/helpers/app_stack.go +++ b/internal/services/appservice/helpers/app_stack.go @@ -31,9 +31,9 @@ const ( type ApplicationStackWindows struct { CurrentStack string `tfschema:"current_stack"` - DockerContainerName string `tfschema:"docker_container_name"` - DockerContainerRegistry string `tfschema:"docker_container_registry"` - DockerContainerTag string `tfschema:"docker_container_tag"` + DockerContainerName string `tfschema:"docker_container_name,removedInNextMajorVersion"` + DockerContainerRegistry string `tfschema:"docker_container_registry,removedInNextMajorVersion"` + DockerContainerTag string `tfschema:"docker_container_tag,removedInNextMajorVersion"` JavaContainer string `tfschema:"java_container"` JavaContainerVersion string `tfschema:"java_container_version"` JavaEmbeddedServer bool `tfschema:"java_embedded_server_enabled"` @@ -418,8 +418,8 @@ type ApplicationStackLinux struct { JavaVersion string `tfschema:"java_version"` JavaServer string `tfschema:"java_server"` JavaServerVersion string `tfschema:"java_server_version"` - DockerImageTag string `tfschema:"docker_image_tag"` - DockerImage string `tfschema:"docker_image"` + DockerImageTag string `tfschema:"docker_image_tag,removedInNextMajorVersion"` + DockerImage string `tfschema:"docker_image,removedInNextMajorVersion"` RubyVersion string `tfschema:"ruby_version"` DockerRegistryUrl string `tfschema:"docker_registry_url"` diff --git a/internal/services/automation/automation_software_update_configuration_resource.go b/internal/services/automation/automation_software_update_configuration_resource.go index d79d95170083..08309215c461 100644 --- a/internal/services/automation/automation_software_update_configuration_resource.go +++ b/internal/services/automation/automation_software_update_configuration_resource.go @@ -63,7 +63,7 @@ type Linux struct { ExcludedPackages []string `tfschema:"excluded_packages"` IncludedPackages []string `tfschema:"included_packages"` - Classification string `tfschema:"classification_included"` // Deprecated use Classifications instead + Classification string `tfschema:"classification_included,removedInNextMajorVersion"` } type MonthlyOccurrence struct { @@ -111,8 +111,7 @@ type Windows struct { IncludedKbs []string `tfschema:"included_knowledge_base_numbers"` RebootSetting string `tfschema:"reboot"` - Classification string `tfschema:"classification_included"` // Deprecated use Classifications instead - + Classification string `tfschema:"classification_included,removedInNextMajorVersion"` } type SoftwareUpdateConfigurationModel struct { diff --git a/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource.go b/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource.go index 0c591ba060cf..626f5691bb93 100644 --- a/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource.go +++ b/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource.go @@ -29,7 +29,7 @@ type SiteRecoveryReplicationRecoveryPlanModel struct { ShutdownRecoveryGroup []GenericRecoveryGroupModel `tfschema:"shutdown_recovery_group"` FailoverRecoveryGroup []GenericRecoveryGroupModel `tfschema:"failover_recovery_group"` BootRecoveryGroup []BootRecoveryGroupModel `tfschema:"boot_recovery_group"` - RecoveryGroup []RecoveryGroupModel `tfschema:"recovery_group"` + RecoveryGroup []RecoveryGroupModel `tfschema:"recovery_group,removedInNextMajorVersion"` RecoveryVaultId string `tfschema:"recovery_vault_id"` SourceRecoveryFabricId string `tfschema:"source_recovery_fabric_id"` TargetRecoveryFabricId string `tfschema:"target_recovery_fabric_id"` diff --git a/internal/services/sentinel/sentinel_log_analytics_workspace_onboard_resource.go b/internal/services/sentinel/sentinel_log_analytics_workspace_onboard_resource.go index ff3d5377ad58..80f779d8545e 100644 --- a/internal/services/sentinel/sentinel_log_analytics_workspace_onboard_resource.go +++ b/internal/services/sentinel/sentinel_log_analytics_workspace_onboard_resource.go @@ -21,8 +21,8 @@ import ( ) type SecurityInsightsSentinelOnboardingStateModel struct { - ResourceGroupName string `tfschema:"resource_group_name"` - WorkspaceName string `tfschema:"workspace_name"` + ResourceGroupName string `tfschema:"resource_group_name,removedInNextMajorVersion"` + WorkspaceName string `tfschema:"workspace_name,removedInNextMajorVersion"` CustomerManagedKeyEnabled bool `tfschema:"customer_managed_key_enabled"` WorkspaceId string `tfschema:"workspace_id"` }