-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate r/webhook_recipient to Framework (#568)
This migrates `r/webhook_recipient` from the Plugin SDKv2 to the Plugin Framework. In the process, also updates `secret` to be optional to match the upstream change.
- Loading branch information
Showing
15 changed files
with
548 additions
and
93 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
This file was deleted.
Oops, something went wrong.
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
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,66 @@ | ||
package validation | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
var _ validator.String = isValidURLValidator{} | ||
|
||
type isValidURLValidator struct { | ||
schemes []string | ||
} | ||
|
||
func (v isValidURLValidator) Description(_ context.Context) string { | ||
return "value must be a valid URL" | ||
} | ||
|
||
func (v isValidURLValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
func (v isValidURLValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) { | ||
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
u, err := url.Parse(request.ConfigValue.ValueString()) | ||
if err != nil { | ||
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( | ||
request.Path, | ||
v.Description(ctx), | ||
fmt.Sprintf("%q: %s", request.ConfigValue.ValueString(), err.Error()), | ||
)) | ||
return | ||
} | ||
|
||
if u.Host == "" { | ||
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( | ||
request.Path, | ||
v.Description(ctx)+" but is missing a host", | ||
request.ConfigValue.ValueString(), | ||
)) | ||
} | ||
|
||
if !slices.Contains(v.schemes, u.Scheme) { | ||
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic( | ||
request.Path, | ||
fmt.Sprintf("%s with a schema of %q", v.Description(ctx), strings.Join(v.schemes, ",")), | ||
request.ConfigValue.ValueString(), | ||
)) | ||
} | ||
} | ||
|
||
// IsURLWithHTTPorHTTPS returns an AttributeValidator which ensures that any configured | ||
// attribute value is a valid HTTP or HTTPS URL. | ||
// | ||
// Null (unconfigured) and unknown (known after apply) values are skipped. | ||
func IsURLWithHTTPorHTTPS() validator.String { | ||
return isValidURLValidator{[]string{"http", "https"}} | ||
} |
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,73 @@ | ||
package validation_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper/validation" | ||
) | ||
|
||
func Test_IsURLWithHTTPorHTTPS(t *testing.T) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
|
||
type testCase struct { | ||
val types.String | ||
expectError bool | ||
} | ||
tests := map[string]testCase{ | ||
"unknown": { | ||
val: types.StringUnknown(), | ||
}, | ||
"null": { | ||
val: types.StringNull(), | ||
}, | ||
"valid http": { | ||
val: types.StringValue("http://sub.example.com/a/b/c/d?e=f#g"), | ||
}, | ||
"valid https": { | ||
val: types.StringValue("https://sub.example.com/a/b/c/d?e=f#g"), | ||
}, | ||
"empty": { | ||
val: types.StringValue(""), | ||
expectError: true, | ||
}, | ||
"garbage": { | ||
val: types.StringValue("not-a-url"), | ||
expectError: true, | ||
}, | ||
"missing host": { | ||
val: types.StringValue("http:///a/b/c/d?e=f#g"), | ||
expectError: true, | ||
}, | ||
"invalid scheme": { | ||
val: types.StringValue("ftp://sub.example.com/"), | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
request := validator.StringRequest{ | ||
Path: path.Root("test"), | ||
PathExpression: path.MatchRoot("test"), | ||
ConfigValue: test.val, | ||
} | ||
response := validator.StringResponse{} | ||
validation.IsURLWithHTTPorHTTPS().ValidateString(ctx, request, &response) | ||
|
||
assert.Equal(t, | ||
test.expectError, | ||
response.Diagnostics.HasError(), | ||
"unexpected error: %s", response.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,12 @@ | ||
package models | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type WebhookRecipientModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
Secret types.String `tfsdk:"secret"` | ||
URL types.String `tfsdk:"url"` | ||
} |
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
Oops, something went wrong.