Skip to content

Commit

Permalink
feat: add http status code functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen committed Mar 15, 2024
1 parent a47efea commit 22d6ce3
Show file tree
Hide file tree
Showing 10 changed files with 544 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "is_http_2xx_status_code function - assert"
page_title: "is_http_2xx function - assert"
subcategory: ""
description: |-
Checks whether the HTTP status code is a valid 2xx status code
---

# function: is_http_2xx_status_code
# function: is_http_2xx



Expand All @@ -16,11 +16,11 @@ description: |-

<!-- signature generated by tfplugindocs -->
```text
is_http_2xx_status_code(status_code number) bool
is_http_2xx(status_code number) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `status_code` (Number) The HTTP status code
1. `status_code` (Number) The HTTP status code to check

Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ import (
)

var (
_ function.Function = IsHTTP2XXStatusCodeFunction{}
_ function.Function = IsHTTP2XXFunction{}
)

func NewIsHTTP2XXStatusCodeFunction() function.Function {
return IsHTTP2XXStatusCodeFunction{}
func NewIsHTTP2XXFunction() function.Function {
return IsHTTP2XXFunction{}
}

type IsHTTP2XXStatusCodeFunction struct{}
type IsHTTP2XXFunction struct{}

func (r IsHTTP2XXStatusCodeFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "is_http_2xx_status_code"
func (r IsHTTP2XXFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "is_http_2xx"
}

func (r IsHTTP2XXStatusCodeFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) {
func (r IsHTTP2XXFunction) 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",
Parameters: []function.Parameter{
function.Int64Parameter{
AllowNullValue: false,
AllowUnknownValues: true,
Description: "The HTTP status code",
Description: "The HTTP status code to check",
Name: "status_code",
},
},
Return: function.BoolReturn{},
}
}

func (r IsHTTP2XXStatusCodeFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
func (r IsHTTP2XXFunction) 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestIsHTTP2XXStatusCodeFunction_basic(t *testing.T) {
func TestIsHTTP2XXFunction_basic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))),
Expand All @@ -21,7 +21,7 @@ func TestIsHTTP2XXStatusCodeFunction_basic(t *testing.T) {
{
Config: `
output "test" {
value = provider::assert::is_http_2xx_status_code(200)
value = provider::assert::is_http_2xx(200)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
Expand All @@ -32,7 +32,7 @@ func TestIsHTTP2XXStatusCodeFunction_basic(t *testing.T) {
})
}

func TestIsHTTP2XXStatusCodeFunction_httpCreated(t *testing.T) {
func TestIsHTTP2XXFunction_httpCreated(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))),
Expand All @@ -42,10 +42,10 @@ func TestIsHTTP2XXStatusCodeFunction_httpCreated(t *testing.T) {
{
Config: `
locals {
http_created = 201
http_created = 201
}
output "test" {
value = provider::assert::is_http_2xx_status_code(local.http_created)
value = provider::assert::is_http_2xx(local.http_created)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
Expand All @@ -56,7 +56,7 @@ func TestIsHTTP2XXStatusCodeFunction_httpCreated(t *testing.T) {
})
}

func TestIsHTTP2XXStatusCodeFunction_imATeaPot(t *testing.T) {
func TestIsHTTP2XXFunction_httpForbidden(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))),
Expand All @@ -66,10 +66,10 @@ func TestIsHTTP2XXStatusCodeFunction_imATeaPot(t *testing.T) {
{
Config: `
locals {
im_a_teapot = 418
forbidden = 403
}
output "test" {
value = provider::assert::is_http_2xx_status_code(local.im_a_teapot)
value = provider::assert::is_http_2xx(local.forbidden)
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down
68 changes: 68 additions & 0 deletions internal/provider/is_http_3xx_function.go
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
}
}
105 changes: 105 additions & 0 deletions internal/provider/is_http_3xx_function_test.go
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"),
),
},
},
})
}
Loading

0 comments on commit 22d6ce3

Please sign in to comment.