Skip to content

Commit

Permalink
Merge pull request #15505 from hashicorp/r/common-schema
Browse files Browse the repository at this point in the history
refactor: moving smaller usages of Location/Resource Group to `commonschema`
  • Loading branch information
tombuildsstuff authored Feb 18, 2022
2 parents 8d3fb77 + 6dce739 commit a22742c
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 83 deletions.
13 changes: 5 additions & 8 deletions helpers/azure/location.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package azure

import (
"github.com/hashicorp/terraform-provider-azurerm/internal/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

func SchemaLocation() *pluginsdk.Schema {
return location.Schema()
}

func SchemaLocationOptional() *pluginsdk.Schema {
return location.SchemaOptional()
return commonschema.Location()
}

func SchemaLocationForDataSource() *pluginsdk.Schema {
return location.SchemaComputed()
return commonschema.LocationComputed()
}

// azure.NormalizeLocation is a function which normalises human-readable region/location
// NormalizeLocation is a function which normalises human-readable region/location
// names (e.g. "West US") to the values used and returned by the Azure API (e.g. "westus").
// In state we track the API internal version as it is easier to go from the human form
// to the canonical form than the other way around.
Expand Down
48 changes: 0 additions & 48 deletions helpers/azure/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,6 @@ func SchemaResourceGroupName() *pluginsdk.Schema {
}
}

func SchemaResourceGroupNameDeprecated() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: ValidateResourceGroupName,
Deprecated: "This field is no longer used and will be removed in the next major version of the Azure Provider",
}
}

func SchemaResourceGroupNameDeprecatedComputed() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: ValidateResourceGroupName,
Deprecated: "This field is no longer used and will be removed in the next major version of the Azure Provider",
}
}

func SchemaResourceGroupNameDiffSuppress() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Expand All @@ -55,35 +36,6 @@ func SchemaResourceGroupNameForDataSource() *pluginsdk.Schema {
}
}

func SchemaResourceGroupNameOptionalComputed() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
ValidateFunc: ValidateResourceGroupName,
}
}

func SchemaResourceGroupNameOptional() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: ValidateResourceGroupName,
}
}

func SchemaResourceGroupNameSetOptional() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: ValidateResourceGroupName,
},
}
}

func ValidateResourceGroupName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

Expand Down
8 changes: 0 additions & 8 deletions internal/location/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ func Schema() *pluginsdk.Schema {
return commonschema.Location()
}

func SchemaOptional() *pluginsdk.Schema {
return commonschema.LocationOptional()
}

func SchemaComputed() *pluginsdk.Schema {
return commonschema.LocationComputed()
}

func SchemaWithoutForceNew() *pluginsdk.Schema {
return commonschema.LocationWithoutForceNew()
}

func DiffSuppressFunc(v, old, new string, d *pluginsdk.ResourceData) bool {
return location.DiffSuppressFunc(v, old, new, d)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/advisor/mgmt/2020-01-01/advisor"
"github.com/gofrs/uuid"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -41,7 +41,7 @@ func dataSourceAdvisorRecommendations() *pluginsdk.Resource {
},
},

"filter_by_resource_groups": azure.SchemaResourceGroupNameSetOptional(),
"filter_by_resource_groups": commonschema.ResourceGroupNameSetOptional(),

"recommendations": {
Type: pluginsdk.TypeList,
Expand Down
2 changes: 1 addition & 1 deletion internal/services/apimanagement/api_management_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func resourceApiManagementSchema() map[string]*pluginsdk.Schema {
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"location": location.SchemaWithoutForceNew(),
"location": commonschema.LocationWithoutForceNew(),

"virtual_network_configuration": {
Type: pluginsdk.TypeList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/botservice/mgmt/2021-03-01/botservice"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down Expand Up @@ -58,7 +59,7 @@ func resourceBotChannelDirectLineSpeech() *pluginsdk.Resource {
ValidateFunc: validation.StringIsNotEmpty,
},

"cognitive_service_location": location.SchemaWithoutForceNew(),
"cognitive_service_location": commonschema.LocationWithoutForceNew(),

"custom_speech_model_id": {
Type: schema.TypeString,
Expand Down
6 changes: 3 additions & 3 deletions internal/services/containers/container_registry_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/parse"
validate2 "github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/validate"
containerValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/containers/validate"
keyVaultValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -857,7 +857,7 @@ func resourceContainerRegistrySchema() map[string]*pluginsdk.Schema {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate2.ContainerRegistryName,
ValidateFunc: containerValidate.ContainerRegistryName,
},

"resource_group_name": azure.SchemaResourceGroupName(),
Expand Down Expand Up @@ -903,7 +903,7 @@ func resourceContainerRegistrySchema() map[string]*pluginsdk.Schema {
}(),
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"location": location.SchemaWithoutForceNew(),
"location": commonschema.LocationWithoutForceNew(),

"zone_redundancy_enabled": {
Type: pluginsdk.TypeBool,
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/cosmosdb_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func resourceCosmosDbAccount() *pluginsdk.Resource {
Computed: true,
},

"location": location.SchemaWithoutForceNew(),
"location": commonschema.LocationWithoutForceNew(),

"failover_priority": {
Type: pluginsdk.TypeInt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -29,7 +29,7 @@ func dataSourceCosmosDbRestorableDatabaseAccounts() *pluginsdk.Resource {
ValidateFunc: validate.CosmosAccountName,
},

"location": location.SchemaWithoutForceNew(),
"location": commonschema.LocationWithoutForceNew(),

"accounts": {
Type: pluginsdk.TypeList,
Expand Down
2 changes: 1 addition & 1 deletion internal/services/firewall/firewall_policy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ func resourceFirewallPolicySchema() map[string]*pluginsdk.Schema {
Required: true,
ValidateFunc: logAnalytiscValidate.LogAnalyticsWorkspaceID,
},
"firewall_location": location.SchemaWithoutForceNew(),
"firewall_location": commonschema.LocationWithoutForceNew(),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/services/iothub/iothub_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func resourceIotHub() *pluginsdk.Resource {
ValidateFunc: iothubValidate.FileNameFormat,
},

"resource_group_name": azure.SchemaResourceGroupNameOptional(),
"resource_group_name": commonschema.ResourceGroupNameOptional(),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-05-01/network"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
Expand Down Expand Up @@ -131,7 +131,7 @@ func resourceArmLoadBalancerBackendAddressPool() *pluginsdk.Resource {
}

if !features.ThreePointOhBeta() {
s["resource_group_name"] = azure.SchemaResourceGroupNameDeprecatedComputed()
s["resource_group_name"] = commonschema.ResourceGroupNameDeprecatedComputed()
s["backend_address"] = &pluginsdk.Schema{
Type: pluginsdk.TypeSet,
Optional: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/services/policy/assignment_base_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (br assignmentBaseResource) arguments(fields map[string]*pluginsdk.Schema)
Optional: true,
},

"location": azure.SchemaLocationOptional(),
"location": commonschema.LocationOptional(),

"identity": commonschema.SystemAssignedIdentityOptional(),

Expand Down
3 changes: 2 additions & 1 deletion internal/services/policy/policy_assignment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/resources/mgmt/2021-06-01-preview/policy"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -84,7 +85,7 @@ resources and will be removed in version 3.0 of the Azure Provider.
Optional: true,
},

"location": azure.SchemaLocationOptional(),
"location": commonschema.LocationOptional(),

//lintignore:XS003
"identity": {
Expand Down
2 changes: 1 addition & 1 deletion internal/services/synapse/synapse_workspace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func resourceSynapseWorkspace() *pluginsdk.Resource {
return commonschema.SystemAssignedIdentityComputed()
}(),

"managed_resource_group_name": azure.SchemaResourceGroupNameOptionalComputed(),
"managed_resource_group_name": commonschema.ResourceGroupNameOptionalComputed(),

"azure_devops_repo": {
Type: pluginsdk.TypeList,
Expand Down
6 changes: 4 additions & 2 deletions internal/services/web/app_service_environment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"

"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2021-02-01/web"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
Expand Down Expand Up @@ -504,9 +506,9 @@ func resourceAppServiceEnvironmentSchema() map[string]*pluginsdk.Schema {

"resource_group_name": func() *pluginsdk.Schema {
if !features.ThreePointOhBeta() {
return azure.SchemaResourceGroupNameOptionalComputed()
return commonschema.ResourceGroupNameOptionalComputed()
}
return azure.SchemaResourceGroupName()
return commonschema.ResourceGroupName()
}(),

"tags": tags.ForceNewSchema(),
Expand Down

0 comments on commit a22742c

Please sign in to comment.