Skip to content

Commit

Permalink
Merge pull request #4213 from terraform-providers/provider/more_depre…
Browse files Browse the repository at this point in the history
…cations

provider: deprecate and move final functions in provider.go
  • Loading branch information
tombuildsstuff committed Sep 5, 2019
2 parents b5e7a98 + 01ff305 commit 8105521
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 68 deletions.
12 changes: 12 additions & 0 deletions azurerm/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/state"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

// NOTE: these methods are deprecated, but provided to ease compatibility for open PR's
Expand Down Expand Up @@ -60,3 +62,13 @@ func evaluateSchemaValidateFunc(i interface{}, k string, validateFunc schema.Sch

return true, nil
}

// nolint: deadcode unused
func base64Encode(data string) string {
return utils.Base64EncodeIfNot(data)
}

// nolint: deadcode unused
func ignoreCaseStateFunc(val interface{}) string {
return state.IgnoreCase(val)
}
9 changes: 9 additions & 0 deletions azurerm/internal/tf/state/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package state

import "strings"

// IgnoreCase is a StateFunc from helper/schema that converts the
// supplied value to lower before saving to state for consistency.
func IgnoreCase(val interface{}) string {
return strings.ToLower(val.(string))
}
51 changes: 0 additions & 51 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package azurerm

import (
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"strings"

"github.com/hashicorp/go-azure-helpers/authentication"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -615,50 +611,3 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
return client, nil
}
}

// ignoreCaseStateFunc is a StateFunc from helper/schema that converts the
// supplied value to lower before saving to state for consistency.
func ignoreCaseStateFunc(val interface{}) string {
return strings.ToLower(val.(string))
}

func userDataDiffSuppressFunc(_, old, new string, _ *schema.ResourceData) bool {
return userDataStateFunc(old) == new
}

func userDataStateFunc(v interface{}) string {
switch s := v.(type) {
case string:
s = base64Encode(s)
hash := sha1.Sum([]byte(s))
return hex.EncodeToString(hash[:])
default:
return ""
}
}

func base64EncodedStateFunc(v interface{}) string {
switch s := v.(type) {
case string:
return base64Encode(s)
default:
return ""
}
}

// base64Encode encodes data if the input isn't already encoded using
// base64.StdEncoding.EncodeToString. If the input is already base64 encoded,
// return the original input unchanged.
func base64Encode(data string) string {
// Check whether the data is already Base64 encoded; don't double-encode
if isBase64Encoded(data) {
return data
}
// data has not been encoded encode and return
return base64.StdEncoding.EncodeToString([]byte(data))
}

func isBase64Encoded(data string) bool {
_, err := base64.StdEncoding.DecodeString(data)
return err == nil
}
15 changes: 12 additions & 3 deletions azurerm/resource_arm_application_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ func possibleArmApplicationGatewaySslCipherSuiteValues() []string {
return cipherSuites
}

func base64EncodedStateFunc(v interface{}) string {
switch s := v.(type) {
case string:
return utils.Base64EncodeIfNot(s)
default:
return ""
}
}

func resourceArmApplicationGateway() *schema.Resource {
return &schema.Resource{
Create: resourceArmApplicationGatewayCreateUpdate,
Expand Down Expand Up @@ -1679,7 +1688,7 @@ func expandApplicationGatewayAuthenticationCertificates(d *schema.ResourceData)
data := v["data"].(string)

// data must be base64 encoded
encodedData := base64Encode(data)
encodedData := utils.Base64EncodeIfNot(data)

output := network.ApplicationGatewayAuthenticationCertificate{
Name: utils.String(name),
Expand Down Expand Up @@ -3118,7 +3127,7 @@ func expandApplicationGatewaySslCertificates(d *schema.ResourceData) *[]network.
password := v["password"].(string)

// data must be base64 encoded
data = base64Encode(data)
data = utils.Base64EncodeIfNot(data)

output := network.ApplicationGatewaySslCertificate{
Name: utils.String(name),
Expand Down Expand Up @@ -3169,7 +3178,7 @@ func flattenApplicationGatewaySslCertificates(input *[]network.ApplicationGatewa

if name == existingName {
if data := existingCerts["data"]; data != nil {
v := base64Encode(data.(string))
v := utils.Base64EncodeIfNot(data.(string))
output["data"] = v
}

Expand Down
3 changes: 2 additions & 1 deletion azurerm/resource_arm_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/state"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func resourceArmLoadBalancer() *schema.Resource {
string(network.Dynamic),
string(network.Static),
}, true),
StateFunc: ignoreCaseStateFunc,
StateFunc: state.IgnoreCase,
DiffSuppressFunc: suppress.CaseDifference,
},

Expand Down
3 changes: 2 additions & 1 deletion azurerm/resource_arm_loadbalancer_nat_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/state"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -48,7 +49,7 @@ func resourceArmLoadBalancerNatPool() *schema.Resource {
"protocol": {
Type: schema.TypeString,
Required: true,
StateFunc: ignoreCaseStateFunc,
StateFunc: state.IgnoreCase,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(network.TransportProtocolAll),
Expand Down
3 changes: 2 additions & 1 deletion azurerm/resource_arm_loadbalancer_nat_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/state"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func resourceArmLoadBalancerNatRule() *schema.Resource {
"protocol": {
Type: schema.TypeString,
Required: true,
StateFunc: ignoreCaseStateFunc,
StateFunc: state.IgnoreCase,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(network.TransportProtocolAll),
Expand Down
3 changes: 2 additions & 1 deletion azurerm/resource_arm_loadbalancer_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/state"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func resourceArmLoadBalancerProbe() *schema.Resource {
Type: schema.TypeString,
Computed: true,
Optional: true,
StateFunc: ignoreCaseStateFunc,
StateFunc: state.IgnoreCase,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(network.ProbeProtocolHTTP),
Expand Down
3 changes: 2 additions & 1 deletion azurerm/resource_arm_loadbalancer_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/state"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -67,7 +68,7 @@ func resourceArmLoadBalancerRule() *schema.Resource {
"protocol": {
Type: schema.TypeString,
Required: true,
StateFunc: ignoreCaseStateFunc,
StateFunc: state.IgnoreCase,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(network.TransportProtocolAll),
Expand Down
3 changes: 2 additions & 1 deletion azurerm/resource_arm_network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/state"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -104,7 +105,7 @@ func resourceArmNetworkInterface() *schema.Resource {
string(network.Dynamic),
string(network.Static),
}, true),
StateFunc: ignoreCaseStateFunc,
StateFunc: state.IgnoreCase,
DiffSuppressFunc: suppress.CaseDifference,
},

Expand Down
3 changes: 2 additions & 1 deletion azurerm/resource_arm_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/state"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -66,7 +67,7 @@ func resourceArmPublicIp() *schema.Resource {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppress.CaseDifference,
StateFunc: ignoreCaseStateFunc,
StateFunc: state.IgnoreCase,
ConflictsWith: []string{"allocation_method"},
Computed: true,
Deprecated: "this property has been deprecated in favor of `allocation_method` to better match the api",
Expand Down
20 changes: 19 additions & 1 deletion azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package azurerm

import (
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"log"
"strings"
Expand All @@ -26,6 +28,22 @@ import (

var virtualMachineResourceName = "azurerm_virtual_machine"

// TODO move into internal/tf/suppress/base64.go
func userDataDiffSuppressFunc(_, old, new string, _ *schema.ResourceData) bool {
return userDataStateFunc(old) == new
}

func userDataStateFunc(v interface{}) string {
switch s := v.(type) {
case string:
s = utils.Base64EncodeIfNot(s)
hash := sha1.Sum([]byte(s))
return hex.EncodeToString(hash[:])
default:
return ""
}
}

func resourceArmVirtualMachine() *schema.Resource {
return &schema.Resource{
Create: resourceArmVirtualMachineCreateUpdate,
Expand Down Expand Up @@ -1457,7 +1475,7 @@ func expandAzureRmVirtualMachineOsProfile(d *schema.ResourceData) (*compute.OSPr
}

if v := osProfile["custom_data"].(string); v != "" {
v = base64Encode(v)
v = utils.Base64EncodeIfNot(v)
profile.CustomData = &v
}

Expand Down
8 changes: 2 additions & 6 deletions azurerm/resource_arm_virtual_machine_scale_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -1395,11 +1395,7 @@ func flattenAzureRMVirtualMachineScaleSetOsProfile(d *schema.ResourceData, profi
result["custom_data"] = *profile.CustomData
} else {
// look up the current custom data
value := d.Get("os_profile.0.custom_data").(string)
if !isBase64Encoded(value) {
value = base64Encode(value)
}
result["custom_data"] = value
result["custom_data"] = utils.Base64EncodeIfNot(d.Get("os_profile.0.custom_data").(string))
}

return []interface{}{result}
Expand Down Expand Up @@ -1904,7 +1900,7 @@ func expandAzureRMVirtualMachineScaleSetsOsProfile(d *schema.ResourceData) (*com
}

if customData != "" {
customData = base64Encode(customData)
customData = utils.Base64EncodeIfNot(customData)
osProfile.CustomData = &customData
}

Expand Down
20 changes: 20 additions & 0 deletions azurerm/utils/base64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package utils

import "encoding/base64"

// Base64EncodeIfNot encodes data if the input isn't already encoded using
// base64.StdEncoding.EncodeToString. If the input is already base64 encoded,
// return the original input unchanged.
func Base64EncodeIfNot(data string) string {
// Check whether the data is already Base64 encoded; don't double-encode
if base64IsEncoded(data) {
return data
}
// data has not been encoded encode and return
return base64.StdEncoding.EncodeToString([]byte(data))
}

func base64IsEncoded(data string) bool {
_, err := base64.StdEncoding.DecodeString(data)
return err == nil
}
File renamed without changes.

0 comments on commit 8105521

Please sign in to comment.