Skip to content

Commit

Permalink
TypedSDK - add filtering tag for removed schema items (hashicorp#23415
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jackofallops authored Oct 9, 2023
1 parent 1bd5b7c commit 0b84d31
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
12 changes: 10 additions & 2 deletions contributing/topics/guide-new-fields-to-resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"`
}
```
6 changes: 5 additions & 1 deletion internal/sdk/resource_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions internal/services/appservice/helpers/app_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down

0 comments on commit 0b84d31

Please sign in to comment.