-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: utility to convert labels to hcloud-go types (#892)
We often have to convert between the Terraform Plugin Framework and hcloud-go representations for labels. Will be used by #817
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package hcloudutil | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func TerraformLabelsToHCloud(ctx context.Context, inputLabels types.Map, outputLabels *map[string]string) diag.Diagnostics { | ||
var diagnostics diag.Diagnostics | ||
*outputLabels = make(map[string]string, len(inputLabels.Elements())) | ||
|
||
if !inputLabels.IsNull() { | ||
diagnostics.Append(inputLabels.ElementsAs(ctx, outputLabels, false)...) | ||
} | ||
|
||
return diagnostics | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package hcloudutil | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTerraformLabelsToHCloud(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputLabels types.Map | ||
wantLabels *map[string]string | ||
wantDiagnostics bool | ||
}{ | ||
{ | ||
name: "Some Labels", | ||
inputLabels: types.MapValueMust(types.StringType, map[string]attr.Value{"key1": types.StringValue("value1")}), | ||
wantLabels: &map[string]string{"key1": "value1"}, | ||
wantDiagnostics: false, | ||
}, | ||
{ | ||
name: "Empty Labels", | ||
inputLabels: types.MapNull(types.StringType), | ||
wantLabels: &map[string]string{}, | ||
wantDiagnostics: false, | ||
}, | ||
{ | ||
name: "Invalid Map Labels", | ||
inputLabels: types.MapValueMust(types.BoolType, map[string]attr.Value{"key1": types.BoolValue(true)}), | ||
wantLabels: &map[string]string{}, | ||
wantDiagnostics: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var outputLabels map[string]string | ||
diagnostics := TerraformLabelsToHCloud(context.Background(), tt.inputLabels, &outputLabels) | ||
assert.Equalf(t, tt.wantDiagnostics, diagnostics != nil, "Unexpected Diagnostics: %v", diagnostics) | ||
assert.Equalf(t, *tt.wantLabels, outputLabels, "Unexpected Labels") | ||
}) | ||
} | ||
} |