-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
231 additions
and
23 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
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,62 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
// GetValidEnumValues returns a list of valid enum values for a given protobuf enum type. | ||
// it includes zero value as well to match its use in the current api | ||
func GetValidEnumValues[T protoreflect.Enum]() []string { | ||
var values []string | ||
var v T | ||
descriptor := v.Descriptor() | ||
for i := 0; i < descriptor.Values().Len(); i++ { | ||
values = append(values, string(descriptor.Values().Get(i).Name())) | ||
} | ||
return values | ||
} | ||
|
||
// EnumValueToPBWithDefault converts a string to a protobuf enum value. | ||
func EnumValueToPBWithDefault[T interface { | ||
~int32 | ||
protoreflect.Enum | ||
}]( | ||
dst *T, | ||
src types.String, | ||
defaultValue T, | ||
diagnostics *diag.Diagnostics, | ||
) { | ||
if src.IsNull() || src.ValueString() == "" { | ||
*dst = defaultValue | ||
return | ||
} | ||
|
||
var v T | ||
enumValue := v.Descriptor().Values().ByName(protoreflect.Name(src.ValueString())) | ||
if enumValue == nil { | ||
diagnostics.AddError( | ||
"InvalidEnumValue", | ||
fmt.Sprintf("The provided %s enum value %q is not valid.", v.Descriptor().FullName(), src.ValueString()), | ||
) | ||
return | ||
} | ||
|
||
*dst = T(enumValue.Number()) | ||
} | ||
|
||
func EnumValueFromPB[T interface { | ||
~int32 | ||
protoreflect.Enum | ||
}]( | ||
src T, | ||
) types.String { | ||
v := src.Descriptor().Values().ByNumber(protoreflect.EnumNumber(src)) | ||
if v == nil { | ||
return types.StringNull() | ||
} | ||
return types.StringValue(string(v.Name())) | ||
} |
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,63 @@ | ||
package provider_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/pomerium/enterprise-client-go/pb" | ||
"github.com/pomerium/enterprise-terraform-provider/internal/provider" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEnumValueToPB(t *testing.T) { | ||
t.Parallel() | ||
|
||
defaultValue := pb.IssuerFormat(-1) | ||
tests := []struct { | ||
name types.String | ||
expect pb.IssuerFormat | ||
expectError bool | ||
}{ | ||
{types.StringValue("IssuerHostOnly"), pb.IssuerFormat_IssuerHostOnly, false}, | ||
{types.StringValue("IssuerURI"), pb.IssuerFormat_IssuerURI, false}, | ||
{types.StringValue("InvalidInexistentTest"), pb.IssuerFormat(-2), true}, | ||
{types.StringNull(), defaultValue, false}, | ||
{types.StringValue(""), defaultValue, false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name.String(), func(t *testing.T) { | ||
var got pb.IssuerFormat | ||
var diagnostics diag.Diagnostics | ||
provider.EnumValueToPBWithDefault(&got, tt.name, defaultValue, &diagnostics) | ||
if tt.expectError { | ||
assert.True(t, diagnostics.HasError()) | ||
} else { | ||
require.False(t, diagnostics.HasError(), diagnostics.Errors()) | ||
assert.Equal(t, tt.expect, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestEnumValueFromPB(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name pb.IssuerFormat | ||
expect types.String | ||
}{ | ||
{pb.IssuerFormat_IssuerHostOnly, types.StringValue("IssuerHostOnly")}, | ||
{pb.IssuerFormat_IssuerURI, types.StringValue("IssuerURI")}, | ||
{pb.IssuerFormat(-1), types.StringNull()}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.expect.String(), func(t *testing.T) { | ||
got := provider.EnumValueFromPB(tt.name) | ||
assert.Equal(t, tt.expect, got) | ||
}) | ||
} | ||
} |
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
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
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
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,85 @@ | ||
package provider_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/pomerium/enterprise-client-go/pb" | ||
"github.com/pomerium/enterprise-terraform-provider/internal/provider" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConvertRouteFromPB(t *testing.T) { | ||
t.Run("jwt_issuer_format", func(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input pb.IssuerFormat | ||
expected string | ||
isNull bool | ||
}{ | ||
{ | ||
name: "host_only", | ||
input: pb.IssuerFormat_IssuerHostOnly, | ||
expected: "IssuerHostOnly", | ||
}, | ||
{ | ||
name: "uri", | ||
input: pb.IssuerFormat_IssuerURI, | ||
expected: "IssuerURI", | ||
}, | ||
{ | ||
name: "invalid value", | ||
input: pb.IssuerFormat(999), | ||
isNull: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
m := &provider.RouteModel{} | ||
r := &pb.Route{ | ||
JwtIssuerFormat: tc.input, | ||
} | ||
diags := provider.ConvertRouteFromPB(m, r) | ||
require.False(t, diags.HasError()) | ||
if tc.isNull { | ||
assert.True(t, m.JWTIssuerFormat.IsNull()) | ||
} else { | ||
assert.Equal(t, tc.expected, m.JWTIssuerFormat.ValueString()) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
func TestConvertRouteToPB(t *testing.T) { | ||
t.Run("jwt_issuer_format", func(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expected pb.IssuerFormat | ||
expectError bool | ||
}{ | ||
{"host_only", "IssuerHostOnly", pb.IssuerFormat_IssuerHostOnly, false}, | ||
{"uri", "IssuerURI", pb.IssuerFormat_IssuerURI, false}, | ||
{"invalid_value", "invalid_value", -1, true}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
m := &provider.RouteModel{ | ||
JWTIssuerFormat: types.StringValue(tc.input), | ||
} | ||
r, diag := provider.ConvertRouteToPB(context.Background(), m) | ||
if tc.expectError { | ||
require.True(t, diag.HasError()) | ||
} else { | ||
require.False(t, diag.HasError()) | ||
assert.Equal(t, tc.expected, r.JwtIssuerFormat) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
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