generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add http status code functions
- Loading branch information
1 parent
a47efea
commit 22d6ce3
Showing
10 changed files
with
544 additions
and
22 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 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,68 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-3.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
) | ||
|
||
var ( | ||
_ function.Function = IsHTTP3XXFunction{} | ||
) | ||
|
||
func NewIsHTTP3XXFunction() function.Function { | ||
return IsHTTP3XXFunction{} | ||
} | ||
|
||
type IsHTTP3XXFunction struct{} | ||
|
||
func (r IsHTTP3XXFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "is_http_3xx" | ||
} | ||
|
||
func (r IsHTTP3XXFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Checks whether the HTTP status code is a valid 3xx status code", | ||
Parameters: []function.Parameter{ | ||
function.Int64Parameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: true, | ||
Description: "The HTTP status code to check", | ||
Name: "status_code", | ||
}, | ||
}, | ||
Return: function.BoolReturn{}, | ||
} | ||
} | ||
|
||
func (r IsHTTP3XXFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var statusCode int | ||
resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &statusCode)) | ||
if resp.Error != nil { | ||
return | ||
} | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, is3xxStatusCode(statusCode))) | ||
} | ||
|
||
// isValid3xxStatusCode checks if an HTTP status code is within the 3xx range | ||
func is3xxStatusCode(statusCode int) bool { | ||
switch statusCode { | ||
case | ||
http.StatusMultipleChoices, | ||
http.StatusMovedPermanently, | ||
http.StatusFound, | ||
http.StatusSeeOther, | ||
http.StatusNotModified, | ||
http.StatusUseProxy, | ||
http.StatusTemporaryRedirect, | ||
http.StatusPermanentRedirect: | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
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,105 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-3.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestIsHTTP3XXFunction_basic(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
output "test" { | ||
value = provider::assert::is_http_3xx(300) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestIsHTTP3XXFunction_httpMovedPermanently(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
locals { | ||
moved_permanently = 301 | ||
} | ||
output "test" { | ||
value = provider::assert::is_http_3xx(local.moved_permanently) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "false"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestIsHTTP3XXFunction_httpForbidden(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
locals { | ||
forbidden = 403 | ||
} | ||
output "test" { | ||
value = provider::assert::is_http_3xx(local.forbidden) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "false"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestIsHTTP3XXFunction_httpCreated(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))), | ||
}, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
locals { | ||
http_created = 201 | ||
} | ||
output "test" { | ||
value = provider::assert::is_http_3xx(local.http_created) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.