Skip to content

Commit

Permalink
chore: remove unused/duplicate code to fix golangci-lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta committed Aug 14, 2024
1 parent e41184f commit 5d90458
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func setManagedObjectStorageData(d *schema.ResourceData, storage *upcloud.Manage
return diag.FromErr(err)
}

if err := d.Set("labels", utils.LabelSliceToMap(storage.Labels)); err != nil {
if err := d.Set("labels", utils.LabelsSliceToMap(storage.Labels)); err != nil {
return diag.FromErr(err)
}

Expand Down
14 changes: 10 additions & 4 deletions internal/service/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/UpCloudLtd/terraform-provider-upcloud/internal/service/storage"
"github.com/UpCloudLtd/terraform-provider-upcloud/internal/utils"
"github.com/UpCloudLtd/terraform-provider-upcloud/internal/validator"
)

const serverTitleLength int = 255
Expand All @@ -36,10 +37,15 @@ func ResourceServer() *schema.Resource {
},
Schema: map[string]*schema.Schema{
"hostname": {
Description: "A valid domain name",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateHostnameDiagFunc(1, 128),
Description: "A valid domain name",
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validation.AllDiag(
validation.ToDiagFunc(
validation.StringLenBetween(1, 128),
),
validator.ValidateDomainNameDiag,
),
},
"title": {
Description: "A short, informational description",
Expand Down
40 changes: 0 additions & 40 deletions internal/service/server/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,10 @@ import (
"strings"

"github.com/UpCloudLtd/terraform-provider-upcloud/internal/utils"
"github.com/UpCloudLtd/terraform-provider-upcloud/internal/validator"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/service"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func validateHostnameDiagFunc(min, max int) schema.SchemaValidateDiagFunc {
return func(v interface{}, path cty.Path) diag.Diagnostics {
var diags diag.Diagnostics
val, ok := v.(string)
if !ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Bad type",
Detail: "expected type to be string",
AttributePath: path,
})
return diags
}

if len(val) < min || len(val) > max {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Hostname length validation failed",
Detail: fmt.Sprintf("expected hostname length to be in the range (%d - %d), got %d", min, max, len(val)),
AttributePath: path,
})
return diags
}

if err := validator.ValidateDomainName(val); err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Hostname validation failed",
Detail: err.Error(),
AttributePath: path,
})
}

return diags
}
}

func validatePlan(ctx context.Context, service *service.Service, plan string) error {
if plan == "" {
return nil
Expand Down
84 changes: 0 additions & 84 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package utils

import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -22,16 +19,6 @@ func FilterZoneIDs(vs []upcloud.Zone, f func(upcloud.Zone) bool) []string {
return vsf
}

func FilterZones(vs []upcloud.Zone, f func(upcloud.Zone) bool) []upcloud.Zone {
vsf := make([]upcloud.Zone, 0)
for _, v := range vs {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}

func FilterNetworks(vs []upcloud.Network, fns ...func(upcloud.Network) (bool, error)) ([]upcloud.Network, error) {
vsf := []upcloud.Network{}

Expand Down Expand Up @@ -87,77 +74,6 @@ func ExpandStrings(data interface{}) []string {
return strSlice
}

// SetOfStringsToSlice transforms a terraform set of strings to a slice of strings
func SetOfStringsToSlice(ctx context.Context, data interface{}) ([]string, error) {
result := []string{}
providerErrMsg := "provider error: failed to transform set data"
debugLogPrefix := "transforming set of strings into slice failed;"

stringsSet, ok := data.(*schema.Set)
if !ok {
tflog.Debug(ctx, fmt.Sprintf("%s expected input data to be a schema.TypeSet but received %T", debugLogPrefix, data))
return result, fmt.Errorf(providerErrMsg)
}

for _, val := range stringsSet.List() {
valStr, ok := val.(string)
if !ok {
tflog.Debug(ctx, fmt.Sprintf("%s expected set elements to be of type string but received %T", debugLogPrefix, val))
return result, fmt.Errorf(providerErrMsg)
}

result = append(result, valStr)
}

return result, nil
}

// MapOfStringsToLabelSlice transforms a terraform map of strings to a LabelSlice
func MapOfStringsToLabelSlice(ctx context.Context, data interface{}) (upcloud.LabelSlice, error) {
result := upcloud.LabelSlice{}
providerErrMsg := "provider error: failed to transform labels data"
debugLogPrefix := "transforming map of strings into labels slice failed;"

labelsMap, ok := data.(map[string]interface{})
if !ok {
tflog.Debug(ctx, fmt.Sprintf("%s expected input data to be a map of strings but received %T", debugLogPrefix, data))
return result, fmt.Errorf(providerErrMsg)
}

for k, v := range labelsMap {
value, ok := v.(string)
if !ok {
tflog.Debug(ctx, fmt.Sprintf("%s expected map elements to be of type string but received %T", debugLogPrefix, v))
return result, fmt.Errorf(providerErrMsg)
}

result = append(result, upcloud.Label{
Key: k,
Value: value,
})
}

return result, nil
}

// LabelSliceToMap transorms `upcloud.LabelSlice` into a map of strings.
// This can be used to set labels fetched from the API into a state
func LabelSliceToMap(data upcloud.LabelSlice) map[string]string {
result := map[string]string{}
for _, label := range data {
result[label.Key] = label.Value
}

return result
}

// SliceOfStringToServerUUIDSlice converts slice of strings into `upcloud.ServerUUIDSlice`
func SliceOfStringToServerUUIDSlice(strs []string) upcloud.ServerUUIDSlice {
result := make(upcloud.ServerUUIDSlice, len(strs))
copy(result, strs)
return result
}

// StorageAddressFormat takes the address in any format and extracts the bus
// type only (ide/scsi/virtio)
func StorageAddressFormat(address string) string {
Expand Down

0 comments on commit 5d90458

Please sign in to comment.