generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement 'is_http_2xx_status_code'
- Loading branch information
1 parent
3e7f50d
commit 6661ca3
Showing
4 changed files
with
181 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,26 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "is_http_2xx_status_code function - terraform-provider-assert" | ||
subcategory: "" | ||
description: |- | ||
Checks whether the HTTP status code is a valid 2xx status code | ||
--- | ||
|
||
# function: is_http_2xx_status_code | ||
|
||
Checks whether the HTTP status code is a valid 2xx status code | ||
|
||
|
||
|
||
## Signature | ||
|
||
<!-- signature generated by tfplugindocs --> | ||
```text | ||
is_http_2xx_status_code(status_code number) bool | ||
``` | ||
|
||
## Arguments | ||
|
||
<!-- arguments generated by tfplugindocs --> | ||
1. `status_code` (Number) The HTTP status code | ||
|
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 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
) | ||
|
||
var ( | ||
_ function.Function = IsHTTP2XXStatusCodeFunction{} | ||
) | ||
|
||
func NewIsHTTP2XXStatusCodeFunction() function.Function { | ||
return IsHTTP2XXStatusCodeFunction{} | ||
} | ||
|
||
type IsHTTP2XXStatusCodeFunction struct{} | ||
|
||
func (r IsHTTP2XXStatusCodeFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "is_http_2xx_status_code" | ||
} | ||
|
||
func (r IsHTTP2XXStatusCodeFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Checks whether the HTTP status code is a valid 2xx status code", | ||
MarkdownDescription: "Checks whether the HTTP status code is a valid 2xx status code", | ||
Parameters: []function.Parameter{ | ||
function.Int64Parameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: true, | ||
Description: "The HTTP status code", | ||
Name: "status_code", | ||
}, | ||
}, | ||
Return: function.BoolReturn{}, | ||
} | ||
} | ||
|
||
func (r IsHTTP2XXStatusCodeFunction) 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, is2xxStatusCode(statusCode))) | ||
} | ||
|
||
// isValid2xxStatusCode checks if an HTTP status code is within the 2xx range | ||
func is2xxStatusCode(statusCode int) bool { | ||
switch statusCode { | ||
case | ||
http.StatusOK, | ||
http.StatusCreated, | ||
http.StatusAccepted, | ||
http.StatusNonAuthoritativeInfo, | ||
http.StatusNoContent, | ||
http.StatusResetContent, | ||
http.StatusPartialContent, | ||
http.StatusMultiStatus, | ||
http.StatusAlreadyReported, | ||
http.StatusIMUsed: | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
internal/provider/is_http_2xx_status_code_function_test.go
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,81 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.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 TestIsHTTP2XXStatusCodeFunction_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_2xx_status_code(200) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestIsHTTP2XXStatusCodeFunction_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_2xx_status_code(local.http_created) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestIsHTTP2XXStatusCodeFunction_imATeaPot(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 { | ||
im_a_teapot = 418 | ||
} | ||
output "test" { | ||
value = provider::assert::is_http_2xx_status_code(local.im_a_teapot) | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckOutput("test", "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