Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asb/reorg #1

Merged
merged 7 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean up util package.
  • Loading branch information
andrewsomething committed Feb 3, 2023
commit 661e576643e34069ee441d7e7f2988606ea2d643
17 changes: 17 additions & 0 deletions digitalocean/util/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package util

import (
"strings"

"github.com/digitalocean/godo"
)

// IsDigitalOceanError detects if a given error is a *godo.ErrorResponse for
// the specified code and message.
func IsDigitalOceanError(err error, code int, message string) bool {
if err, ok := err.(*godo.ErrorResponse); ok {
return err.Response.StatusCode == code &&
strings.Contains(strings.ToLower(err.Message), strings.ToLower(message))
}
return false
}
15 changes: 12 additions & 3 deletions digitalocean/util/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import (
"crypto/sha1"
"encoding/hex"
"hash/crc32"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// HashString produces a hash of a string.
func HashString(s string) string {
hash := sha1.Sum([]byte(s))
return hex.EncodeToString(hash[:])
}

// HashStringIgnoreCase is a helper function for sets of strings that are case insensitive
func HashStringIgnoreCase(v interface{}) int {
return SDKHashString(strings.ToLower(v.(string)))
}

// HashStringStateFunc implements a schema.SchemaStateFunc with HashString
func HashStringStateFunc() schema.SchemaStateFunc {
return func(v interface{}) string {
switch v.(type) {
Expand All @@ -24,9 +32,10 @@ func HashStringStateFunc() schema.SchemaStateFunc {
}
}

// hashcode.String in the terraform-plugin-sdk was made internal to the SDK in v2.
// Embed the implementation here to allow same hash function to continue to be used
// by the code in this provider that used it for hash computation.
// SDKHashString implements hashcode.String from the terraform-plugin-sdk which
// was made internal to the SDK in v2. Embed the implementation here to allow
// same hash function to continue to be used by the code in this provider that
// used it for hash computation.
func SDKHashString(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
Expand Down
1 change: 1 addition & 0 deletions digitalocean/util/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// SetResourceDataFromMap sets a *schema.ResourceData from a map.
func SetResourceDataFromMap(d *schema.ResourceData, m map[string]interface{}) error {
for key, value := range m {
if err := d.Set(key, value); err != nil {
Expand Down
7 changes: 0 additions & 7 deletions digitalocean/util/set.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package util

import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Helper function for sets of strings that are case insensitive
func HashStringIgnoreCase(v interface{}) int {
return SDKHashString(strings.ToLower(v.(string)))
}

// GetSetChanges compares two *schema.Set, "old" and "new." It returns one
// *schema.Set only containing items not found in the "new" set and another
// containing items not found in the "old" set.
Expand Down
1 change: 1 addition & 0 deletions digitalocean/util/suppress.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// CaseSensitive implements a schema.SchemaDiffSuppressFunc that ignores case
func CaseSensitive(_, old, new string, _ *schema.ResourceData) bool {
return strings.ToLower(old) == strings.ToLower(new)
}
9 changes: 0 additions & 9 deletions digitalocean/util/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package util

import (
"context"
"strings"
"time"

"github.com/digitalocean/godo"
Expand Down Expand Up @@ -44,11 +43,3 @@ func WaitForAction(client *godo.Client, action *godo.Action) error {
}).WaitForState()
return err
}

func IsDigitalOceanError(err error, code int, message string) bool {
if err, ok := err.(*godo.ErrorResponse); ok {
return err.Response.StatusCode == code &&
strings.Contains(strings.ToLower(err.Message), strings.ToLower(message))
}
return false
}