Skip to content

Commit

Permalink
refactor: misc cleanup of the azure and utils package (hashicorp#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff authored Feb 18, 2022
1 parent 30cbaef commit 8d3fb77
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 274 deletions.
22 changes: 0 additions & 22 deletions helpers/azure/deprecated.go

This file was deleted.

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.

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
File renamed without changes.
File renamed without changes.
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
2 changes: 1 addition & 1 deletion internal/services/compute/ssh_public_key_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func dataSourceSshPublicKeyRead(d *pluginsdk.ResourceData, meta interface{}) err

var publicKey *string
if props := resp.SSHPublicKeyResourceProperties; props.PublicKey != nil {
publicKey, err = utils.NormalizeSSHKey(*props.PublicKey)
publicKey, err = NormalizeSSHKey(*props.PublicKey)
if err != nil {
return fmt.Errorf("normalising public key: %+v", err)
}
Expand Down
22 changes: 22 additions & 0 deletions internal/services/compute/validate/orchestrated_vmss_sku.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package validate

import (
"fmt"
"strings"
)

func OrchestratedVirtualMachineScaleSetSku(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
}
4 changes: 2 additions & 2 deletions internal/services/dns/dns_aaaa_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func expandAzureRmDnsAaaaRecords(input []interface{}) *[]dns.AaaaRecord {
records := make([]dns.AaaaRecord, len(input))

for i, v := range input {
ipv6 := utils.NormalizeIPv6Address(v)
ipv6 := NormalizeIPv6Address(v)
records[i] = dns.AaaaRecord{
Ipv6Address: &ipv6,
}
Expand All @@ -225,7 +225,7 @@ func flattenAzureRmDnsAaaaRecords(records *[]dns.AaaaRecord) []string {
continue
}

results = append(results, utils.NormalizeIPv6Address(*record.Ipv6Address))
results = append(results, NormalizeIPv6Address(*record.Ipv6Address))
}
return results
}
2 changes: 1 addition & 1 deletion utils/network.go → internal/services/dns/ipv6_address.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package dns

import "net"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package dns

import (
"testing"
Expand Down
Loading

0 comments on commit 8d3fb77

Please sign in to comment.