Skip to content

Commit

Permalink
Merge branch 'main' into r/aks-rbac-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stephybun committed Feb 23, 2022
2 parents 0170be0 + a22742c commit a7f508e
Show file tree
Hide file tree
Showing 40 changed files with 232 additions and 357 deletions.
22 changes: 0 additions & 22 deletions helpers/azure/deprecated.go

This file was deleted.

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
50 changes: 0 additions & 50 deletions helpers/azure/sku.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"fmt"
"strconv"
"strings"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-07-01/compute"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func SplitSku(sku string) (string, int32, error) {
Expand All @@ -23,50 +20,3 @@ func SplitSku(sku string) (string, int32, error) {

return skuParts[0], int32(capacity), nil
}

func ExpandOrchestratedVirtualMachineScaleSetSku(input string, capacity int) (*compute.Sku, error) {
skuParts := strings.Split(input, "_")

if len(skuParts) < 2 || strings.Contains(input, "__") || strings.Contains(input, " ") {
return nil, fmt.Errorf("'sku_name'(%q) is not formatted properly.", input)
}

sku := &compute.Sku{
Name: utils.String(input),
Capacity: utils.Int64(int64(capacity)),
Tier: utils.String("Standard"),
}

return sku, nil
}

func FlattenOrchestratedVirtualMachineScaleSetSku(input *compute.Sku) (*string, error) {
var skuName string
if input != nil && input.Name != nil {
if strings.HasPrefix(strings.ToLower(*input.Name), "standard") {
skuName = *input.Name
} else {
skuName = fmt.Sprintf("Standard_%s", *input.Name)
}

return &skuName, nil
}

return nil, fmt.Errorf("Sku struct 'name' is nil")
}

func ValidateOrchestratedVirtualMachineScaleSetSku(input interface{}, key string) (warnings []string, errors []error) {
v, ok := input.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
return
}

skuParts := strings.Split(v, "_")

if len(skuParts) < 2 || strings.Contains(v, "__") || strings.Contains(v, " ") {
errors = append(errors, fmt.Errorf("%q(%q) is not formatted properly.", key, v))
}

return
}
11 changes: 0 additions & 11 deletions helpers/suppress/string.go

This file was deleted.

15 changes: 0 additions & 15 deletions internal/azuresdkhacks/notes.go

This file was deleted.

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
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2021-08-01/apimanagement"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/schemaz"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
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
File renamed without changes.
File renamed without changes.
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func resourceOrchestratedVirtualMachineScaleSet() *pluginsdk.Resource {
"sku_name": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: azure.ValidateOrchestratedVirtualMachineScaleSetSku,
ValidateFunc: computeValidate.OrchestratedVirtualMachineScaleSetSku,
},

"os_profile": OrchestratedVirtualMachineScaleSetOSProfileSchema(),
Expand Down Expand Up @@ -281,7 +281,7 @@ func resourceOrchestratedVirtualMachineScaleSetCreate(d *pluginsdk.ResourceData,
instances := d.Get("instances").(int)
if v, ok := d.GetOk("sku_name"); ok {
isLegacy = false
sku, err := azure.ExpandOrchestratedVirtualMachineScaleSetSku(v.(string), instances)
sku, err := expandOrchestratedVirtualMachineScaleSetSku(v.(string), instances)
if err != nil {
return fmt.Errorf("expanding 'sku_name': %+v", err)
}
Expand Down Expand Up @@ -888,7 +888,7 @@ func resourceOrchestratedVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData,
if d.HasChange("sku_name") {
updateInstances = true

sku, err = azure.ExpandOrchestratedVirtualMachineScaleSetSku(d.Get("sku_name").(string), instances)
sku, err = expandOrchestratedVirtualMachineScaleSetSku(d.Get("sku_name").(string), instances)
if err != nil {
return err
}
Expand Down Expand Up @@ -988,7 +988,7 @@ func resourceOrchestratedVirtualMachineScaleSetRead(d *pluginsdk.ResourceData, m
var skuName *string
var instances int
if resp.Sku != nil {
skuName, err = azure.FlattenOrchestratedVirtualMachineScaleSetSku(resp.Sku)
skuName, err = flattenOrchestratedVirtualMachineScaleSetSku(resp.Sku)
if err != nil || skuName == nil {
return fmt.Errorf("setting `sku_name`: %+v", err)
}
Expand Down Expand Up @@ -1181,3 +1181,34 @@ func resourceOrchestratedVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData,

return nil
}

func expandOrchestratedVirtualMachineScaleSetSku(input string, capacity int) (*compute.Sku, error) {
skuParts := strings.Split(input, "_")

if len(skuParts) < 2 || strings.Contains(input, "__") || strings.Contains(input, " ") {
return nil, fmt.Errorf("'sku_name'(%q) is not formatted properly.", input)
}

sku := &compute.Sku{
Name: utils.String(input),
Capacity: utils.Int64(int64(capacity)),
Tier: utils.String("Standard"),
}

return sku, nil
}

func flattenOrchestratedVirtualMachineScaleSetSku(input *compute.Sku) (*string, error) {
var skuName string
if input != nil && input.Name != nil {
if strings.HasPrefix(strings.ToLower(*input.Name), "standard") {
skuName = *input.Name
} else {
skuName = fmt.Sprintf("Standard_%s", *input.Name)
}

return &skuName, nil
}

return nil, fmt.Errorf("Sku struct 'name' is nil")
}
6 changes: 4 additions & 2 deletions utils/ssh_key.go → internal/services/compute/ssh_key.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package utils
package compute

import (
"fmt"
"strings"

"github.com/hashicorp/terraform-provider-azurerm/utils"
)

// NormalizeSSHKey attempts to remove invalid formatting and line breaks that can be present in some cases
Expand All @@ -24,5 +26,5 @@ func NormalizeSSHKey(input string) (*string, error) {

normalised := strings.Join(lines, "")

return String(normalised), nil
return utils.String(normalised), nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package compute

import "testing"

Expand Down
6 changes: 3 additions & 3 deletions internal/services/compute/ssh_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ func parseUsernameFromAuthorizedKeysPath(input string) *string {
}

func SSHKeyDiffSuppress(_, old, new string, _ *pluginsdk.ResourceData) bool {
oldNormalized, err := utils.NormalizeSSHKey(old)
oldNormalized, err := NormalizeSSHKey(old)
if err != nil {
log.Printf("[DEBUG] error normalising ssh key %q: %+v", old, err)
return false
}

newNormalized, err := utils.NormalizeSSHKey(new)
newNormalized, err := NormalizeSSHKey(new)
if err != nil {
log.Printf("[DEBUG] error normalising ssh key %q: %+v", new, err)
return false
Expand All @@ -138,7 +138,7 @@ func SSHKeySchemaHash(v interface{}) int {
var buf bytes.Buffer

if m, ok := v.(map[string]interface{}); ok {
normalisedKey, err := utils.NormalizeSSHKey(m["public_key"].(string))
normalisedKey, err := NormalizeSSHKey(m["public_key"].(string))
if err != nil {
log.Printf("[DEBUG] error normalising ssh key %q: %+v", m["public_key"].(string), err)
}
Expand Down
Loading

0 comments on commit a7f508e

Please sign in to comment.